DBMS Lab 18 SQL Constraints Combine
DBMS Lab 18 SQL Constraints Combine
LAB No. 18
SQL CONSTRAINTS
(Data Definition Language Constraints)
SQL Constraints
➢ Constraints are rules that are defined within a relational database to ensure the data
integrity and consistency of the data stored in the database.
➢ They act as limitations or restrictions on the data that can be entered, modified, or
deleted in a table.
➢ Constraints play a crucial role in maintaining the accuracy and reliability of the data
within a database system.
1) NOT NULL
2) UNIQUE
3) PRIMARY KEY
4) FOREIGN KEY
NOT NULL:
Definition:
The NOT NULL constraint is used to ensure that a column in a relational database table cannot
contain any NULL (missing or undefined) values.
Usage:
When defining a column in a table, you can add the NOT NULL constraint to specify that
values in that column must be provided during insertion, and they cannot be left blank.
CREATE TABLE example (column1 INT NOT NULL, column2 VARCHAR (50));
Error Handling:
If an attempt is made to insert a row without providing a value for a NOT NULL column, or if
an update sets the column to NULL, the database will generate an error.
Database Management System LAB # 18 Department of Technology
The University of Lahore
-- Attempt to insert a row without a value for column1 (will result in an error)
INSERT INTO example (column2) VALUES ('Value');
CREATE TABLE users (user_id INT, username VARCHAR (50) NOT NULL,
email VARCHAR (100) NOT NULL);
Dropping NOT NULL Constraints:
Dropping the NOT NULL constraint from a column involves using the ALTER TABLE
statement. Here's the basic syntax to drop the NOT NULL constraint:
Syntax:
The unique key constraint is specified when creating a table or altering an existing table.
-- Creating a table with a unique key constraint
CREATE TABLE example (id INT, email VARCHAR (100) UNIQUE,
name VARCHAR (50));
Single and Composite Unique Keys:
A unique key can be applied to a single column or a combination of columns.
-- Single-column unique key
CREATE TABLE example (id INT, username VARCHAR (50) UNIQUE, age INT);
Database Management System LAB # 18 Department of Technology
The University of Lahore
-- Composite unique key
CREATE TABLE example (id INT PRIMARY KEY, email VARCHAR (100),
phone_number VARCHAR (15), UNIQUE (email, phone_number));
Handling NULL Values:
By default, unique constraints allow multiple NULL values in the column(s). This means that
NULL is not considered a duplicate value.
Syntax:
When creating a table, you define a primary key using the PRIMARY KEY constraint.
CREATE TABLE example (id INT PRIMARY KEY, name VARCHAR (50));
FOREIGN KEY:
Definition:
A foreign key is a constraint that establishes a link between two tables in a relational database.
It ensures that values in a column (or a set of columns) of one table match the values in the
primary key column(s) of another table.
Syntax:
When creating a table, you define a foreign key using the FOREIGN KEY constraint.
Insert Records:
2) Insert three records into the employees table, ensuring that each record has a value for
employee_id and employee_name.
Update Records:
3) Update the employees table to set the employee_name to 'Unknown' where the
employee_name is currently NULL.
Conditional Insert:
5) Create a table named students with columns student_id and student_name. Add a NOT
NULL constraint on student_id and allow student_name to be NULL. Insert a record
with a value for student_id but without providing a value for student_name and write
your reviews.
Multiple Constraints:
6) Create a table named inventory with columns product_id, product_name, and quantity.
Add a NOT NULL constraint on both product_id and product_name. Insert a record
with a value for product_id but without providing a value for product_name and write
your review.
Insert Records:
8) Insert three records into the employees table, ensuring that each record has a unique
email value.
Database Management System LAB # 18 Department of Technology
The University of Lahore
Update Unique Value:
9) Update the email of the employee with employee_id 1 to a new unique value.