0% found this document useful (0 votes)
19 views7 pages

Lab 5 DB

The document outlines SQL constraints including NOT NULL, Primary Key, Unique, Default, FOREIGN KEY, CHECK, and INDEX, with detailed explanations and syntax for each. It also provides examples of creating tables with CHECK and FOREIGN KEY constraints, as well as how to manage indexes. Additionally, it presents a lab task for designing a database schema for a student management system with specific requirements for Students, Courses, and Enrollments tables.

Uploaded by

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

Lab 5 DB

The document outlines SQL constraints including NOT NULL, Primary Key, Unique, Default, FOREIGN KEY, CHECK, and INDEX, with detailed explanations and syntax for each. It also provides examples of creating tables with CHECK and FOREIGN KEY constraints, as well as how to manage indexes. Additionally, it presents a lab task for designing a database schema for a student management system with specific requirements for Students, Courses, and Enrollments tables.

Uploaded by

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

Database Systems (Lab-5)

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)
);

2. Foreign Key Constraint


 Establishes a link between data in two tables, enforcing referential integrity.
 The foreign key column(s) in one table must match the primary key column(s) in another
table.
Syntax
CREATE TABLE orders (
order_id INT PRIMARY KEY,
product_id INT,
FOREIGN KEY (product_id) REFERENCES
products(product_id)
);

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,…);

 CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...);

SQL CHECK on CREATE TABLE


 SQL Server CHECK Constraint
CREATE TABLE Staff (
staffNo int NOT NULL,
fName varchar(255),
lName varchar(255),
position varchar(255),
DOB date,
salary money CHECK (salary>=17000)
);
 MySQL CHECK Constraint
CREATE TABLE Staff (
staffNo int NOT NULL,
fName varchar(255),
lName varchar(255),
position varchar(255),
DOB date,
salary money,
CHECK (salary>=17000)
);
 CHECK constraint on multiple columns
CREATE TABLE Staff (
staffNo int NOT NULL UNIQUE,
fName varchar(255),
lName varchar(255),
position varchar(255),
gender char ,
DOB date,
salary money,
CONSTRAINT CHK_Staff CHECK (salary>=17000
AND position=‘Supervisor')
);
 SQL CHECK on ALTER TABLE
For Single Column
ALTER TABLE staff
ADD CHECK (salary>=17000);

ALTER TABLE staff ADD CHECK (position='supervisor' or


position='Manager');
For Multiple Column
ALTER TABLE staff
ADD CONSTRAINT CHK_staff CHECK (salary>=17000
AND position=’Supervisor');
 DROP a CHECK Constraint
SQL
ALTER TABLE Staff
DROP CONSTRAINT CHK_staff;
MySQL
ALTER TABLE Staff
DROP CHECK CHK_staff;

SQL CREATE INDEX Statement Example


CREATE INDEX idx_lname
ON staff (lName);
CREATE INDEX idx_sname
ON staff (lName, fName);
 DROP INDEX Statement
SQL Server:
DROP INDEX staff. idx_sname;
MySQL:
ALTER TABLE staff
DROP INDEX idx_sname;

Foreign Key Concept

 CREATE TABLE Branch (


branchNo varchar(10) NOT NULL PRIMARY KEY,
street varchar(255),
city varchar(255),
postCode varchar(255),
);
 CREATE TABLE Staff (
staffNo int NOT NULL PRIMARY KEY,
fName varchar(255),
lName varchar(255),
position varchar(255),
sex char ,
DOB date,
salary money,
branchNo varchar(10) ,
CONSTRAINT FK_BranchStaff FOREIGN KEY (branchNo)
REFERENCES Branch(branchNo)
);
 SQL FOREIGN KEY on ALTER TABLE
ALTER TABLE Staff
ADD FOREIGN KEY (branchNo) REFERENCES Branch(branchNo)
;
 Add Foreign key Constraint with Constraint Name
ALTER TABLE Staff
ADD CONSTRAINT FK_BranchStaff
FOREIGN KEY (branchNo) REFERENCES Branch(branchNo);
 DROP a FOREIGN KEY Constraint
MS SQL SERVER
ALTER TABLE Staff
DROP CONSTRAINT FK_BranchStaff
MySQL
ALTER TABLE Staff
DROP FOREIGN KEY FK_BranchStaff;

SQL AUTO INCREMENT Field


CREATE TABLE Persons (
Personid int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
INSERT INTO Persons (FirstName, LastName, age)
VALUES ('Lars','Monsen',30),
('Malinda','John',32);
Select Personid,FirstName,LastName,Age from Persons
Lab Task
Scenario:
You are tasked with designing a database schema for a student management system. The system
should store information about students, courses, and their enrollment details. Each student can
enroll in multiple courses, and each course can have multiple students enrolled in it. Additionally,
each student and course record should have specific attributes with appropriate constraints.

Database Schema Requirements:

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.

You might also like