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

Database Systems Lab 3 Enhanced Presentation

This document discusses SQL constraints and their importance in database design. It covers the NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and INDEX constraints. Examples are provided for each constraint on how to implement them when creating tables. Exercises at the end instruct students to apply the constraints in creating tables to represent real world entities such as students, courses, enrollments, and grades to ensure referential integrity and data validity.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Database Systems Lab 3 Enhanced Presentation

This document discusses SQL constraints and their importance in database design. It covers the NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and INDEX constraints. Examples are provided for each constraint on how to implement them when creating tables. Exercises at the end instruct students to apply the constraints in creating tables to represent real world entities such as students, courses, enrollments, and grades to ensure referential integrity and data validity.

Uploaded by

sp22-bse-097
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lab 3: SQL Constraints and

Database Design
• Objective: Understand various SQL constraints
and their importance in database design.
SQL Constraints Overview
• Constraints are rules enforced on data
columns of a table. They ensure the accuracy
and reliability of data in the database.
NOT NULL Constraint
• Ensures that a column cannot have a NULL
value.
Example: NOT NULL Constraint
• CREATE TABLE Students (
• ID INT NOT NULL,
• Name VARCHAR(50) NOT NULL
• );
Exercise: NOT NULL Constraint
• Create a table 'Courses' where both 'CourseID'
and 'CourseName' columns can't be NULL.
UNIQUE Constraint
• Ensures that all values in a column are unique.
Example: UNIQUE Constraint
• CREATE TABLE Students (
• ID INT UNIQUE,
• Name VARCHAR(50)
• );
Exercise: UNIQUE Constraint
• Create a table 'Courses' where 'CourseID' is a
unique field.
PRIMARY KEY Constraint
• Uniquely identifies each record in a table. A
primary key column cannot contain NULL
values.
Example: PRIMARY KEY Constraint
• CREATE TABLE Students (
• ID INT PRIMARY KEY,
• Name VARCHAR(50)
• );
Exercise: PRIMARY KEY Constraint
• Create a table 'Courses' where 'CourseID' is
the primary key.
FOREIGN KEY Constraint
• Ensures referential integrity in the relation
between two tables.
Example: FOREIGN KEY Constraint
• CREATE TABLE Enrollments (
• StudentID INT,
• CourseID INT,
• FOREIGN KEY (StudentID) REFERENCES
Students(ID),
• FOREIGN KEY (CourseID) REFERENCES
Courses(CourseID)
• );
Exercise: FOREIGN KEY Constraint
• Create a table 'Grades' that references
'Students' based on StudentID and 'Courses'
based on CourseID.
CHECK Constraint
• Ensures that the value in a column meets a
specific condition.
Example: CHECK Constraint
• CREATE TABLE Students (
• ID INT,
• Age INT CHECK (Age >= 18)
• );
Exercise: CHECK Constraint
• Create a table 'Courses' where the 'Credits'
column should have values between 1 and 5.
INDEX
• Used to create and retrieve data from the
database quickly.
Example: INDEX
• CREATE INDEX idx_student_name
• ON Students (Name);
Exercise: INDEX
• Create an index on the 'CourseName' column
of the 'Courses' table.
Constraints in Database Design
• Constraints are essential in preserving the
integrity of data. They prevent accidental
damage to the database and ensure data
consistency.
Feedback and Questions
• Students are encouraged to provide feedback
and ask questions at the end of the lab.

You might also like