DBMS Lab Manual Exp 6 & 7
DBMS Lab Manual Exp 6 & 7
Experiment -6
INTEGRITY CONSTRAINT: Integrity constraints are a set of rules. It is used to maintain the
quality of information. Integrity constraint is used to guard against accidental damage to the
database. Constraints can be defined in two ways :-
1) The constraints can be specified immediately after the column definition. This is called
column-level definition.
2) The constraints can be specified after all the columns are defined. This is called table-level
definition.
DATABASE CONSTRAINTS
• PRIMARY Key
• FOREIGN Key
• CHECK Constraint
• UNIQUE Constraint
• NOT NULL Constraint
PRIMARY KEY : A primary key is a column or a group of columns used to identify a row
uniquely in a table. A primary key constraint is the combination of a not-null constraint and a
UNIQUE constraint.
Syntax: CREATE TABLE TABLE_Name (column_1 data_ type PRIMARY KEY, column_2
data_ type,…….);
Example :
Syntax:
CONSTRAINT constraint_ name PRIMARY KEY(column_ 1, column_ 2,...);
DR SANGEETA SONI 29
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
Syntax: ALTER TABLE table_ name ADD PRIMARY KEY(column_ 1, column_ 2);
Example:
syntax . ALTER TABLE table name DROP CONSTRAINT primary_ key_ constraint;
FOREIGN KEY: Also called referential integrity. This constraint identifies any column
referencing the PRIMARY KEY in another table. For a column to be defined as a Foreign
Key, it should be a defined as a Primary Key in the table which it is referring. One or more
columns can be defined as Foreign key.
DR SANGEETA SONI 30
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
CREATE TABLE Orders (OrderID int NOT NULL, OrderNumber int NOT
NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID)
REFERENCES Persons(PersonID)),
CHECK Constraint : This constraint defines a business rule on a column. All the rows must
satisfy this rule. The constraint can be applied for a single column or a group of columns.
DR SANGEETA SONI 31
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
UNIQUE Constraint : This constraint ensures that a column or a group of columns in each
row have a distinct value. A column(s) can have a null value but the values cannot be
duplicated.
syntax : CREATE TABLE table_name (c1 data_ type, c2 data_ type, c3 data_type,
UNIQUE (c2, c3));
NOT NULL Constraint : This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not allowed.
DR SANGEETA SONI 32
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
Example :
CREATE TABLE invoice (id serial PRIMARY KEY, product_ id int NOT
NULL, qty numeric NOT NULL CHECK (qty > 0), net_price numeric
CHECK(net_price > 0) );
syntax :
ALTER TABLE table_name
ALTER COLUMN column_name _1 SET NOT NULL,
ALTER COLUMN column_name _ 2 SET NOT NULL;
Example :
VIVA Questions:
DR SANGEETA SONI 33
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
DR SANGEETA SONI 34
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
Experiment-7
CREATE TABLE Dept (dept id INT NOT NULL, dept name VARCHAR(256),
PRIMARY KEY (dept_id)) ENGINE=INNODB;
• Insert Query :
DR SANGEETA SONI 35
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
• Delete command:
VIVA QUESTIONS:-
DR SANGEETA SONI 36
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
4. Explain the purpose of the ON DELETE SET NULL option in a Foreign Key constraint.
If ON DELETE SET NULL is specified, it means that when a referenced row in the parent
table is deleted, the corresponding foreign key values in the child table are set to NULL.
1. Domain Constraints: Domain constraints can be defined as the definition of a valid set
of values for an attribute. The data type of domain includes string, character, integer,
time, date, currency, etc.
2. Entity integrity Constraints: The entity integrity constraint states that primary key
value can't be null. A primary key is used to identify individual records in a table and if
the primary key has a null value, then we can't identify those records .
3. Key Constraints: Keys are the set of entities that are used to identify an entity within
its entity set uniquely. An entity set can have multiple keys, but out of which one key
will be the primary key. A primary key can contain a unique and not null value in the
table.
6. What is a subquery?
Also called an inner query; a query placed inside another query, or an outer query. A
subquery may occur in the clauses such as SELECT, FROM, WHERE, UPDATE, etc.
DR SANGEETA SONI 37