mysql - 从 MySQL 切换到 Oracle 会不会很困难?

标签 mysql database oracle

下面是我如何运行我的 mysql 查询的一些示例代码,我通过一个我认为可能会简单地切换数据库的函数来运行它们。

下面是我运行的 mysql 查询示例,下面是实际函数。

如果我决定使用此设置,更改为不同的数据库类型(如 oracle 或其他数据库)会不会很困难?

是否可以只修改函数,或者我是否需要更改每个页面上的查询?

$sql_photo = "select * from friend_user_photo  where userid='$user_id' and  defaultphoto='yes' order by auto_id desc";
$result_photo = executeQuery($sql_photo);

function executeQuery($sql) {
    $result = mysql_query_2($sql);
    if(mysql_error()){
         $error = '<BR><center><font size="+1" face="arial" color="red">An Internal Error has Occured.<BR> The error has been recorded for review</font></center><br>';
        // If admin is viewing then we show the query code and the error returned
        if($_SESSION['auto_id'] == 1){
           $sql_formatted = highlight_string( stripslashes( $sql ), true );
           $error .= '<b>The MySQL Syntax Used</b><br>' . $sql_formatted . '<br><br><b>The MySQL Error Returned</b><br>' . mysql_error() ;
        }
       die($error);
    }
    return $result;
}

最佳答案

如果您坚持使用纯非专有 (ANSI) SQL,则无需修改任何查询。您可能会使用专有扩展的一个示例是分页。如果您在 Web 应用程序中进行任何分页,很可能您正在使用类似这样的东西:

select id, name, created from thing limit(0,20)

该查询不适用于 Oracle,因为 limit是 MySql 的专有扩展,您将不得不使用 Oracle 的 rownum 进行分页(它只能接受一个参数),所以基本上你必须重写你的分页查询,看起来像这样:

select * 
  from ( select /*+ FIRST_ROWS(n) */ 
  a.*, ROWNUM rnum 
      from ( your_query_goes_here, 
      with order by ) a 
      where ROWNUM <= 
      :MAX_ROW_TO_FETCH ) 
where rnum  >= :MIN_ROW_TO_FETCH;

还要考虑到您将使用 Oracle 扩展之一,因此您的数据库连接、操作和错误处理代码也必须重写 ( example from the oci8 extension docs ) 看起来更像:

if ($c = oci_connect("hr", "hr_password", "localhost/XE")) {
   echo "Successfully connected to Oracle.";
   oci_close($c);
 } else {
   $err = oci_error();
   echo "Oracle Connect Error " . $err['text'];
 }
   // Select Data...
   $s = oci_parse($c, "select * from tab1");
   oci_execute($s, OCI_DEFAULT);
   while (oci_fetch($s)) {
     echo "COL1 = " . oci_result($s, "COL1") .
        ", COL2 = " . oci_result($s, "COL2") . "<br>\n";
   }

如您所见,这并非易事,尤其是当您拥有大量 MySql 硬连线代码时。

如果您的应用程序的可移植性是一个主要问题,您应该认真考虑使用一个允许您完全抽象出数据库供应商的库。我用 Zend_Db (它顺便支持 Oracle)但还有其他的。

关于mysql - 从 MySQL 切换到 Oracle 会不会很困难?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1275868/

相关文章:

MySQL 触发器开始日期早于结束日期

php - 编辑前置任务时调整数据库中的后续任务日期

mysql - 跟踪数据库更改

Php登录到你的数据库

java - 使用 tomcat 数据源 - 如何通过 spring jndi 访问数据源以获取当前数据库池状态

php - MySQL 多重更新正在插入重复项

mysql - 在 MySQL 中处理多个时区

sql - 列中什么时候为空值 "safe"?

mysql - 多个表上的多对多关系

Oracle分层查询: how to include top-level parent