Sie sind auf Seite 1von 5

MySQL 04

INTEGRITY CONSTRAINTS

1) Create a database Department. Inside this database, create a table Members


having the following structure. Constraints to be added are column level constraints.
Name of Column
ID
Name
User_id
DOB

Type
CHAR (2)
VARCHAR (15)
VARCHAR (15)
DATE

Constraint
Primary Key
Not Null
Unique
Not null

create database group1;


use group1;
create table people
(ID char(2) primary key,
Name varchar(15) not null,
User_id varchar(15) unique,
DOB date not null);

2) List the columns and their associated constraints of the Members table.

show create table people;

3) Display the physical structure of the table Members.

desc people;

4) Insert the following 3 records shown below and save the changes. Display data.
ID
Name
User_id
DOB
M1
M2
M3

Azhar
Priya
Raj

A001
P001
R002

insert into people values('M1','Rahul','R001','1980-05-24');


insert into people values('M2','Sai','S003','1956-06-13');
insert into people values('M3','Priya','P001','1975-01-02');
insert into people values('M4','Raj','R002','1978-02-05');
commit;
select * from people;

24th May 1980


2nd Jan 1975
5th Feb 1978

Testing Integrity Constraints


5) Insert a tuple into this table having the information M1,Arjun, A002, 1977-03-31.
Will this work? Why/ why not?

insert into people values('M1','Arjun','A002','1977-03-31');

6) Insert a record into this table having the information M4,Azhar,A004,1977-11-20.


What will happen?

insert into people values('M5','Azhar','A004','1977-11-20');

7) Insert a record having the information M5,Smitha,S001. Is this allowed?

insert into people values('M6','Smitha','S001');

8) Insert a tuple having the attributes M6, Peter,P001,1981-10-04. Will it work?

insert into people values('M7','Peter','P001','1981-10-04');

9) Delete the member M4. Save the changes. Display the data.

delete from people where id='M4';


select * from people;

10) Change DOB of Azhar to 24th June 1980.

update people set dob='1980-06-24' where name= Azhar';

11) Modify Priyas name to Priyadarsini and User_id to P002. Verify using Select command.

update people set name='Priyadarsini', user_id='P002' where name='Priya';


select * from people;

12) Drop the unique constraint of User_id. Verify using describe command.

alter table people drop index user_id;


desc people;

13) Create a new table called Job.

Name of Column
Job_id
Job_des
Salary

Type
INT (2)
VARCHAR (15)
FLOAT(8,2)

create table work


(Job_id int(2),
Job_des varchar(15),
Salary float(8,2));

14) Add the following specifications:


a) Job_id is the Primary key.
b) job_des must be a unique value and cannot be left blank.

alter table work add primary key(Job_id);


alter table work modify Job_des varchar(15) unique not null;

15) Add a default value 3500 to the Salary field column.

alter table work modify salary float(8,2) default'3000';

16) Add a new field named M_ID to the table Job. It should be of type Varchar and
size 4. Use the describe command to verify.

alter table work add M_ID varchar(4);


desc work;

17) Modify the M_ID field to type Char and size 2.

alter table work modify M_ID char(2);

18) Insert a null value to Job_id and Secretary to job_des field to test the primary
key constraint.

insert into work(job_id,job_des) values(null,'Secretary');

19) Drop the primary key constraint.

alter table work drop primary key;

20) Insert the value Officer two times using Insert command to test the unique key
constraint.

insert into work(job_des) values('Officer');


insert into work(job_des) values('Officer');

21) Drop the unique key constraint.

alter table work drop index job_des;

22) Insert a null value to job_des field to test not null constraint. Drop the not null
constraint.

insert into work(job_des) values(null);


alter table work modify Job_des varchar(15);

23) Insert a tuple 10,clerk to job_id and job_des fields so that default value 3500 is
automatically added to Salary field. Display the data using select command.

insert into work(job_id, job_des) values(10,'clerk');


select * from work;

24) Delete the record. Drop the default value of the field Salary.

delete from work;


alter table work modify salary float(8,2);

25) Insert the following 3 records shown below and save the changes. Display data.

Job_id

Job_des

Salary

M_ID

10
11
12

Teacher
Cashier
Teacher

4000
3500
4000

M1
M2
M1

insert into work values(10,'Teacher',4000,'M1');


insert into work values(11,'engineer',3500,'M2');
insert into work values(12,'Teacher',4000,'M1');

26) List Members and Job table using Select command.

select * from people;


select * from work;

27) In both the tables i.e. ID of Members table and M_ID of Job table is same. Set
foreign key constraint for M_ID in job table with reference to ID of members table.

alter table work add foreign key(M_ID) references people(id);

28) Test referential integrity by violating foreign key constraint.


a) Insert M5 into M_ID of job table.
b) update the first record in members table where id is m1 to m4.

insert into work(m_id) values('M5');


update people set id='M4' where id='M1';

29) Drop the foreign key constraint.

alter table work drop foreign key work_ibfk_1;

30) Create a new table Payment. Constraints to be added are Table level constraints.
Name of Column
Type
Constraint
Job_id
INT (2)
Des
VARCHAR (15)
Payment
FLOAT(8,2)
MID
Char(2)
Foreign key referencing Id of
members table.

create table Payment


(Job_id int(2),
Des varchar(15),
Payment float(8,2),
MID char(2),
foreign key(MID) references people(id));

31) Insert all records from job table to payment table. Test the foreign key constraint.
a) Delete the record from members table where ID is M1.
b) Modify the second record in members table by changing id M2 to M9.

insert into payment(select * from work);


delete from people where id='M1';
update people set id='M9' where id='M2';

32) Drop database and quit.

drop database group1;


quit;

Das könnte Ihnen auch gefallen