Lab 5 DB
Lab 5 DB
SQL Constraints
NOT NULL
Primary Key
Unique
Default
FOREIGN KEY
CHECK
INDEX
1. Check Constraint
Defines a condition that must be satisfied for the data in a column.
It ensures that values inserted or updated meet a specified condition.
Syntax
CREATE TABLE age_restriction (
person_id INT,
age INT CHECK (age >= 18)
);
3. INDEX Constraint
In SQL, the CREATE INDEX statement is used to create an index on one or more
columns of a table, which helps improve the performance of queries that involve
those columns.
Syntax
CREATE INDEX index_name
ON table_name (column1, column2,…);
Students Table:
Attributes:
student_id (Primary Key, Integer): Unique identifier for
each student.
first_name (String): First name of the student.
last_name (String): Last name of the student.
date_of_birth (Date): Date of birth of the student, Cannot
be a present date
Constraints:
student_id should be a Primary Key and cannot be NULL.
first_name and last_name cannot be NULL.
Courses Table:
Attributes:
course_id (Primary Key, Integer): Unique identifier for each
course.
course_name (String): Name of the course.
credits (Integer): Number of credits for the course. Ensuring
credits is a positive value
Constraints:
course_id should be a Primary Key and cannot be NULL.
course_name cannot be NULL.
Enrollments Table:
Attributes:
enrollment_id (Primary Key, Integer): Unique identifier for
each enrollment.
student_id (Foreign Key, Integer): References student_id in
the Students table.
course_id (Foreign Key, Integer): References course_id in
the Courses table.
enrollment_date (Date): Date when the student enrolled in
the course.
Constraints:
enrollment_id should be a Primary Key and cannot be NULL.
student_id and course_id should be Foreign Keys referencing
the respective tables.
enrollment_date cannot be NULL.
Each combination of student_id and course_id should be
unique to prevent duplicate enrollments.