Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

LAB 2

Date-31-08-2021

AIM- To study about integrity constraints commands and


implement them on the database.

INTEGRITY CONSTRAINTS-
 DOMAIN INTEGRITY CONSTRAINTS-
It verifies whether the data entered is in proper form and
also they set a range for the input data.

Not null Constraints-


PURPOSE-
The enforcement of Not null constraints in a table makes the user to
enter a value in the field.

SYNTAX-
create table tablename (columnname1 datatype not null,
columnname2 datatype, columnname3 datatype, ....);
QUERY-
create table job(id int(5) not null,name char(10),job char(10));

insert into job values(1,'hyungsik','actor');

insert into job values(2,'rose','singer');

insert into job values(3,'null','actor');

insert into job values(null,'lisa','dancer');

ERROR 1048 (23000): Column 'id' cannot be null

OUTPUT-
Check constraints-
PURPOSE-

It allows only a particular range of values defined in the constraints.

SYNTAX-

create table tablename (columnname1 datatype,columnname2


datatype, ...., check(condition);

QUERY-
CREATE TABLE student (id INTEGER PRIMARY KEY,name TEXT NOT
NULL, gender TEXT NOT NULL,check(id>1);

insert into student values(1,'kumoulica','f');

ERROR 3819 (HY000): Check constraint 'student_chk_1' is violated.

insert into student values(2,'lica','f');

insert into student values(3,'kanna','f');

select * from student;

OUTPUT-
 ENTITY INTEGRITY CONSTRAINTS

Entity integrity constraints are used to enforce consistency in the


database. There are two types of entity integrity constraints.

Unique constraints-
PURPOSE-

It is used to prevent duplication of data in a column or a group of


columns in a table. It allows the null values.

SYNTAX-

create table tablename (columnname1 datatype unique,


columnname2 datatype, ....);

Primary key constraints


PURPOSE-

It is used to prevent duplication of data in a column or a group of


columns in a table. It does not allows the null values.

SYNTAX-

create table tablename (columnname1 datatype primary key,


columnname2 datatype, ....);
QUERY-
CREATE TABLE employ ( id int(5) primary key,name char(20) NOT
NULL,dept char(10) unique,age int(2) check(age>24),salary int(10));
insert into employ values(26543,'trinadh','ceo',43,3000000);
insert into employ values(23443,'srinu','secretary',47,250000);
insert into employ values(45543,'akshara','manager',25,20000000);
insert into employ values(92843,'mahesh','accountant',14,200000);
ERROR 3819 (HY000): Check constraint 'employ_chk_1' is violated.
insert into employ values(48732,'ram','coo',39,2000000);
insert into employ values(48733,'hyungsik','cof',29,5000000);
insert into employ values(48742,'rosie','ceo',25,4000000);
ERROR 1062 (23000): Duplicate entry 'ceo' for key 'employ.dept'
insert into employ values(48732,'woobin','cod',35,2000000);
ERROR 1062 (23000): Duplicate entry '48732' for key 'employ.PRIMARY'
select * from employ where salary>20000;

OUTPUT-
 REFERENTIAL INTEGRITY CONSTRAINTS-

PURPOSE-

Referential integrity constraints enforce relationship between tables.


The foreign key establishes a column or a combination of columns as a
foreign key. It establishes a relationship with the a specified primary or
unique key in another table, called the referenced key

Syntax-

create table tablename (columnname1 datatype references


table1(columnname) columnname2 datatype, ....);
EXERCISE QUESTIONS
1. Alter the table product_info to make the type column NOT NULL.

QUERY-
alter table product_info add(type char(5) not null);

select * from product_info;

2. Alter the table pc to have a default speed of 2.


QUERY-
alter table pc modify speed int(3) default 2;
3. Create suitable primary keys for all the tables of the above database
schema
a. Check if the primary keys are created successfully

QUERY-
alter table pc add primary key (model_no);
desc pc;

alter table product_inf add primary key(model_no);


desc product_inf;

alter table laptop add primary key(model_no);


desc laptop;
alter table printer add primary key(model_no);
desc printer;

14. Create suitable foreign keys for all the tables of the above
database schema

QUERY-
Alter table laptop
add foreign key(Model_No) references Product_info(Model_no);
Alter table printer
add foreign key(Model_No) references Product_info(Model_No);
Alter table Pc
add foreign key(Model_No) references Product_info(Model_No);
5. Check Constraints:
a. Apply a check constraint on the product_info table such that the only
permitted values for the type column are ‘pc’, ‘lp’ and ‘pr’.

QUERY-
alter table Product_info
Modify column Type Varchar(50) check(Type = PC OR Type = LP OR Type =
PR);

b. Apply a check constraint such that the prices of pc, laptop


and printer are all positive.

QUERY-
Alter table PC Modify column price Integer check(Price > 0);
Alter table laptop
Modify column price integer check(price > 0);
Alter table Printer
Modify column Price integer check(price > 0);
6. What are the different values for constraint_type in the
user_constraints table and what is the meaning of each of
those values?
In the table’s Product_info, Primary key is used as constraint for
Model_No To ensure that the model number is unique and not null.
Then check constraint is used to ensure that price is not below 0. And
since the product is laptop or Pc or printer, check constraint with OR
operator is used for type. Then a not null constraint is used in type of
Product_info table. And then foreign key is used in table Pc,Laptop
and Printer to link with table Product_info.

7. Create a table named ‘Type_info’ with the columns model ,


type from ProductInfo table ( Use the “create table yyy as select”
construct )
a. Initially no rows should be present in the dummy table
b. Insert all the rows from product_info into ‘Type_Info’ (only
selected columns)

QUERY-
create table type_info as select model_no,type from product_info;
desc type_info;
8. Modify the table printer by adding a column printercode of
type varchar2(10) to identify the printer uniquely in the world.

QUERY-
alter table type_info add column printercode char(10);
desc type_info;

9. Add a Unique key constraint in the printercode column.


a. Check if the unique key constraint is created successfully
(User_Constraints table)

QUERY-
alter table type_info add unique(printercode);
desc type_info;
10. Increase the width of the column printercode by 2.

QUERY-
alter table type_info modify column printercode char(12);
desc type_info;

11. Without removing the table definition from the database remove all the
rows from the table ‘Type_info’.

QUERY- truncate table type_info;

12. Drop the table ‘type_info’ from the database

QUERY- drop table type_info;

You might also like