0% found this document useful (0 votes)
41 views10 pages

Lab Sheet 08 (ICT.005)

The document provides instructions for modifying table constraints and using basic SELECT statements in MySQL. It includes creating tables without constraints, then adding primary keys, unique constraints, default values, and foreign keys. SELECT statements are used to retrieve data from the tables, such as employee details, manager NICs, project information, employees who work over 40 hours, and department locations. The aims are to practice modifying table constraints and using SELECT statements appropriately.

Uploaded by

sewmini hewage
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)
41 views10 pages

Lab Sheet 08 (ICT.005)

The document provides instructions for modifying table constraints and using basic SELECT statements in MySQL. It includes creating tables without constraints, then adding primary keys, unique constraints, default values, and foreign keys. SELECT statements are used to retrieve data from the tables, such as employee details, manager NICs, project information, employees who work over 40 hours, and department locations. The aims are to practice modifying table constraints and using SELECT statements appropriately.

Uploaded by

sewmini hewage
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/ 10

SEU/IS/18/ICT/005

Practical Recording.

Date: 03rd September 2020.


Practical Number: 08.
Title: Modify Table Constraints and basic SELECT statement.
Aims:
 Getting practice to modify constraints on created tables.
 Getting practice to use SELECT statements appropriately.
Exercise 01:
1. Create the tables given below. (Do not add any constraints during the table creation).
Create Employee table:
mysql> create table employee
-> (
-> Fname varchar(60),
-> Lname varchar(60),
-> NIC varchar(12),
-> salary int(6),
-> supervisor_NIC varchar(12),
-> Dno int
-> );
Create Project table:
mysql> create table project
-> (
-> Pname varchar(60),
-> Pno int,
-> Plocation varchar(50),
-> Dno int
-> );

Page 1 of 10
SEU/IS/18/ICT/005

Create works_on table:


mysql> create table works_on
-> (
-> Eid varchar(60),
-> Phone int,
-> Hours int(4)
-> );
Create Location table:
mysql> create table location
-> (
-> Dno int,
-> Dlocation varchar(60)
-> );
Create Department table:
mysql> create table department
->(
-> Dname varchar(50),
-> Dno int,
-> MID varchar(50)
-> );
2. Add the columns underlined as primary keys of the respective table.
Employee:
mysql> ALTER TABLE employee
-> ADD PRIMARY KEY (NIC);

Page 2 of 10
SEU/IS/18/ICT/005

Project:
mysql> ALTER TABLE project
-> ADD PRIMARY KEY (Pno);

Works_on:
mysql> ALTER TABLE works_on
-> ADD PRIMARY KEY (Eid);

Location:
mysql> ALTER TABLE Location
-> ADD PRIMARY KEY (Dno);

Department:
mysql> ALTER TABLE Department
-> ADD PRIMARY KEY(Dno);

Page 3 of 10
SEU/IS/18/ICT/005

3. Set Dname of the table DEPARTMENT and Pname of the table PROJECT to UNIQUE.
 mysql> ALTER TABLE department
-> ADD CONSTRAINT UNIQUE (Dname);

 mysql> ALTER TABLE project


-> ADD CONSTRAINT UNIQUE (Pname);

4. Set ‘12’ as the default Hours in the table WORKS_ON.


mysql> ALTER TABLE works_on
-> ALTER hours SET DEFAULT 12;

5. Drop the PRIMARY KEY of the table LOCATIONS.


mysql> ALTER TABLE location
-> DROP PRIMARY KEY;

6. Add (Dnumber and Dlocation) as PRIMARY KEY to the table LOCATIONS.


mysql> ALTER TABLE location
-> ADD CONSTRAINT PRIMARY KEY (Dno, Dlocation);

Page 4 of 10
SEU/IS/18/ICT/005

7. Drop the UNIQUE KEY Pname of the table PROJECT.


mysql> ALTER TABLE project
-> DROP INDEX Pname;

8. Drop the default value ‘12.0’ and set it back to ‘13’ for Hours in WORKS_ON table.
mysql> ALTER TABLE works_on
-> ALTER hours SET DEFAULT 13;

9. Add the foreign keys to the tables created above using the details given in the following
table.

fk_Dno:
mysql> ALTER TABLE EMPLOYEE
-> ADD CONSTRAINT fk_Dno
-> FOREIGN KEY (Dno) REFERENCES deparment (Dno);

Page 5 of 10
SEU/IS/18/ICT/005

fk_Mgr:
mysql> alter table department
-> add constraint fk_mgr
-> foreign key (mid) references employee(nic);

fk_Dnumber:
mysql> ALTER TABLE location
-> ADD CONSTRAINT fk_Dnumber
-> FOREIGN KEY(Dno) REFERENCES department(Dno);

fk_Dnum:
mysql> ALTER TABLE project
-> ADD CONSTRAINT fk_Dnum
-> FOREIGN KEY(Dno) REFERENCES department(Dno);

fk_loc:
mysql> ALTER TABLE project
-> ADD CONSTRAINT fk_loc
-> FOREIGN KEY(Plocation) REFERENCES department(Dno);

fk_emp:
mysql> ALTER TABLE works_on
-> ADD CONSTRAINT fk_emp
-> FOREIGN KEY(emp_nic) REFERENCES department(nic);

fk_Dnum:
mysql> ALTER TABLE works_on
-> ADD CONSTRAINT fk_Dnum

Page 6 of 10
SEU/IS/18/ICT/005

-> FOREIGN KEY(pno) REFERENCES department(pno);

10. Drop the FOREIGN KEY “fk_loc” of the table PROJECT.

mysql> ALTER TABLE project


-> DROP FOREIGN KEY fk_loc;

Exercise 02:
1. Display the entire details of all the employees.
SELECT * FROM employee;

Page 7 of 10
SEU/IS/18/ICT/005

2. Display the NIC of the managers (Hint: Mgr_NIC from table DEPARTMENT).

mysql> SELECT MID FROM department;

3. Display the available projects with their project number (Hint: Pname, Pnumber from
table PROJECT).

mysql> SELECT Pname,Pno FROM project;

Page 8 of 10
SEU/IS/18/ICT/005

4. List out the project names that are located in Houston.

mysql> SELECT Pname FROM project


-> WHERE plocation="Houston";

5. Find out the NIC of employees who works either in Project 1 or in Project 5.

mysql>SELECT emp_NIC
->FROM works_on
->WHERE pno In(1,5);

6. Display the names (Fname and Lname), NIC and Salary of the employees who obtain a
salary more than 40000.

mysql>SELECT Fname, Lname, NIC, Salary


->FROM employee
->WHERE salary>4000;

Page 9 of 10
SEU/IS/18/ICT/005

7. Display the NIC of the employees who work less than 15 hours.

mysql>SELECT Emp_NIC
->FROM WORKS_ON
->WHERE Hours <15;

8. In the table LOCATIONS, find out the DISTINCT location of the departments.

mysql>SELECT Dlocation
->FROM location;

9. List the names (Fname and Lname) of the employees who do not work in Department 5.

mysql>SELECT Fname,Lname
->FROM employee
WHERE Dno=5;

Discussion:
 Studied about how to modify constraint created tables.
 Learned about how to use SELECT statement appropriately.

Page 10 of 10

You might also like