-2

I am beginner in sql.I am using sqlplus to run the sql query .I used simple query but it shows an error like "MISSING RIGHT PARENTHESIS".My objective is to create the autoincrement primary key .Can anyone solve the error?Thanks in advance...

create table student(rollno int identity(1,1) primary key,
                     name varchar(20),marks int);
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843

2 Answers2

0

For Oracle, the rollno column could be defined as NUMBER(0010) and primary key. Then you would need to add an ON INSERT trigger to populate rollno from a SEQUENCE. There are many samples of triggers and sequences on this site.

Stilgar
  • 451
  • 2
  • 6
0

In oracle 12 you can use a identity column to automatically fill your ID

CREATE TABLE students
(
    "ID" NUMBER GENERATED BY DEFAULT AS IDENTITY MINVALUE 1 MAXVALUE 9999999999 
    INCREMENT BY 1 START WITH 1 , 
    "NAME" VARCHAR2(20), 
    "MARKS" NUMBER(2,0),
    CONSTRAINT PK_STUDENTS PRIMARY KEY (ID) ENABLE 
);
/

This creates a table without any triggers needed and automatically fills the id column (of not specified with a value) with the next number up to 99999...

If you're using oracle 11 and below, you need a trigger on insert and assign a value (custom_sequence.nextval) to the id column.

CREATE TABLE students
(
    "ID" NUMBER(5,0) not null,
    "NAME" VARCHAR2(20),
    "MARKS" NUMBER(2,0),
    CONSTRAINT PK_STUDENTS PRIMARY KEY (ID) ENABLE 
);
/

CREATE SEQUENCE SEQ_STUDENTS INCREMENT BY 1 START WITH 1;
/
TRIGGER TC_students
before insert on students
for each row
begin
    if (:new.id is null) then
        select SEQ_students.nextval into :new.id from dual;
    end if;
end;
/

And please use VARCHAR2.

Kaushik Nayak
  • 29,706
  • 5
  • 28
  • 41