sqloracle-databaseparenthesesidentify

Why am I getting Error at line 3: ORA-00907: missing right parenthesis in my SQL when using IDENTITY


I am trying to create a table using SQL commands on an Oracle database 12C Enterprise Edition 12.2.0.1.0.

I want a new TRANS_num to be created when I add a new values to the table starting at 1 and incrementing by one.

my code is as follows:

CREATE TABLE Sales
(
    TRANS_num INTEGER  IDENTITY (1,1) PRIMARY KEY,
    c_name VARCHAR (35) NOT NULL,
    c_address VARCHAR (35) NOT NULL,
    c_phonenumber CHAR (10) NOT NULL,
);

This results in the below image after I copy and paste into Tera Term and run the code

enter image description here

any ideas at all? I have browsed some of the answers on here for similar problems but nothing seems to fix it. I am very new to coding, sorry if it is something simple.


Solution

  • Whereever you got the syntax from, it must have said, that it is for SQL Server or maybe some other DBMS but not Oracle.

    The closest thing since Oracle 12c is GENERATED ... AS IDENTITY. You can use

    ...
    trans_num integer GENERATED AS IDENTITY ...
    ...
    

    or

    ...
    trans_num integer GENERATED ALWAYS AS IDENTITY ...
    ...
    

    (ALWAYS is the default option if omitted) if you want, that the value is always generated and cannot be overwritten in an INSERT,

    ...
    trans_num integer GENERATED BY DEFAULT AS IDENTITY ...
    ...
    

    if you want that the value is generated when trans_num isn't specified in an INSERT but take the value given if it is specified (except for a given value of NULL, that will throw an error), or

    ...
    trans_num integer GENERATED BY DEFAULT ON NULL AS IDENTITY ...
    ...
    

    which will behave like BY DEFAULT without ON NULL except that NULL values can be given in an INSERT but will be overwritten by a generated value.

    Optionally options also used for CREATE SEQUENCE can follow, for example

    ... GENERATED ... AS IDENTITY (START WITH 1 INCREMENT BY 1) ...
    

    to start the values at 1 and increment them by 1 for each subsequent generated value. (START WITH 1 INCREMENT BY 1) would be what you have. But that's also the default, so you can leave it too.