Specifying a Primary Key with PRIMARY KEY I described primary keys in “Primary Keys” in Chapter 2, but I’ll review the basics here: ◆ A primary key identifies each row uniquely in a table. ◆ No two rows can have the same primary- key value. ◆ Primary keys don’t allow nulls. ◆ Each table has exactly one primary key. ◆ A one-column key is a simple key; a multiple-column key is a composite key. ◆ In a composite key, values can be dupli- cated within one column, but each combination of values from all the key’s columns must be unique. ◆ A table can have more than one combi- nation of columns that uniquely identify its rows; each combination is a candidate key. The database designer picks one of the candidate keys to be the primary key. When you’re defining a primary-key con- straint, some important considerations are: ◆ A simple key can be a column constraint or a table constraint; a composite key always is a table constraint. See “Understanding Constraints” earlier in this chapter. ◆ You define a primary-key constraint by using the keywords PRIMARY KEY in a CREATE TABLE definition. ◆ As a table constraint, PRIMARY KEY makes you specify column name(s) explicitly. As a column constraint, PRIMARY KEY applies to the column in which it’s defined. ◆ The SQL standard lets you create a table without a primary key (in violation of the relational model). In practice, you always should define a primary key for every table. ◆ No more than one primary-key constraint is allowed in a table. ◆ In practice, primary-key constraints almost always are named explicitly. Use a CONSTRAINT clause to do so; see “Understanding Constraints” earlier in this chapter. ◆ The nullability of all PRIMARY KEY columns must be NOT NULL . If you don’t specify a nullability constraint, the DBMS sets all primary-key columns to NOT NULL implicitly; see “Forbidding Nulls with NOT NULL ” earlier in this chapter. ◆ You must specify a primary-key value explicitly when you INSERT a row unless the column’s data type generates a unique row identifier automatically; see “Other Data Types” in Chapter 3. For informa- tion about inserting rows, see “Inserting Rows with INSERT ” in Chapter 10. ◆ Primary-key values normally don’t change after they’re inserted. ◆ For considerations related to inserting, updating, and deleting primary keys that are referenced by foreign keys, see “Specifying a Foreign Key with FOREIGN KEY ” later in this chapter. ◆ The DBMS will create a unique index for a primary key automatically (see Chapter 12). 350 Chapter 11 Specifying a Primary Key with PRIMARY KEY To specify a simple primary key: ◆ To specify a simple primary key as a column constraint, add the follow- ing column constraint to a CREATE TABLE column definition: [CONSTRAINT constraint_name] PRIMARY KEY or To specify a simple primary key as a table constraint, add the following table constraint to a CREATE TABLE definition: [CONSTRAINT constraint_name] PRIMARY KEY (key_column) key_column is the name of the primary- key column. No more than one PRIMARY KEY constraint is allowed in a table. For the general syntax of CREATE TABLE , see “Creating Tables” earlier in this chapter. The CONSTRAINT clause is optional, and constraint_name is the name of the pri- mary-key constraint; see “Understanding Constraints” earlier in this chapter. Listings 11.8a, 11.8b, and 11.8c show three equivalent ways to define a simple primary key for the sample-database table publishers . Listing 11.8a uses a column constraint to designate the primary-key column. This syntax shows the easiest way to create a simple primary key. Listing 11.8b uses an unnamed table con- straint to specify the primary key. I’ve added an explicit NOT NULL column constraint to pub_id , but it’s unnecessary because the DBMS sets this constraint implicitly and silently (except for DB2; see the DBMS Tip later in this section). Listing 11.8c uses a named table constraint to specify the primary key. This syntax shows the preferred way to add a primary key; you can use the name publishers_pk if you decide to change or delete the key later. See “Altering a Table with ALTER TABLE ” later in this chapter. 351 Creating, Altering, and Dropping Tables Specifying a Primary Key with PRIMARY KEY Listing 11.8a Define a simple primary key for the sample-database table publishers by using a column constraint. CREATE TABLE publishers ( pub_id CHAR(3) PRIMARY KEY, pub_name VARCHAR(20) NOT NULL , city VARCHAR(15) NOT NULL , state CHAR(2) , country VARCHAR(15) NOT NULL ); Listing Listing 11.8b Define a simple primary key for the sample-database table publishers by using an unnamed table constraint. CREATE TABLE publishers ( pub_id CHAR(3) NOT NULL, pub_name VARCHAR(20) NOT NULL, city VARCHAR(15) NOT NULL, state CHAR(2) , country VARCHAR(15) NOT NULL, PRIMARY KEY (pub_id) ); Listing Listing 11.8c Define a simple primary key for the sample-database table publishers by using a named table constraint. CREATE TABLE publishers ( pub_id CHAR(3) NOT NULL, pub_name VARCHAR(20) NOT NULL, city VARCHAR(15) NOT NULL, state CHAR(2) , country VARCHAR(15) NOT NULL, CONSTRAINT publishers_pk PRIMARY KEY (pub_id) ); Listing To specify a composite primary key: ◆ Add the following table constraint to a CREATE TABLE definition: [CONSTRAINT constraint_name] PRIMARY KEY (key_columns) key_columns is a list of comma-separated names of the primary-key columns. No more than one PRIMARY KEY constraint is allowed in a table. For the general syntax of CREATE TABLE , see “Creating Tables” earlier in this chapter. The CONSTRAINT clause is optional, and constraint_name is the name of the primary-key constraint; see “Understand- ing Constraints” earlier in this chapter. Listing 11.9 defines a composite primary key for the sample-database table title_authors . The primary-key columns are title_id and au_id , and the key is named title_authors_pk . ✔ Tips ■ To see the result of a CREATE TABLE statement, examine the table’s structure by using one of the commands described in “Displaying Table Definitions” in Chapter 10. ■ To define a column that contains unique values but isn’t a primary key, see “Forcing Unique Values with UNIQUE ” later in this chapter. ■ It’s illegal to specify two or more PRIMARY KEY column constraints in the same table. You can’t use the following statement, for example, to specify the composite key for title_authors : CREATE TABLE title_authors( title_id CHAR(3) PRIMARY KEY, au_id CHAR(3) PRIMARY KEY, au_order SMALLINT NOT NULL, ); Illegal ■ DB2 makes you set the nullability constraint to NOT NULL explicitly for PRIMARY KEY columns; see “Forbidding Nulls with NOT NULL ” earlier in this chapter. To run Listing 11.8a, add NOT NULL to pub_id ’s column constraint. Oracle treats an empty string ( ‘’ ) as null; see the DBMS Tip in “Nulls” in Chapter 3. 352 Chapter 11 Specifying a Primary Key with PRIMARY KEY Listing 11.9 Define a composite primary key for the sample-database table title_authors by using a named table constraint. CREATE TABLE title_authors ( title_id CHAR(3) NOT NULL, au_id CHAR(3) NOT NULL, au_order SMALLINT NOT NULL, royalty_share DECIMAL(5,2) NOT NULL, CONSTRAINT title_authors_pk PRIMARY KEY (title_id, au_id) ); Listing Specifying a Foreign Key with FOREIGN KEY I described foreign keys in “Foreign Keys” in Chapter 2, but I’ll review the basics here: ◆ A foreign key is a mechanism that associates two tables. ◆ A foreign key is a column (or set of columns) in a table whose values relate to, or reference, values in some other table. ◆ A foreign key ensures that rows in one table have corresponding rows in another table, called the referenced table or parent table. ◆ A foreign key establishes a direct relation- ship to a primary key or candidate key in the referenced table, so foreign-key values are restricted to parent-key values that already exist. This restriction is called referential integrity. ◆ A foreign key, unlike a primary key, can allow nulls. ◆ A table can have zero or more for- eign keys. ◆ Foreign-key values generally aren’t unique in their own table. ◆ Foreign-key columns in different tables can reference the same column in a parent table. ◆ A one-column key is a simple key; a multiple-column key is a composite key. When you’re defining a foreign-key constraint, some important considerations are: ◆ A simple key can be a column con- straint or a table constraint; a composite key always is a table constraint. See “Understanding Constraints” earlier in this chapter. ◆ You define a foreign-key constraint by using the keywords FOREIGN KEY or REFERENCES in a CREATE TABLE definition. ◆ A foreign key and its parent key can have different column names. ◆ The foreign key’s data type must have the same data type or must be convertible implicitly to the same type as its parent key; see “Converting Data Types with CAST() ” in Chapter 5. ◆ A FOREIGN KEY column doesn’t have to reference only a PRIMARY KEY column in another table; it also can reference a UNIQUE column in another table. See “Forcing Unique Values with UNIQUE ” later in this chapter. ◆ A table can have any number of foreign- key constraints (or none at all). ◆ In practice, foreign-key constraints almost always are named explicitly. Use a CONSTRAINT clause to name a con- straint; see “Understanding Constraints” earlier in this chapter. ◆ Foreign-key constraints simplify updates and deletions and make it difficult to introduce inconsistencies into a database, but the topology of relations in even a medium-size database can become astonishingly complex. Poor design can lead to time-consuming routine queries, circular rules, tricky backup-and-restore operations, and psychotically ambitious cascading deletes. 353 Creating, Altering, and Dropping Tables Specifying a Foreign Key with FOREIGN KEY To preserve referential integrity, your DBMS won’t let you create orphan rows or make existing rows orphans (rows in a foreign-key table without an associated row in a parent table). When you INSERT , UPDATE , or DELETE a row with a FOREIGN KEY column that refer- ences a PRIMARY KEY column in a parent table, your DBMS performs the following referential-integrity checks: Inserting a row into the foreign-key table. The DBMS checks that the new FOREIGN KEY value matches a PRIMARY KEY value in the parent table. If no match exists, the DBMS won’t INSERT the row. Updating a row in the foreign-key table. The DBMS checks that the updated FOREIGN KEY value matches a PRIMARY KEY value in the parent table. If no match exists, the DBMS won’t UPDATE the row. Deleting a row in the foreign-key table. Areferential-integrity check is unnecessary. Inserting a row into the parent table. Areferential-integrity check is unnecessary. Updating a row in the parent table. The DBMS checks that none of the FOREIGN KEY values matches the PRIMARY KEY value to be updated. If a match exists, the DBMS won’t UPDATE the row. Deleting a row from the parent table. The DBMS checks that none of the FOREIGN KEY values matches the PRIMARY KEY value to be deleted. If a match exists, the DBMS won’t DELETE the row. 354 Chapter 11 Specifying a Foreign Key with FOREIGN KEY The DBMS skips the referential-integrity check for rows with a null in the FOREIGN KEY column. To specify a simple foreign key: ◆ To specify a simple foreign key as a col- umn constraint, add the following column constraint to a CREATE TABLE column definition: [CONSTRAINT constraint_name] REFERENCES ref_table(ref_column) or To specify a simple foreign key as a table constraint, add the following table con- straint to a CREATE TABLE definition: [CONSTRAINT constraint_name] FOREIGN KEY (key_column) REFERENCES ref_table(ref_column) key_column is the name of the foreign- key column. ref_table is the name of the parent table referenced by the FOREIGN KEY constraint. ref_column is the name of the column in ref_table that is the ref- erenced key. Zero or more FOREIGN KEY constraints are allowed in a table. For the general syntax of CREATE TABLE , see “Creating Tables” earlier in this chapter. The CONSTRAINT clause is optional, and constraint_name is the name of the for- eign-key constraint; see “Understanding Constraints” earlier in this chapter. Listing 11.10 uses a column constraint to designate a foreign-key column in the table titles . This syntax shows the easiest way to create a simple foreign key. After you run this statement, the DBMS will ensure that values inserted into the column pub_id in titles already exist in the column pub_id in publishers . Note that nulls aren’t allowed in the foreign-key column, so every book must have a publisher. 355 Creating, Altering, and Dropping Tables Specifying a Foreign Key with FOREIGN KEY Listing 11.10 Define a simple foreign key for the sample-database table titles by using a column constraint. CREATE TABLE titles ( title_id CHAR(3) NOT NULL PRIMARY KEY , title_name VARCHAR(40) NOT NULL, type VARCHAR(10) , pub_id CHAR(3) NOT NULL REFERENCES publishers(pub_id) , pages INTEGER , price DECIMAL(5,2) , sales INTEGER , pubdate DATE , contract SMALLINT NOT NULL ); Listing The table royalties has a one-to-one relation- ship with the table titles , so Listing 11.11 defines the column title_id to be both the primary key and a foreign key that points to title_id in titles . For information about relationships, see “Relationships” in Chapter 2. Listing 11.12 uses named table constraints to create two foreign keys. This syntax shows the preferred way to add foreign keys; you can use the names if you decide to change or delete the keys later. (See “Altering a Table with ALTER TABLE ” later in this chapter.) Each foreign-key column is an individual key and not part of a single composite key. Note that foreign keys together, however, comprise the table’s composite primary key. To specify a composite foreign key: ◆ Add the following table constraint to a CREATE TABLE definition: [CONSTRAINT constraint_name] FOREIGN KEY (key_columns) REFERENCES ref_table(ref_columns) key_columns is a list of comma-separated names of the foreign-key columns. ref_table is the name of the parent table referenced by the FOREIGN KEY constraint. ref_columns is a list of comma-separated names of the columns in ref_table that are the referenced keys. key_columns and ref_columns must have the same number of columns, listed in corresponding order. Zero or more FOREIGN KEY constraints are allowed in a table. For the general syntax of CREATE TABLE , see “Creating Tables” earlier in this chapter. The CONSTRAINT clause is optional, and constraint_name is the name of the for- eign-key constraint; see “Understanding Constraints” earlier in this chapter. 356 Chapter 11 Specifying a Foreign Key with FOREIGN KEY Listing 11.11 Define a simple foreign key for the sample-database table royalties by using a named table constraint. CREATE TABLE royalties ( title_id CHAR(3) NOT NULL, advance DECIMAL(9,2) , royalty_rate DECIMAL(5,2) , CONSTRAINT royalties_pk PRIMARY KEY (title_id), CONSTRAINT royalties_title_id_fk FOREIGN KEY (title_id) REFERENCES titles(title_id) ); Listing Listing 11.12 Define simple foreign keys for the sample-database table title_authors by using named table constraints. CREATE TABLE title_authors ( title_id CHAR(3) NOT NULL, au_id CHAR(3) NOT NULL, au_order SMALLINT NOT NULL, royalty_share DECIMAL(5,2) NOT NULL, CONSTRAINT title_authors_pk PRIMARY KEY (title_id, au_id), CONSTRAINT title_authors_fk1 FOREIGN KEY (title_id) REFERENCES titles(title_id), CONSTRAINT title_authors_fk2 FOREIGN KEY (au_id) REFERENCES authors(au_id) ); Listing The sample database contains no composite foreign keys, but suppose that I create a table named out_of_print to store informa- tion about each author’s out-of-print books. The table title_authors has a composite primary key. This constraint shows how to reference this key from the table out_of_print : CONSTRAINT out_of_print_fk FOREIGN KEY (title_id, au_id) REFERENCES title_authors(title_id, au_id) ✔ Tips ■ To see the result of a CREATE TABLE statement, examine the table’s structure by using one of the commands described in “Displaying Table Definitions” in Chapter 10. ■ You can omit the (ref_column) or (ref_columns) expression in the REFERENCES clause if the referenced col- umn(s) is the primary key of ref_table. ■ A FOREIGN KEY constraint can reference another column in the same table (a self- reference). Recall from “Creating a Self-Join” in Chapter 7 that the table employees is self-referencing. (I created employees for illustrative purposes; it’s not part of the sample database.) employees has three columns: emp_id , emp_name , and boss_id . emp_id is a primary key that uniquely identifies an employee, and boss_id is an employee ID that identifies the employee’s manager. Each manager also is an employee, so to ensure that each manager ID that is added to the table matches an existing employee ID, boss_id is defined as a foreign key of emp_id : CREATE TABLE employees ( emp_id CHAR(3) NOT NULL, emp_name CHAR(20) NOT NULL, boss_id CHAR(3) NULL, CONSTRAINT employees_pk PRIMARY KEY (emp_id), CONSTRAINT employees_fk FOREIGN KEY (boss_id) REFERENCES employees(emp_id) ); ■ SQL lets you define the action the DBMS takes when you try to UPDATE or DELETE a key value (in a parent table) to which foreign-key values point. To trigger a referential action, specify an ON UPDATE or ON DELETE clause in the FOREIGN KEY con- straint. Support for these clauses varies by DBMS; search your DBMS documen- tation for foreign key or referential integrity. The next two Tips explain the SQL standard’s definition of these clauses. continues on next page 357 Creating, Altering, and Dropping Tables Specifying a Foreign Key with FOREIGN KEY ■ The ON UPDATE action clause specifies what the DBMS does if you attempt to UPDATE a key value in a row (in a parent table) where the key value is referenced by foreign keys in rows in other tables. action takes one of four values: CASCADE updates the dependent foreign- key values to the new parent-key value. SET NULL sets the dependent foreign-key values to nulls. SET DEFAULT sets the dependent foreign- key values to their default values; see “Specifying a Default Value with DEFAULT ” earlier in this chapter. NO ACTION generates an error on a foreign- key violation. This action is the default. ■ The ON DELETE action clause specifies what the DBMS does if you attempt to DELETE a key value in a row (in a parent table) where the key value is referenced by foreign keys in rows in other tables. action takes one of four values: CASCADE deletes the rows that contain foreign-key values that match the deleted parent-key value. SET NULL sets the dependent foreign-key values to null. SET DEFAULT sets the dependent foreign- key values to their default values; see “Specifying a Default Value with DEFAULT ” earlier in this chapter. NO ACTION generates an error on a foreign- key violation. This action is the default. ■ Microsoft SQL Server doesn’t support the data type DATE . To run Listing 11.10, change the data type of the column pubdate to DATETIME . Oracle treats an empty string ( ‘’ ) as null; see the DBMS Tip in “Nulls” in Chapter 3. MySQL enforces foreign-key constraints through InnoDB tables; search MySQL documentation for foreign key. InnoDB FOREIGN KEY syntax is more restrictive than standard CREATE TABLE syntax. 358 Chapter 11 Specifying a Foreign Key with FOREIGN KEY Forcing Unique Values with UNIQUE A unique constraint ensures that a column (or set of columns) contains no duplicate values. A unique constraint is similar to a primary-key constraint, except that a unique column can contain nulls and a table can have multiple unique columns. (For infor- mation about primary-key constraints, see “Specifying a Primary Key with PRIMARY KEY ” earlier in this chapter.) Suppose that I add the column isbn to the table titles to hold a book’s ISBN. An ISBN is a unique, standardized identification number that marks a book unmistakably. titles already has a primary key ( title_id ), so to ensure that each ISBN value is unique, I can define a unique constraint on the column isbn . When you’re defining a unique constraint, some important considerations are: ◆ A one-column key is a simple constraint; a multiple-column key is a composite constraint. ◆ In a composite constraint, values can be duplicated within one column, but each combination of values from all the columns must be unique. ◆ A simple unique constraint can be a column constraint or a table constraint; a composite unique constraint always is a table constraint. See “Understanding Constraints” earlier in this chapter. ◆ You define a unique constraint by using the keyword UNIQUE in a CREATE TABLE definition. ◆ As a table constraint, UNIQUE makes you specify column name(s). As a column constraint, UNIQUE applies to the column in which it’s defined. ◆ A table can have zero or more unique constraints. ◆ In practice, unique constraints almost always are named explicitly. Use a CONSTRAINT clause to name a constraint; see “Understanding Constraints” earlier in this chapter. ◆ A UNIQUE column can forbid nulls; see “Forbidding Nulls with NOT NULL ” earlier in this chapter. 359 Creating, Altering, and Dropping Tables Forcing Unique Values with UNIQUE . string ( ‘’ ) as null; see the DBMS Tip in “Nulls” in Chapter 3. MySQL enforces foreign-key constraints through InnoDB tables; search MySQL documentation for foreign key. InnoDB FOREIGN KEY syntax. employees_pk PRIMARY KEY (emp_id), CONSTRAINT employees_fk FOREIGN KEY (boss_id) REFERENCES employees(emp_id) ); ■ SQL lets you define the action the DBMS takes when you try to UPDATE or DELETE a key value (in. your DBMS documen- tation for foreign key or referential integrity. The next two Tips explain the SQL standard’s definition of these clauses. continues on next page 357 Creating, Altering, and Dropping