0% found this document useful (0 votes)
29 views

How To Use The Constraint Clause To Create Primary Key and Foreign Key in MS Access

Ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

How To Use The Constraint Clause To Create Primary Key and Foreign Key in MS Access

Ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

July 19, 2024

How to Use the


Constraint Clause
to Create Primary
Key and Foreign
Key in MS Access
Agenda

•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

What are Constraints?

• Constraints are rules enforced on data in database tables.

• They limit the types of data that can be entered in a table.

• Constraints ensure data integrity and accuracy.

Types of Constraints

• Single-column or column-level constraints apply to individual columns.

• Multi-column or table-level constraints apply across multiple columns.

• Both types help maintain consistent and valid data entries.


Constraints

Primary Key Constraint

Definition and Purpose

● A primary key is a unique identifier for each record in a


database table, ensuring that no two rows have the same
value in this field.

● The purpose of a primary key is to enforce entity integrity


by providing a way to uniquely distinguish each record,
which is critical for data accuracy and retrieval.

● In MS Access, a primary key constraint is declared using


the 'CONSTRAINT' clause in SQL statements, specifying
which field(s) will serve as the primary key.
Primary Key

Creating Primary Key in MS Access

Steps to Create a Primary Key Example SQL Statement

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

Shortcut for Declaring


Primary Key

Pros Cons

✓ The shortcut method simplifies ✕ The automatically generated


the process of declaring a constraint name can be difficult to
primary key by eliminating the reference in code, leading to
need for the 'constraint' clause confusion during database
management.
in SQL statements.
✕ This method does not allow for the
✓ It allows for quicker table definition of custom constraints,
creation, which can be which may limit flexibility in
beneficial during initial complex database designs.
database design phases.
✕ Using the shortcut may lead to
✓ The method automatically inconsistent naming conventions
generates a unique constraint within the database, making
name, saving the user from maintenance more challenging.
manually defining one.
Database Design

Composite Primary Key

Creating a Composite Primary Key

• A composite primary key is formed by combining two or more columns.

• 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.

Example SQL Statement

• SQL: CREATE TABLE Employees (EmployeeID INT, LastName VARCHAR(255),


PRIMARY KEY (EmployeeID, LastName));

• This creates a composite primary key using EmployeeID and LastName.

• Run this SQL statement in the SQL View of the Query Design to create the
table.
Database Design

Foreign Key Constraint

Definition and Purpose of Foreign


Keys

● A foreign key is a field (or collection of fields) in one table that


uniquely identifies a row of another table, creating a link
between the two tables.

● Foreign keys enforce referential integrity by ensuring that the


value in one table matches a value in another, thus
maintaining consistent and valid data relationships.

● In MS Access, a foreign key can be declared using the


'constraint' clause during table creation or modification. This
establishes a relationship between the foreign key in the
referencing table and the primary key in the referenced table.
Foreign Key

Examples of Foreign Key Relationships

Process of Running Queries


One-to-Many Relationship One-to-One Relationship
Each customer can have only one Use SQL View in Access to run the SQL
A customer can have multiple orders, address, creating a one-to-one statements. View the tables in design
establishing a one-to-many relationship. relationship. SQL: CREATE TABLE view to verify relationships and
SQL: CREATE TABLE Customers Customers ( CustomerID INT PRIMARY constraints after creating them.
( CustomerID INT PRIMARY KEY ); KEY );

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

NOT NULL Constraint

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.

Adding Constraints with ALTER TABLE

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.

You might also like