Database Systems Lab 3 Enhanced Presentation
Database Systems Lab 3 Enhanced Presentation
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.