How To Use The Constraint Clause To Create Primary Key and Foreign Key in MS Access
How To Use The Constraint Clause To Create Primary Key and Foreign Key in MS Access
•Introduction to Constraints
•Primary Key Constraint
•Creating Primary Key in MS Access
•Shortcut for Declaring Primary Key
•Composite Primary Key
•Foreign Key Constraint
•Examples of Foreign Key Relationships
•Additional Constraints
Constraints
Introduction to Constraints
Types of Constraints
Open Query Design, select SQL View, and Use the statement: CREATE TABLE
enter the SQL statement for creating a employees (employeeID INT PRIMARY KEY);
table with a primary key. to create a table with a primary key.
Primary Key
Pros Cons
• Define the composite primary key in the CREATE TABLE statement after field
definitions.
• Use parentheses to enclose the columns that make up the composite key,
separated by commas.
• Run this SQL statement in the SQL View of the Query Design to create the
table.
Database Design
CREATE TABLE Orders ( OrderID INT CREATE TABLE Address ( CustomerID INT
PRIMARY KEY, CustomerID INT, FOREIGN PRIMARY KEY, AddressLine1
KEY (CustomerID) REFERENCES VARCHAR(255), City VARCHAR(100),
Customers(CustomerID) ); State VARCHAR(100), ZipCode
VARCHAR(20), FOREIGN KEY
(CustomerID) REFERENCES
Constraints
Additional Constraints
The NOT NULL constraint ensures that a column cannot accept null values,
enforcing that every row must contain a value for this column.
UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different from
each other, preventing duplicate entries in that column.
To add constraints after table creation, use the ALTER TABLE statement followed
by ADD CONSTRAINT. For example: ALTER TABLE students ADD CONSTRAINT
unique_name UNIQUE (first_name, last_name);.
Thank you.