将PostgreSQL串行主键转换为Oracle语句

时间:2015-04-23 06:56:05

标签: java oracle postgresql netbeans oracle11g

我有一个连接到数据库的java文件,这是代码:

package movies;

import java.sql.*;

public class CreateTable {

public static void main(String args[]) {
    Connection c = null;
    Statement stmt = null;
    String sql;
try {
        Class.forName("oracle.jdbc.OracleDriver");//driver
        c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "username", "password");//PUT DATABASE CONNECTION INFO  
        System.out.println("Opened database successfully from within CreateTable.java");
        stmt = c.createStatement();

        sql = "CREATE TABLE MOVIES "
                //+ "(ID  SERIAL PRIMARY KEY,"//can't figure out this statement and it works if I comment it out.
                + " (NAME         NVARCHAR2(255) PRIMARY KEY,"//I don't want this to be the primary key.
                + " YEAR          NVARCHAR2(255),"
                + " RATING        NVARCHAR2(16),"
                + " ACTORS        NVARCHAR2(1024))";
        stmt.executeUpdate(sql);
        stmt.close();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("Table created successfully");
    }
}

评论部分是我遇到问题的地方。我试图将PostgreSQL语句转换为Oracle 11g XE语句。我想要一个带序号的ID来识别它。 (即0001,0002,0003等)我该怎么做?你能提供一个例子吗?我现在感到茫然。我使用的是Netbeans 8.02,Oracle 11g XE和Apache Tomcat 8.0.15.0。

编辑我试图将ID列作为唯一的主键。我将NAME列作为主键消除,并将ID列作为新的主键。

2 个答案:

答案 0 :(得分:0)

我不认为你可以有两个单独的主键,如果你想在两列上使用主键,

edit :


 sql = "CREATE TABLE MOVIES "
                + "(ID  INT PRIMARY KEY,"  -- CHANGES
                + " NAME         NVARCHAR2(255),"
                + " YEAR          NVARCHAR2(255),"
                + " RATING        NVARCHAR2(16),"
                + " ACTORS        NVARCHAR2(1024))";

我已创建表格以显示语法现在在oracle中有效..

SQL>   create table movies
  2                               (id int primary key,
  3                               name NVARCHAR2(255),
  4                               year NVARCHAR2(255),
  5                               rating NVARCHAR2(255),
  6                               actors NVARCHAR2(1024));

Table created.

答案 1 :(得分:0)

此代码有效。请参阅以下评论以查看更改:

        try {
        Class.forName("oracle.jdbc.OracleDriver");//driver
        c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE", "username", "password");//PUT DATABASE CONNECTION INFO  
        System.out.println("Opened database successfully from within CreateTable.java");
        stmt = c.createStatement();
        sql = "CREATE TABLE MOVIES "
                //THE FOLLOWING LINE WAS CHANGED:
                + "(\"ID\" INTEGER PRIMARY KEY, \"TYPE\" VARCHAR2(32),"
                + " NAME         NVARCHAR2(255),"
                + " YEAR          NVARCHAR2(255),"
                + " RATING        NVARCHAR2(16),"
                + " ACTORS        NVARCHAR2(1024))";

        //THE FOLLOW TWO STATEMENTS WERE ADDED:
        String sql1 = "CREATE SEQUENCE MOVIES_SEQ START WITH 1 INCREMENT BY 1 NOMAXVALUE";
        String sql2 = "CREATE TRIGGER MOVIES_TRIGGER BEFORE INSERT ON MOVIES FOR EACH ROW BEGIN SELECT MOVIES_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END;";

        stmt.executeUpdate(sql);
        //THESE TWO WERE ALSO ADDED:
        stmt.executeUpdate(sql1);
        stmt.executeUpdate(sql2);

        stmt.close();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }