0% found this document useful (0 votes)
4 views

DBMS Lab Manual Exp 6 & 7

The document outlines the concept of integrity constraints in databases, detailing various types such as primary keys, foreign keys, check constraints, unique constraints, and not null constraints. It provides syntax examples for creating and modifying tables with these constraints, as well as explanations of referential integrity and its advantages. Additionally, it includes viva questions related to data integrity and the purpose of different constraints.

Uploaded by

fopivac546
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)
4 views

DBMS Lab Manual Exp 6 & 7

The document outlines the concept of integrity constraints in databases, detailing various types such as primary keys, foreign keys, check constraints, unique constraints, and not null constraints. It provides syntax examples for creating and modifying tables with these constraints, as well as explanations of referential integrity and its advantages. Additionally, it includes viva questions related to data integrity and the purpose of different constraints.

Uploaded by

fopivac546
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/ 9

Global Institute of Technology

DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment -6

Aim- Write the query to implement the concept of Integrity constrains.

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 :

CREATE TABLE po_items (po_no INTEGER, item_no INTEGER, product_no


INTEGER, qty INTEGER, net_price NUMERIC, PRIMARY KEY (po_no, item_no));

In case we want to specify the name of the primary key constraint, we


use CONSTRAINT clause as follows:

Syntax:
CONSTRAINT constraint_ name PRIMARY KEY(column_ 1, column_ 2,...);

DR SANGEETA SONI 29
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Define primary key when changing the existing table structure

Syntax: ALTER TABLE table_ name ADD PRIMARY KEY(column_ 1, column_ 2);

Example:

CREATE TABLE products ( product no INTEGER, description TEXT,


product_cost NUMERIC);

When we want to add primary key constraint in the table :

ALTER TABLE products ADD PRIMARY KEY (product_no);

How to add an auto-incremented primary key to an existing table

ALTER TABLE vendors ADD COLUMN ID SERIAL PRIMARY KEY;

Remove primary key

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)),

Define a group of columns as a foreign key :

CREATE TABLE child_ table (c1 INTEGER PRIMARY KEY, c2 INTEGER, c3


INTEGER, FOREIGN KEY (c2, c3) REFERENCES parent_table (p1, p2));

Add a foreign key constraint to an existing table :

ALTER TABLE child table ADD CONSTRAINT constraint_name FOREIGN


KEY (c1) REFERENCES parent_table (p1);

Drop existing foreign key constraint :

ALTER TABLE child_tabIe DROP CONSTRAINT constraint_fkey;

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.

syntax: column_ name data type CONSTRAINT constraint name CHECK(...)

CHECK on ALTER TABLE :

ALTER TABLE Persons ADD CHECK (Age>=18);

DR SANGEETA SONI 31
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

DROP a CHECK Constraint :

ALTER TABLE Persons DROP CHECK CHK_PersonAge;

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 – [CONSTRAINT constraint_ name] UNIQUE(column_ name)

Creating a UNIQUE constraint on multiple columns :

syntax : CREATE TABLE table_name (c1 data_ type, c2 data_ type, c3 data_type,
UNIQUE (c2, c3));

UNIQUE Constraint on ALTER TABLE :

ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID, LastName);

DROP a Unique Constraint :

ALTER TABLE Persons


DROP INDEX UC_Person;

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.

syntax : [CONSTRAINT constraint name] NOT NULL

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) );

Not-null constraint to columns of an existing table :

syntax :
ALTER TABLE table_name
ALTER COLUMN column_name _1 SET NOT NULL,
ALTER COLUMN column_name _ 2 SET NOT NULL;

Example :

ALTER TABLE production_orders

ALTER COLUMN material_id SET NOT NULL,

ALTER COLUMN start_date SET NOT NULL,

ALTER COLUMN finish_date SET NOT NULL;

VIVA Questions:

1. What is data integrity in SQL?


Data integrity in SQL ensures the accuracy, consistency, and reliability of data in a database. It
is maintained through constraints and rules defined on the tables.

2. What are SQL Constraints?


SQL Constraints are used to specify the rules for the data in a table. These are used to limit
which type of data must be stored in the database, and aims to increase the accuracy and
reliability of the data stored in the database.

DR SANGEETA SONI 33
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

3. What is NOT NULL Constraint ?


The NOT NULL constraint makes sure that a column cannot have a NULL value. The NOT
NULL constraint can be used either while creating the table database or while modifying it.

4. What UNIQUE Constraint?


The UNIQUE constraint ensures that values in a column (or a set of columns) are unique
across all records in a table, preventing duplicate entries.

5. How is a Foreign Key different from a Primary Key?


A Primary Key is a constraint that uniquely identifies each record in a table. It ensures that the
column(s) declared as the primary key have unique values and cannot contain NULLs.

6. What is a Default constraint?


The DEFAULT constraint is used to fill a column with default and fixed values. The value
will be added to all new records when no other value is provided.

7. Explain the CHECK constraint.


The CHECK constraint specifies a condition that must be satisfied for the values entered into
a column. It ensures that data meets certain criteria before being inserted or updated.

DR SANGEETA SONI 34
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment-7

AIM: Using the referential integrity constraints.

Referential Integrity Constraint: Referential Integrity is set of constraints applied to foreign


key which prevents entering a row in child table (where we have foreign key) for which we
don't have any corresponding row in parent table. Referential Integrity prevents our table from
having incorrect or incomplete relationship .

Referential Integrity example :

• Create Table command:

CREATE TABLE Dept (dept id INT NOT NULL, dept name VARCHAR(256),
PRIMARY KEY (dept_id)) ENGINE=INNODB;

CREATE TABLE Emp (em id INT NOT NULL,


em name VARCHAR(256), dept id INT,
FOREIGN KEY (dept id) REFERENCES Dept(dept id) ) ENGINE=INNODB;

• Insert Query :

DR SANGEETA SONI 35
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

insert into emp (emp_id,dept_id,emp_name) values(1,105,"amita");

insert into emp (emp_id,dept_id,emp_name) values(2,104,"anita");

insert into emp (emp_id,dept_id,emp_name) values(3,103,"anitya”);

insert into emp (emp_id,dept_id,emp_name) values(4,102,”ananya”);

insert into emp (emp_id,dept_id,emp_name) values(5,101,"amaaya");

• Delete command:

mysql> DELETE FROM Dept;


Query OK, 1 row affected (0.05 sec)

mysql> SELECT * FROM Emp;


Empty SET (0.00 sec)

VIVA QUESTIONS:-

1. What are the Integrity Constraints?


Integrity Constraints in Database Management Systems are the set of pre-defined rules
responsible for maintaining the quality and consistency of data in the database.

2. Explain the advantages of referential Integrity.


 Prevents the entry of duplicate data
 Prevents one table from pointing to a nonexistent field in another table
 Guarantees consistency between "partnered" tables
 Prevents the deletion of a record that contains a value referred to by a foreign
key in another table
 Prevents the addition of a record to a table that contains a foreign key unless
there is a primary key in the linked table

3. What is the CASCADE option in a Foreign Key constraint?


The CASCADE option means that if a referenced row in the parent table is deleted or
updated, the corresponding rows in the child table are automatically deleted or updated to
maintain referential integrity.

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.

5. Explain the types of Integrity Constraints.

There are four types of integrity constraints which are:

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.

4. Referential integrity constraints: A referential integrity constraint is specified a valid


relationship between two relational tables. This valid relationship between the two tables
confirms that a foreign key exists in a table. It should always reference a corresponding
value or attribute in the other table or be null.

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

You might also like