table create with primary key, auto-increment error

Hi,

I am trying to creates simple table student3 in oracle10g using followingsyntax


CREATE TABLE student3(
  id VARCHAR2(20 BYTE) primary key NOT NULL AUTO_INCREMENT,
  sname VARCHAR2(20 BYTE) NOT NULL,
  sroll VARCHAR2(20 BYTE) NOT NULL,
  scourse VARCHAR2(20 BYTE) NOT NULL
) ;

in SQL plus. I am getting error like


SQL> CREATE TABLE student3(
  2    id VARCHAR2(20 BYTE) primary key NOT NULL AUTO_INCREMENT,
  3    sname VARCHAR2(20 BYTE) NOT NULL,
  4    sroll VARCHAR2(20 BYTE) NOT NULL,
  5    scourse VARCHAR2(20 BYTE) NOT NULL
  6  ) ;
  id VARCHAR2(20 BYTE) primary key NOT NULL AUTO_INCREMENT,
                                            *
ERROR at line 2:
ORA-00907: missing right parenthesis


Please advise on how can i fix this error. Any ideas, linkss, sample code, resources highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
flow01Commented:
remove AUTO_INCREMENT
In ORACLE you will have to use a sequence  to supply programmatically a unique key
0
 
gudii9Author Commented:
Hi,

What would be syntax to create table with sequence on primary key column 'id'. please advise
0
 
gatorvipCommented:
Once you create the sequence as flow01 said, you will  have two choices as to how to insert the data:

1. INSERT INTO student3(id, sname, sroll,scourse)
values(<your_sequence>.nextval, 'A', 'B','C')

or

2. create an Insert trigger on your table that will automatically fill the id field with the next value in the sequence, then your insert will look like this

INSERT INTO student3(sname, sroll,scourse)
values('A', 'B','C')

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
gudii9Author Commented:
>>>Once you create the sequence

How to create sequence through SQL Plus?  I remember doing easily earlier using toad etc clients. Through SQL Plus i have to code for it which i never did. Please advise
0
 
gatorvipCommented:
basic format is:


create sequence <sequence_name>
start with ...
increment by ...


see more details here:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_6015.htm
0
 
flow01Commented:
and the trigger (for <sequence_name> = example
create or replace trigger student_id
before insert
on student3
  for each row
begin
  select example.nextval
  into :new.id
  from dual;
end;
/
0
 
gudii9Author Commented:
thank you very much. I appreciate it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.