May 1st, 2009

How do Constraints Work?, PRIMARY KEY (Part 3 of 3)

Posted by admin in P. Table Creation

The format is:

          [CONSTRAINT column_constraint_name]
            PRIMARY KEY

                                                  or

          [CONSTRAINT table_constraint_name]
            PRIMARY KEY (key_columns)

The primary key can be defined as a column constraint:

          CREATE TABLE     SAMPLE_TBL (
                 SAMPLE_ID CHAR(12)    NOT NULL PRIMARY KEY,
                 ...       );

The primary key can be defined as an unnamed table constraint:

          CREATE TABLE       SAMPLE_TBL (
                 SAMPLE_ID   CHAR(12)    NOT NULL,
                 ...
                 PRIMARY KEY (SAMPLE_ID)
                 ...         );

                                                  or

          CREATE TABLE       SAMPLE_TBL (
                 SAMPLE1_ID  CHAR(12)    NOT NULL,
                 SAMPLE2_ID  CHAR(12)    NOT NULL,
                 ...
                 PRIMARY KEY (SAMPLE1_ID, SAMPLE2_ID)
                 ...         );

The primary key can be defined as a named table constraint:

          CREATE TABLE       SAMPLE_TBL (
                 SAMPLE_ID   CHAR(12)    NOT NULL,
                 ...
                 CONSTRAINT  SAMPLE_PK
                 PRIMARY KEY (SAMPLE_ID)
                 ...         );

                                                  or

          CREATE TABLE SAMPLE_TBL (
                 SAMPLE1_ID  CHAR(12)    NOT NULL,
                 SAMPLE2_ID  CHAR(12)    NOT NULL,
                 ...
                 CONSTRAINT  SAMPLE_PK
                 PRIMARY KEY (SAMPLE1_ID, SAMPLE2_ID)
                 ...         );

Comments are closed.

Sorry, the comment form is closed at this time.