1. Trang chủ
  2. » Công Nghệ Thông Tin

SQL VISUAL QUICKSTART GUIDE- P38 pdf

10 224 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 183,58 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 TABLEcolumn definition: [CONSTRAINT constraint_

Trang 1

Specifying a 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 KEYin a

CREATE TABLEdefinition

◆ As a table constraint, PRIMARY KEYmakes

you specify column name(s) explicitly As

a column constraint, PRIMARY KEYapplies

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 aCONSTRAINTclause to do so; see

“Understanding Constraints” earlier in this chapter

◆ The nullability of all PRIMARY KEYcolumns 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 INSERTa 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)

Trang 2

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 TABLEcolumn 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 TABLEdefinition:

[CONSTRAINT constraint_name]

PRIMARY KEY (key_column) key_column is the name of the

primary-key column No more than one PRIMARY KEYconstraint is allowed in a table For the general syntax of CREATE TABLE, see

“Creating Tables” earlier in this chapter

TheCONSTRAINTclause 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 NULLcolumn 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_pkif you decide

to change or delete the key later See “Altering

a Table with ALTER TABLE” later in this chapter

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

Trang 3

To specify a composite primary key:

◆ Add the following table constraint to a

CREATE TABLEdefinition:

[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 KEYconstraint is

allowed in a table For the general syntax

ofCREATE TABLE, see “Creating Tables”

earlier in this chapter

TheCONSTRAINTclause 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_idandau_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 KEYcolumn 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 NULLexplicitly for PRIMARY KEY

columns; see “Forbidding Nulls with

NOT NULL” earlier in this chapter To run Listing 11.8a, add NOT NULLto

pub_id’s column constraint

Oracle treats an empty string (‘’) as null; see the DBMS Tip in “Nulls” in Chapter 3

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

Trang 4

Specifying a 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 concon-straint; 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 KEYor

REFERENCESin a CREATE TABLEdefinition

◆ 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

◆ AFOREIGN KEYcolumn doesn’t have to reference only a PRIMARY KEYcolumn in another table; it also can reference a

UNIQUEcolumn 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 aCONSTRAINTclause 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

Trang 5

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 KEYcolumn that refer-ences a PRIMARY KEYcolumn 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 KEYvalue matches a PRIMARY KEY

value in the parent table If no match exists, the DBMS won’t INSERTthe row

Updating a row in the foreign-key table.

The DBMS checks that the updated FOREIGN KEYvalue matches a PRIMARY KEYvalue in the parent table If no match exists, the DBMS won’t UPDATEthe row

Deleting a row in the foreign-key table.

A referential-integrity check is unnecessary

Inserting a row into the parent table.

A referential-integrity check is unnecessary

Updating a row in the parent table The

DBMS checks that none of the FOREIGN KEY

values matches the PRIMARY KEYvalue to be updated If a match exists, the DBMS won’t

UPDATEthe row

Deleting a row from the parent table.

The DBMS checks that none of the FOREIGN KEYvalues matches the PRIMARY KEYvalue

to be deleted If a match exists, the DBMS won’t DELETEthe row

Trang 6

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 colcol-umn constraint to a CREATE TABLEcolumn 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 TABLEdefinition:

[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 KEYconstraint 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

TheCONSTRAINTclause 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_idin

titlesalready exist in the column pub_id

inpublishers Note that nulls aren’t allowed

in the foreign-key column, so every book must have a publisher

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

Trang 7

The table royaltieshas a one-to-one

relation-ship with the table titles, so Listing 11.11

defines the column title_idto be both the

primary key and a foreign key that points to

title_idintitles 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 TABLEdefinition:

[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 KEYconstraint

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 KEYconstraints are

allowed in a table For the general syntax

ofCREATE TABLE, see “Creating Tables”

earlier in this chapter

TheCONSTRAINTclause is optional, and

constraint_name is the name of the

for-eign-key constraint; see “Understanding

Constraints” earlier in this chapter

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

Trang 8

The sample database contains no composite

foreign keys, but suppose that I create a

table named out_of_printto store

informa-tion about each author’s out-of-print books

The table title_authorshas 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

REFERENCESclause if the referenced

col-umn(s) is the primary key of ref_table.

■ AFOREIGN KEYconstraint can reference

another column in the same table (a

self-reference) Recall from “Creating a Self-Join”

in Chapter 7 that the table employeesis

self-referencing (I created employeesfor

illustrative purposes; it’s not part of the

sample database.)

employeeshas three columns: emp_id,

emp_name, and boss_id.emp_idis a primary key that uniquely identifies an employee, andboss_idis 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_idis defined as a foreign key

ofemp_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 UPDATEorDELETE

a key value (in a parent table) to which foreign-key values point To trigger a referential action, specify an ON UPDATEor

ON DELETEclause 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

Trang 9

■ TheON UPDATE actionclause specifies

what the DBMS does if you attempt to

UPDATEa 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:

CASCADEupdates the dependent

foreign-key values to the new parent-foreign-key value

SET NULLsets the dependent foreign-key

values to nulls

SET DEFAULTsets the dependent

foreign-key values to their default values; see

“Specifying a Default Value with DEFAULT”

earlier in this chapter

NO ACTIONgenerates an error on a

foreign-key violation This action is the default

■ TheON DELETE actionclause specifies

what the DBMS does if you attempt to

DELETEa 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:

CASCADEdeletes the rows that contain

foreign-key values that match the deleted

parent-key value

SET NULLsets the dependent foreign-key

values to null

SET DEFAULTsets the dependent

foreign-key values to their default values; see

“Specifying a Default Value with DEFAULT”

earlier in this chapter

NO ACTIONgenerates 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 pubdatetoDATETIME

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 KEYsyntax is more restrictive than standard CREATE TABLEsyntax

Trang 10

Forcing Unique Values

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 isbnto the

table titlesto hold a book’s ISBN An ISBN

is a unique, standardized identification

number that marks a book unmistakably

titlesalready has a primary key (title_id),

so to ensure that each ISBN value is unique,

I can define a unique constraint on the

columnisbn

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 UNIQUEin a CREATE TABLE

definition

◆ As a table constraint, UNIQUEmakes you specify column name(s) As a column constraint, UNIQUEapplies 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

CONSTRAINTclause to name a constraint; see “Understanding Constraints” earlier

in this chapter

◆ AUNIQUEcolumn can forbid nulls; see

“Forbidding Nulls with NOT NULL” earlier

in this chapter

Ngày đăng: 05/07/2014, 05:20

TỪ KHÓA LIÊN QUAN