[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 861
  • Last Modified:

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
0
gudii9
Asked:
gudii9
  • 3
  • 2
  • 2
2 Solutions
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now