SQL Lab Sheet Updated
SQL Lab Sheet Updated
Data Constraints
Data Constraints are a set of rules that must be applied to the data being stored to
ensure its integrity. These are also known as integrity constraints. For instance no
employee in the Sales department can have a salary less than Rs 10,000.
Such limitations have to be enforced on the data and only that data which satisfies the
conditions set will actually be stored. If the data gathered fails to satisfy the
conditions set, is it rejected. Even if a single column in a record being entered into
the table fails a constraint, the entire record is rejected and not stored in the table.
This technique ensures that the data that is stored in the database will be valid, and
thus has integrity.
Both the CREATE TABLE and the ALTER TABLE commands can be used to write
SQL statements that attach constraints to a table column.
Types of Data Constraints
The Primary Key Constraint
A primary key is one or more columns in a table used to uniquely identify each row
in the table. A primary key column in a table has special attributes:
• It defines the column as a mandatory column i.e. the column cannot be left
blank.
• The data held across the column must be unique.
Example:
In the sales_order table, the column, order_no is the primary key and this constraint is
specified in the CREATE TABLE command as follows:
CREATE TABLE sales_order
(order_no VARCHAR(6) PRIMARY KEY,
order_date DATE,
client_no VARCHAR(6),
…
);
A multicolumn primary key is called a Composite Primary Key. When a record
cannot be uniquely identified using the value in a single column, a composite primary
key needs to be defined.
1
Example:
The sales_order_details table will hold multiple records that are sales orders. Each
such sales order will have multiple products that have been ordered. Here we have a
composite primary key consisting of the order_no and product_no.
CREATE TABLE sales_order_details
(order_no VARCHAR(6),
product_no VARCHAR(6),
qty_ordered INTEGER,
qty_disp INTEGER,
product_rate REAL,
PRIMARY KEY (order_no, product_no)
);
The Foreign Key Constraint
Foreign Keys represent relationships between tables. A foreign key is a column
whose values are derived from the primary key of some other table.
The table in which the foreign key is defined is called the referencing or foreign
table and the table that defines the primary key is called the referenced or master
table.
Foreign key properties:
• Rejection of an INSERT or UPDATE of a value, if a corresponding value does
not exist in the master table
• The foreign key columns and the referenced primary key may have different
names, but must have the same data types
• A foreign key column of a table may reference to the primary key of the same
table
There are two foreign keys in the sales_order_details table:
• order_no which references the order_no column in the sales_order table
• product_no which references the product_no column in the product_master
table
The CREATE TABLE command including the foreign key constraints would be:
2
CREATE TABLE sales_order_details
(order_no VARCHAR(6) REFERENCES sales_order(order_no),
product_no VARCHAR(6) REFERENCES
product_master(product_no),
qty_ordered INTEGER,
qty_disp INTEGER,
product_rate REAL,
PRIMARY KEY (order_no, product_no)
);
Note: If the foreign key and the primary key in the referenced table have the same
name, there is no need to specify the column name in brackets.
For example:
order_no VARCHAR(6) REFERENCES sales_order
The foreign key constraints can alternatively be placed at the end of the CREATE
TABLE command:
CREATE TABLE sales_order_details
(order_no VARCHAR(6),
product_no VARCHAR(6),
qty_ordered INTEGER,
qty_disp INTEGER,
product_rate REAL,
PRIMARY KEY (order_no, product_no)
FOREIGN KEY (order_no) REFERENCES sales_order(order_no),
FOREIGN KEY (product_no) REFERENCES
product_master(product_no),
);
The foreign key constraint also has the ON UPDATE CASCADE and ON DELETE
CASCADE option.
• If the ON UPDATE CASCADE option is set, any update in referenced field of
the master table will be reflected in the foreign key field of the referencing
table
• If the ON DELETE CASCADE option is set, any delete in the master table will
result in the delete of corresponding records in the referencing table
Example:
FOREIGN KEY (order_no) REFERENCES sales_order(order_no)
ON UPDATE CASCADE ON DELETE CASCADE,
3
The CHECK constraint
Business rule validations can be applied to a table column by using the CHECK
constraint. CHECK constraints are specified as a logical expression that evaluates to
TRUE or FALSE.
Example:
The following constraints need to be specified for the client_master table:
• Values in the client_no column must start with the capital letter 'C'
• Only 'Bombay', 'Delhi' and 'Madras' are legitimate values for the column city
• The bal_due field cannot have a negative value
CREATE TABLE client_master
(client_no VARCHAR(6) CHECK (client_no LIKE 'C%'),
name VARCHAR(20),
city VARCHAR(15) CHECK (city IN
('Bombay','Delhi','Madras')),
pincode INTEGER,
state VARCHAR(15),
bal_due REAL CHECK(bal_due>=0),
PRIMARY KEY (client_no));
The CHECK constraints can also be specified at the end of the CREATE TABLE
command.
CREATE TABLE client_master
(client_no VARCHAR(6),
name VARCHAR(20),
city VARCHAR(15),
pincode INTEGER,
state VARCHAR(15),
bal_due REAL,
PRIMARY KEY (client_no),
CHECK (client_no LIKE 'C%'),
CHECK (city IN ('Bombay','Delhi','Madras')),
CHECK(bal_due>=0));
4
The NOT NULL Constraint
Often there may be records in a table that do not have values for every field, either
because the information is not available at the time of data entry or because the field
is not applicable in every case. In the absence of a userdefined value, a NULL value
will be placed in the column.
However, there are certain fields for which data is mandatory. Such fields need to be
specified with the NOT NULL constraint.
Example:
The field name in client_master table may be specified as NOT NULL.
CREATE TABLE client_master
(client_no VARCHAR(6),
name VARCHAR(20) NOT NULL,
city VARCHAR(15),
…);
The UNIQUE Constraint
Specifying the UNIQUE constraint for a column ensures that the values of that
column are unique throughout the table.
Example:
The field description in product_master table may be specified as UNIQUE.
CREATE TABLE product_master
(product_no VARCHAR(6),
description VARCHAR(15) UNIQUE,
profit_percent REAL,
…);
NOTE: Primary Key constraints already include the NOT NULL and UNIQUE
constraints. For example, there is no need to specify the NOT NULL and UNIQUE
constraints for the client_no column in the client_master table as the PRIMARY KEY
constraint has already been specified.
5
Adding User Defined Names to Constraints
Constraints can be given userdefined names along with the constraint definition. A
constraint can be dropped by referring to the constraint by its name.
Example:
CREATE TABLE sales_order_details
(order_no VARCHAR(6),
product_no VARCHAR(6),
qty_ordered INTEGER,
qty_disp INTEGER,
product_rate REAL,
CONSTRAINT sod_pk PRIMARY KEY (order_no, product_no),
CONSTRAINT sod_orderno_fk FOREIGN KEY (order_no) REFERENCES
sales_order(order_no) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT sod_prodno_fk FOREIGN KEY (product_no) REFERENCES
product_master(product_no) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT sod_prodrate_chk CHECK (product_rate >0),
);
Defining Integrity Constraints in the ALTER TABLE command
Integrity constraints can also be defined using the CONSTRAINT clause in the
ALTER Table command.
Adding Primary Key Constraint:
ALTER TABLE client_master
ADD PRIMARY KEY (client_no);
Adding NOT NULL Constraint:
ALTER TABLE client_master
MODIFY (name VARCHAR(20) NOT NULL);
Adding FOREIGN KEY, UNIQUE and CHECK constraints:
ALTER TABLE sales_order
ADD CONSTRAINT so_clientno_fk FOREIGN KEY (client_no)
REFERENCES client_master(client_no);
6
ALTER TABLE client_master
ADD CONSTRAINT cm_clientno_chk CHECK (client_no LIKE 'C%');
Deleting PRIMARY KEY constraint:
ALTER TABLE client_master
DROP PRIMARY KEY;
Deleting FOREIGN KEY, UNIQUE and CHECK constraints:
ALTER TABLE sales_order
DROP CONSTRAINT so_clientno_fk;
ALTER TABLE client_master
DROP CONSTRAINT cm_clientno_chk;
Exercise
1. Create the tables described below:
a) Table Name: sales_order
Description: Used to store client's orders
Column
Data Type Size Constraint
Name
Always starts with 'O', Primary
order_no varchar 6
Key
order_date date
Foreign key, references
client_no varchar 6 client_no in client_master table
Always starts with 'C'
dely Addr varchar 25
Foreign key, references
salesman_no varchar 6 salesman_no in
salesman_master table
dely _type char 1 Can only have values 'F' or 'P'
billed_yn char 1 Can only have values 'N' or 'Y'
dely_date date
Can only have values 'In
order_status varchar 10 Process', 'Cancelled' or
'Fulfilled'
7
b) Table Name; sales_order_details
Description: Used to store client's orders with details of each product ordered.
Column Data
Size Constraint
Name Type
Always starts with 'O', Foreign
order_no varchar 6 key, references order_no in
sales_order table
Always starts with 'P', Foreign
product_no varchar 6 key, references product_no in
product_master table
qty_ordered integer 8 Greater than 0
Greater than 0, cannot be greater
qty_disp integer 8
than qty_ordered
product_rate real 10,2 Primary Key
2. Insert the following data into their respective tables.
a) Data for sales_order table:
8
b) Data for sales_order_details table: