Question

CREATE TABLE employees (
  id         INT NOT NULL auto_increment PRIMARY KEY (ID),
  first_name VARCHAR(20) DEFAULT NULL,
  last_name  VARCHAR(20) DEFAULT NULL,
  salary     INT         DEFAULT NULL);

I think this is correct query to create table in Oracle database.. but it gives the following error:

ORA-00907: missing right parenthesis

How to correct the statement?

Was it helpful?

Solution

You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm

auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.

also, "id int not null auto_increment primary key (id)" does not need the last "(id)"

Using Oracle, you shoud try something like this

    CREATE SEQUENCE seq;

CREATE TABLE employees
  (
     id         INTEGER NOT NULL PRIMARY KEY,
     first_name VARCHAR2(20) DEFAULT NULL,
     last_name  VARCHAR2(20) DEFAULT NULL,
     salary     INTEGER DEFAULT NULL
  );

INSERT INTO employees
VALUES      (seq.NEXTVAL,
         'name',
         'last name',
         1);  

Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.

Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)

OTHER TIPS

EDITED : New feature 12c

CREATE TABLE employees(
  id          NUMBER GENERATED ALWAYS AS IDENTITY,
  first_name  VARCHAR2(30)
etc.
);

Why would you do default null?

The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To avoid possible changes in behavior, always use the VARCHAR2 datatype to store variable-length character strings.

Replace

  id         INT NOT NULL auto_increment PRIMARY KEY (ID),

with

  id         INT NOT NULL auto_increment PRIMARY KEY,

this is more efficient

CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top