06 Handout 1
06 Handout 1
06 Handout 1
NOT NULL on ALTER TABLE – ensures that a column in o Example: ALTER TABLE Orders ADD FOREIGN KEY
an existing table cannot have a NULL value (CustomerID) REFERENCES Customers (CustomerID);
o Example: ALTER TABLE Students ALTER COLUMN
Section varchar(5) NOT NULL;
UNIQUE on CREATE TABLE – ensures that all values in a
column are different upon creating a table
o Example: CREATE TABLE Students (StudentID
varchar(11) NOT NULL UNIQUE, LastName
varchar(99) NOT NULL, FirstName varchar(99) NOT
NULL, Section varchar(5));
UNIQUE on ALTER TABLE – creates a UNIQUE constraint on
a column of an existing table
o Syntax: ALTER TABLE table_name ADD UNIQUE
(column);
o Example: ALTER TABLE Students ADD UNIQUE
(StudentID);
PRIMARY KEY on CREATE TABLE – uniquely identifies each
row in a table
o Example: CREATE TABLE Students (StudentID
varchar(11) NOT NULL PRIMARY KEY, LastName
varchar(99) NOT NULL, FirstName varchar(99) NOT
NULL, Section varchar(5));
PRIMARY KEY on ALTER TABLE – creates a PRIMARY
KEY
constraint on a column of an existing table
o Syntax: ALTER TABLE table_name ADD PRIMARY KEY
(column);
o Example: ALTER TABLE Students ADD PRIMARY KEY
(StudentID);
FOREIGN KEY on CREATE TABLE – uniquely identifies a
row in another table
o Example: CREATE TABLE Orders (OrderID int NOT
NULL PRIMARY KEY, TableNumber int NOT NULL,
CustomerID int FOREIGN KEY REFERENCES
Customers (CustomerID));
FOREIGN KEY on ALTER TABLE – creates a FOREIGN
KEY
constraint on a column of an existing table
o Syntax: ALTER TABLE table1_name ADD
FOREIGN KEY (table1_column) REFERENCES
table2_name (table2_column);
06 Handout 1
CHECK on CREATE TABLE – ensures that all values
in a column satisfy a specific condition upon
creating a table
References:
o Example: CREATE TABLE Students Coronel, C. and Morris, S. (2017). Database systems: design, implementation, and
(StudentID varchar(11) NOT NULL, management (12th ed.). USA: Cengage Learning.
LastName varchar(99) NOT NULL, Elmasri, R. and Navathe, S. (2016). Fundamentals of database systems (7th ed.). USA:
FirstName varchar(99) NOT NULL, Age int Pearson Higher Education.
Kroenke, D. and Auer, D. (2016). Database processing: fundamentals, design,
CHECK (Age>=15));
and implementation. England: Pearson Education Limited.
CHECK on ALTER TABLE – ensures that all values
in a column of an existing table satisfy a specific
condition
o Syntax: ALTER TABLE table_name ADD
CHECK
(condition);
o Example: ALTER TABLE Students ADD
CHECK (Age>=15);
DEFAULT on CREATE TABLE – sets a default value
for a column when there is no value specified
o Example: CREATE TABLE Students
(StudentID varchar(11) NOT NULL,
LastName varchar(99) NOT NULL,
FirstName varchar(99) NOT NULL, Section
varchar(5) DEFAULT 'Not yet enrolled');
DEFAULT on ALTER TABLE – sets a default value
for a column of an existing table when there is no
value specified
o Syntax: ALTER TABLE table_name ADD
CONSTRAINT
constraint_name DEFAULT 'value' FOR column;
o Example: ALTER TABLE Students ADD
CONSTRAINT df_section DEFAULT 'Not yet
enrolled' FOR Section;
06 Handout 1