0% found this document useful (0 votes)
12 views3 pages

Exp 8

Exp 8

Uploaded by

guptasankalp387
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)
12 views3 pages

Exp 8

Exp 8

Uploaded by

guptasankalp387
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/ 3

Database Management System (3230324)

Experiment Number − 8

1 Objective:
Study and Implementation of different types of constraints.

2 Theory:
Constraints are rules enforced on data columns in a table. They are crucial for ensuring the accuracy
and reliability of the data. The main types of constraints in SQL are:

1. Primary Key: Uniquely identifies each record in a table. A primary key must contain unique
values and cannot contain NULL values.

2. Foreign Key: A field (or collection of fields) in one table that refers to the primary key in
another table. It establishes a relationship between the two tables.

3. Unique: Ensures that all values in a column are different from one another, allowing NULL
values unless otherwise specified.

4. Check: Ensures that all values in a column satisfy a specific condition.

5. Not Null: Ensures that a column cannot have a NULL value, enforcing data entry in that field.

3 Lab Activities:
Prerequisite: Access to a SQL database management system such as MySQL, Oracle, or SQL Server.

Problem: To demonstrate the use of different constraints, the following tasks were performed:

• Create a database to hold student and course information.

• Implement the appropriate constraints on the tables to ensure data integrity.

• Attempt to insert invalid data to test the effectiveness of the constraints.

3.1 Activity - 1: Create a Sample Database, and Table with Constraints

CREATE DATABASE SchoolDB;


USE SchoolDB;
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100) NOT NULL,
Age INT CHECK (Age >= 18),
Email VARCHAR(100) UNIQUE
);

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
StudentName VARCHAR(100) NOT NULL,
Age INT CHECK (Age >= 18),
Email VARCHAR(100) UNIQUE
);

CREATE TABLE Courses (


CourseID INT PRIMARY KEY,
CourseName VARCHAR(100) NOT NULL,
Credits INT CHECK (Credits > 0)
);

CREATE TABLE Enrollment (


EnrollmentID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
EnrollmentDate DATE NOT NULL,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID),
UNIQUE (StudentID, CourseID)
);

3.2 Activity - 2: Insert Data

-- Valid Insertions
INSERT INTO Students (StudentID, StudentName, Age, Email)
VALUES (1, 'Alice', 20, '[email protected]');
INSERT INTO Students (StudentID, StudentName, Age, Email)
VALUES (2, 'Bob', 22, '[email protected]');

INSERT INTO Courses (CourseID, CourseName, Credits)


VALUES (101, 'Mathematics', 3);
INSERT INTO Courses (CourseID, CourseName, Credits)
VALUES (102, 'Physics', 4);

INSERT INTO Enrollment (EnrollmentID, StudentID, CourseID, EnrollmentDate)


VALUES (1, 1, 101, '2024-10-01');

Page 2
3.3 Activity - 3: Attempt Invalid Insertions

-- Attempt to insert a duplicate email


INSERT INTO Students (StudentID, StudentName, Age, Email)
VALUES (3, 'Charlie', 19, '[email protected]');

-- Attempt to insert a student with age < 18


INSERT INTO Students (StudentID, StudentName, Age, Email)
VALUES (4, 'David', 16, '[email protected]');

-- Attempt to insert a course with negative credits


INSERT INTO Courses (CourseID, CourseName, Credits)
VALUES (103, 'Chemistry', -3);

4 Conclusion
The experiment successfully demonstrated the implementation and enforcement of various SQL con-
straints. The constraints ensured data integrity by preventing invalid entries and maintaining rela-
tionships between tables. The attempts to insert invalid data highlighted the effectiveness of each
constraint, confirming that:

• Primary Key constraints prevented duplicate records.

• Unique constraints ensured no two entries could have the same email.

• Check constraints validated data based on specified conditions.

• Not Null constraints enforced mandatory data entry.

Page 3

You might also like