Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

Oracle Create Table Primary Key

Need to verify the primary key syntax and usage.

CREATE TABLE customer  
     (  customer_no            NUMBER
      CONSTRAINT        customer_no_pk  PRIMARY KEY (customer_no)

When rows are inserted, will the customer_no increment by 1?

Does the insert statement need to specify the customer_no column name?

INSERT INTO customer(customer_no, cust_name, cust_phone)
VALUES( ???, 'Mike', '385-367-1234');

Thanks,
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

Oracle databases before 12c do not support the autoincrement. In version 12c it was introduced:

http://docs.oracle.com/database/121/DRDAA/migr_tools_feat.htm#DRDAA109

For purpose of the autoincrement columns you need to create sequence and on insert triger will populate the column with newxt val from sequence...

as part of the creation of the table you should do it

CREATE TABLE customer  (
  customer_no            NUMBER);

ALTER TABLE customer  ADD (
  CONSTRAINT customer_no_pk  PRIMARY KEY (customer_no ));

Open in new window

Avatar of Dovberman

ASKER

Yes, I should have said that I am using 10g.
Can I still include the constraint in the Create Table syntax?

CREATE TABLE customer  
      (  customer_no            NUMBER
       CONSTRAINT        customer_no_pk  PRIMARY KEY (customer_no)

I will find out more about insert triggers and new value for incrementing the next number in a sequence.

Thanks,
Here is the code

CREATE TABLE customer  
      (  customer_no            NUMBER 
       CONSTRAINT        customer_no_pk  PRIMARY KEY )

Open in new window

10g is definitely different.

CREATE TABLE customer  
       (  customer_no            NUMBER
        CONSTRAINT        customer_no_pk  PRIMARY KEY (customer_no)
Should be:

CREATE TABLE customer  
       (  customer_no            NUMBER
        CONSTRAINT        customer_no_pk  PRIMARY KEY )
       CREATE SEQUENCE customer_no_seq start with 1
     ,  birth_date          DATE
     , etc....

Is the sequence defined within the primary key column definition as shown above?

CREATE TABLE customer  
       (  customer_no            NUMBER
        CONSTRAINT        customer_no_pk  PRIMARY KEY )
       CREATE SEQUENCE customer_no_seq start with 1
     ,  birth_date          DATE
     , etc....

OR is it defined within the CREATE TABLE block as shown below?

CREATE TABLE customer  
       (  customer_no            NUMBER
        CONSTRAINT        customer_no_pk  PRIMARY KEY )
           ,  birth_date          DATE
     , etc....);

CREATE TABLE customer  
       (  customer_no            NUMBER
        CONSTRAINT        customer_no_pk  PRIMARY KEY )
       ,  birth_date          DATE
     , etc....);

     CREATE SEQUENCE customer_no_seq start with 1 -- Where is the table name referenced?

Thanks,
I found out that the created sequence can be shared and does not need to reference a specific table.
ASKER CERTIFIED SOLUTION
Avatar of Haris Dulic
Haris Dulic
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is new to me as a MS SQL Server developer.

MS SQL Server is similar to Oracle 12. You specify in the table creation that the primary key is self  increments by one.

This helps.

Thank you.
Excellent