0% found this document useful (0 votes)
77 views20 pages

Data Integrity Constraints-Lab-2 Notes

This document discusses various types of data integrity constraints in SQL including: not null, unique, primary key, default, check, like, in, foreign key constraints. It provides the syntax for creating these constraints when tables are created and for altering existing tables to add constraints. Examples are given for each constraint type. Managing constraints through enabling, disabling, and dropping them is also covered.

Uploaded by

chauhansumi143
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)
77 views20 pages

Data Integrity Constraints-Lab-2 Notes

This document discusses various types of data integrity constraints in SQL including: not null, unique, primary key, default, check, like, in, foreign key constraints. It provides the syntax for creating these constraints when tables are created and for altering existing tables to add constraints. Examples are given for each constraint type. Managing constraints through enabling, disabling, and dropping them is also covered.

Uploaded by

chauhansumi143
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/ 20

DATA INTEGRITY CONSTRAINTS LAB 3

CONSTRAINTS
It is used to specify rules for data in a table.
It can be specified when the table is created
And it can also be specified when table is already exists by using alter command
TYPES OF CONSTRAINTS
1. Not Null
2. Unique
3. Primary Key
4. Default
5. Check
6. Like
7. In
8. Foreign Key
9. Default
10. Index
NOT NULL CONSTRAINT
By default, a column can hold NULL values. If you do not want a column to have a
NULL value, then you need to define such a constraint on this column specifying that
NULL is now not allowed for that column.
Example: name of employee is must in employee table, In some forms there are some
columns that are mandatory.
Syntax:
CREATE TABLE table_name (Column1 datatype NOT NULL);
Example
Create table sample(S_id number(7) not null, FirstName varchar(22), LastName
varchar(20), E_mail varchar(22), Mob_no number(10));
When there is already table, and we can apply not null constraint in existing table.
Syntax
ALTER TABLE table_name modify column_name datatype not null;
Example:
Alter table Sample modify FirstName varchar(22) not null;
UNIQUE CONSTRAINT
When u don‟t need duplicate value in a particular column or in multiple column then
we use unique constraint.
Example: Mobile number, email
Syntax:
CREATE TABLE table_name(Column datatype UNIQUE…)
Example
Create table sample(S_id number(7) not null, FirstName varchar(22), LastName
varchar(20), E_mail varchar(22) unique, Mob_no number(10));
Syntax for Single Constraint in multiple columns
CREATE TABLE table_name(Col1 datatype, col2 datatpye, col3 datatype,
UNIQUE(Col1,Col2));
Example:
Create table emp(e_name varchar(11), LastName varchar(10), DateOfJoining
varchar(20), unique(e_name, DateOfJoining ));
Add unique constraint in already existing table.
Syntax:
ALTER TABLE table_name ADD UNIQUE(Column_name);
Example
Alter table sample add unique(Mob_no);
PRIMARY KEY CONSTRAINT
Primary key is the which is the key which can not be null and have unique value.
It can be single or multiple attribute.
Syntax:
CREATE TABLE table_name (col1 datatype, col2 datatype, col3 datatype PRIMARY
KEY, col4 datatype);
Example
Create table emp(E_id number primary key, name varchar(20), age number(2));
For multiple Attributes
Syntax:
CREATE TABLE table_name(col1 datatype, col2 datatype, col3 datatype, primary
key(col1,col3));
Example
Create table info(dept_id number(3) Emp_name varchar(30), salary number(10),
primary key(dept_id,Emp_name));
In existing table
Syntax:
ALTER TABLE table_name ADD PRIMARY KEY(Col_name);
Example
Alter table emp add primary key(e_id);
In one table there is only one primary key
It avoid not null and unique constraint.
DEFAULT CONSTRAINT
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all
new records, if no other value is specified.
Create table tablne_name (c1 datatype, c2 datatype, c3 dadatype default „Def_Val‟);
To use this on Existing Tables we use
Alter Table table_name ALTER Col_name Set Default „Def_Val‟;
CHECK CONSTRAINT
The CHECK constraint ensures that all the values in a column satisfies certain
conditions.
Example: Salary, age
For this you can apply any operators.
Syntax:
CREATE TABLE table_name (col1 datatype check(condition), col2 datatype…)
Example:
Create table info(dept_id number(3) Emp_name varchar(30), salary number(10)
check (salary>10000), age number(2);
In Existing Table
Syntax:
ALTER TABLE table_name ADD CHECK(condition);
Example:
Alter table info add check(age>20);
LIKE OPERATOR
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents
one, single character.
SELECT c1, c2 FROM table_name1 WHERE c1 LIKE pattern;
WHERE c1 LIKE 'a%' Finds any values that start with "a"
WHERE c1 LIKE '%a' Finds any values that end with "a"
WHERE c1 LIKE '%or%' Finds any values that have "or" in any position
WHERE c1 LIKE '_r%' Finds any values that have "r" in the second position
WHERE c1 LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE c1 LIKE 'a%o’ Finds any values that start with "a" and ends with "o"
We can also use the LIKE operator with the check constraint while creating the table
to make sure that the columns have a particulate type of input.
Eg: for a column to have only values that start with the letter “O”
create table tab1(c1 varchar(5) check (c1 like “O%"), c2 varchar(5), c3 varchar(5));

This can also be added after the table is created using the alter table command.
alter table table_name ADD CONSTRAINT cnst CHECK (c2 like "%p");
IN OPERATOR
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2);

We can also use in operator while creating the table so as to allow only specific entries in a column.
create table tab2 (c1 varchar(5), c2 varchar(5), c3 varchar(5) check (c3 in ('val1', 'val2', 'val3')));
It can also be implemented on already existing tables
alter table tab2 add constraint check_c1 check (c1 in ('val1', 'val2', 'val3'));
FOREIGN KEY CONSTRAINT
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table.
The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it
must be one of the values contained in the parent table.

Department Table Employee Table


While Creation of table:
Create table Table_name1 (c1 data type primary key, c2 datatype, c3 datatype);
Create table Table_name2 (c1 data type primary key, c2 datatype, c3 datatype foreign key references
table_name1(c1));

After Creation of table:


For this there must be two tables and both have primary key.
ALTER TABLE table_name ADD FOREIGN KEY(ColName) REFERENCES table_name2(ColName2)
ON DELETE SET NULL
Example:
Alter table employee add foreign key(D_id) references Department(D_id) on delete set null
MANAGING THE CONSTRAINTS
To see all the constraints use
DESC USER_CONSTRAINTS
Disable the constraint
ALTER TABLE table_name DISABLE constraint_name;
Enable the Constraints
ALTER TABLE table_name ENABLE constraint_name;
Dropping the constraints
ALTER TABLE table_name DROP constraint_name;
Using dropping u cant enable the constraint for that u have to recreate the constraint.

You might also like