Creating a Table with a Primary

When you create a database table you should define a primary key to uniquely identify rows within the table. A primary key allows you to relate a particular table with other tables in the database (for procedure examples, refer to the previous chapter). A table can have only one primary key; however, a primary key can consist of more than one column.

To create a table with a primary key, use the CONSTRAINT clause with the CREATE TABLE statement. The procedure in Hands-On 21-4 uses the following CONSTRAINT clause to create a single-field primary key based on the SupplierId field:


To create a table with a primary key based on two or more columns, specify column names in parentheses following the PRIMARY KEY keyword. For example, the following CONSTRAINT clause will create a primary key based on the SupplierId and SupplierName columns:

CONSTRAINT idxPrimary PRIMARY KEY (SupplierId, SupplierName)

0 0

Post a comment