Exp 8
Exp 8
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.
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:
-- 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]');
Page 2
3.3 Activity - 3: Attempt Invalid Insertions
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:
• Unique constraints ensured no two entries could have the same email.
Page 3