Prev | SQL Engine Reference | Next |
PRIMARY KEY
Remarks
Include PRIMARY KEY in the ADD clause to add a primary key to a table definition. The primary key is a unique index that does not include null values. When you specify a primary key, Pervasive.SQL creates a unique index with the specified attributes on the defined group of columns.
Because a table can have only one primary key, you cannot add a primary key to a table that already has a primary key defined. To change the primary key of a table, delete the existing key using a DROP clause in an ALTER TABLE statement and add the new primary key.
Note
You must be logged in to the database using a database name before you can add a primary key or conduct any other referential integrity (RI) operation.
Include PRIMARY KEY in the ADD clause with the ALTER TABLE statement to add a primary key to a table definition.
Before adding the primary key, you must ensure that the columns in the primary key column list are defined as NOT NULL. A primary key is a unique index and can be created only on not nullable columns.
If a unique index on not nullable columns already exists, the ADD PRIMARY KEY does not create another unique index. Instead, the existing unique index is promoted to a primary key. For example, the following statements would promote the named index T1_C1C2 to be a primary key.
CREATE TABLE T1 (C1 INT NOT NULL, C2 CHAR(10) NOT NULL) CREATE UNIQUE INDEX T1_C1C2 ON T1(C1,C2) ALTER TABLE T1 ADD PRIMARY KEY(C1, C2)If such a primary key is dropped, the primary key would be switched to a unique index.
ALTER TABLE T1 DROP PRIMARY KEYIf no unique index on not nullable columns exists in the table, ADD PRIMARY KEY creates a unique index on not nullable columns. DROP PRIMARY KEY completely deletes the unique index.
Include a PRIMARY KEY clause with the CREATE TABLE statement to add a primary key to a table definition.
To define referential constraints on your database, you must include a PRIMARY KEY clause to specify the primary key on the parent table. The primary key can consist of one column or multiple columns but can only be defined on columns that are not null. The columns you specify must also appear in the column Definitions list of the CREATE TABLE statement.
When you specify a primary key, Pervasive.SQL creates an index with the specified attributes on the defined group of columns. If the columns are not specifically defined as NOT NULL in the CREATE TABLE statement, the SRDE forces the columns to be not nullable. The SRDE also creates a unique index on the columns.
For example, the following two statements yield the same results:
CREATE TABLE T1 (C1 INT, C2 CHAR(10), PRIMARY KEY(C1,c2)) CREATE TABLE T1 (C1 INT NOT NULL, C2 CHAR(10) NOT NULL, PRIMARY KEY(C1,c2))Examples
The following statement defines a primary key on a table called Faculty. (The ID column is defined as a unique index that does not include null values.)
ALTER TABLE Faculty ADD PRIMARY KEY (ID)See Also
Prev OPEN |
Contents Up Check for Revisions | Next PUBLIC |