NAME- AASHAY KUMAR REG.
NO- 20BCE2180
School of Computer Science and Engineering
CSE2004 - Database Management Systems Lab
Cycle Sheet - I
Aim: To study Data Definition and Data Manipulation commands.
Consider the following schema:
Table Name: Employee
Attribute Data Type
First Name VARCHAR(15)
Mid Name CHAR(2)
Last Name VARCHAR(15)
SSN Number CHAR(9)
Birthday DATE
Address VARCHAR(50)
Sex CHAR(1)
Salary NUMBER (7)
Supervisor SSN CHAR(9)
Department Number NUMBER (5)
Table Name: Department
Attribute Data Type
Department Name Varchar(15)
Department Number Number(5)
ManagerSSN CHAR(9)
ManageStartDate DATE
Table Name: Project
Attribute Data Type
Project Name VARCHAR(15)
Project Number NUMBER(5)
Project Location VARCHAR(15)
Department Number NUMBER(5)
NAME- AASHAY KUMAR REG.NO- 20BCE2180
Data For Employee Table
Mini
FName LName SSN BDate Address Sex Salary SuperSSN DepNo
t
Doug E Gilbert 123 09-JUN-1968 Chennai M 80000 1
Joyce PAN 124 07-FEB-1973 Vellore F 70000 1
Frankin T Wong 125 08-DEC-1972 Delhi M 40000 123 2
Jennifer S Wallace 564 20-JUN-1983 Chennai F 43000 123 2
John B Smith 678 09-JAN-1987 Madurai M 30000 124 1
Ramesh K Narayan 234 15-SEP-1985 Bangalore M 38000 124 3
Data For Department table
DName DepNo MgrSSN MgrStartDate
Administration 2 564 03-Jan-2012
Headquarter 1 678 16-Dec-2014
Finance 3 234 18-May-2013
IT 4 123 12-Jun-2015
Data For Project
PName PNumber Plocation DepNo
ProjectA 3388 Delhi 1
ProjectB 1945 Hyderabad 1
ProjectC 6688 Chennai 2
ProjectD 2423 Chennai 2
ProjectE 7745 Bangalore 3
NAME- AASHAY KUMAR REG.NO- 20BCE2180
CREATING TABLE
create table Employee
(
"First Name" varchar(15),
"Mid Name" char(2),
"Last Name" varchar(15),
"SSN Number" char(9),
Birthday date,
Address varchar(50),
Sex char(1),
Salary number(7),
"Supervisor SSN" char(9),
"Department Number" number(5)
create table Department
(
"Department Name" varchar(15),
"Department Number" number(5),
"Manager SSN" char(9),
ManagerStartDate date
)
create table Project
(
"Project Name" varchar(15),
"Project Number" number(5),
"Project Location" varchar(15),
"Department Number" number(5)
)
NAME- AASHAY KUMAR REG.NO- 20BCE2180
Exercise-I:
1. Insert the data given above in both employee, department and project
tables.
insert into Employee values (' Doug' , ' E', ' Gilbert' , ' 123' ,' 09-JUN-1968', '
Chennai','M','80000',' ','1')
insert into Employee values (' Joyce' , ' ', 'PAN' , ' 124' ,' 07-FEB-1973', '
Vellore','F','70000',' ','1')
insert into Employee values (' Franklin' , ' T', 'Wong' , ' 125' ,' 08-DEC-1972',
'Delhi','M','40000','123','2')
insert into Employee values (' Jennifer' , ' S', 'Wallace' , ' 564' ,' 20-JUN-1983',
'Chennai','F','43000','123','2')
insert into Employee values (' John' , ' B', 'Smith' , '678' ,' 09-JAN-1987',
'Madurai','M','30000','124','1')
insert into Employee values (' Ramesh' , ' K', 'Narayan' , ' 234' ,' 15-SEP-1985',
'Bangalore','M','38000','124','3')
insert into Department values( ' Administration','2','564','03-JAN-2012')
insert into Department values(' Headquarter','1','678','16-DEC-2014')
insert into Department values ('Finance','3','234','18-MAY-2013')
insert into Department values (' IT','4','123','12-JUN-2015')
insert into Project values('ProjectA','3388','Delhi','1')
insert into Project values('ProjectB','1945','Hyderabad','1')
insert into Project values('ProjectC','6688','Chennai','2')
insert into Project values('ProjectD','2423','Chennai','2')
insert into Project values('ProjectE','7745','Bangalore','3')
NAME- AASHAY KUMAR REG.NO- 20BCE2180
2. Display all the employees’ information.
select * from Employee
3. Display Employee name along with his SSN and Supervisor SSN.
select "First Name","Mid Name","Last Name","SSN Number","Supervisor SSN" from
Employee
NAME- AASHAY KUMAR REG.NO- 20BCE2180
4. Display the employee names whose bdate is ’20-JUN-1983’.
select "First Name","Mid Name","Last Name" from Employee where Birthday = '20-Jun-
1983'
5. Display salary of the employees without duplications.
select distinct Salary from Employee
6. Display the MgrSSN, MgrStartDate of the manager of ‘Finance’ department
select "Manager SSN",ManagerStartDate from Department where "Department
Name" = 'Finance'
NAME- AASHAY KUMAR REG.NO- 20BCE2180
7. Modify the department number of an employee having fname as ‘Joyce’ to 2
update Employee set "Department Number" = '2' where "First Name" = 'Joyce'
8. Alter Table department add column DepartmentPhoneNum of NUMBER data type and
insert values into this column only.
alter table Department add DepartmentPhoneNum number(10)
9. Alter table department to modify the size of DepartmentPhoneNum.
alter table Department modify DepartmentPhoneNum number(15)
10.Modify the field name DepartmentPhoneNum of departments table to PhNo.
alter table Department rename column DepartmentPhoneNum to PhNo
NAME- AASHAY KUMAR REG.NO- 20BCE2180
11. Rename Table Department as DEPT.
alter table Department rename to DEPT
12. Alter Table department remove column PhNo.
alter table Dept drop column phno
13. Create a table COPYOFDEPT as a copy of the table DEPT.
create table COPYOFDEPT as select * from DEPT
NAME- AASHAY KUMAR REG.NO- 20BCE2180
14. Delete all the rows from COPYOF DEPT table.
delete from COPYOFDEPT
15. Remove COPYOF DEPT table.
drop table COPYOFDEPT
NAME- AASHAY KUMAR REG.NO- 20BCE2180
Exercise: II
Aim: To use constraints and formulate the tables to have consistent data.
Table Name: Employee
Attribute Data Type Constraint
First Name Varchar (15) Not Null
Mid Name Char(2)
Last Name Varchar (15) Not Null
SSN Number Char (9) Primary Key
Birthday Date
Address Varchar (50)
Sex Char(1) Sex In (M,F,m,f)
Salary Number (7) Default 800
Supervisor SSN Char (9) Foreign Key Employee (SSN)
on delete set null
Department number Number(5) Foreign key to department
number of department table on
delete cascade
Table Name : Department
Attribute Data type Constraint
Department Name Varchar(15) Not Null
Department number INT(5) Primary key
Manager SSN Char (9) Foreign key-Employee (SSN)
on delete set null
Manage start date Date
Table Name: Project
Attribute Data type Constraint
Project Name Varchar2(15) Not Null
Project number Number(5) Primary key
Project Location Varchar2(50)
Department Number Number(5) Foreign Key –Department (dep
no ) on delete set null
NAME- AASHAY KUMAR REG.NO- 20BCE2180
I. Add the above mentioned constraints to employee, project and department tables using
alter table statement.
ALTER TABLE
Employee MODIFY "First Name" NOT NULL
ALTER TABLE Employee
MODIFY "Last Name" NOT NULL
ALTER TABLE Employee
ADD CONSTRAINT "Employee pk" PRIMARY KEY ("SSN Number")
ALTER TABLE Employee
ADD CONSTRAINT "check 1" CHECK (sex IN ('M', 'F', 'm', 'f'));
ALTER TABLE Employee
MODIFY Salary DEFAULT 800
ALTER TABLE Employee
ADD CONSTRAINT "Employee fk1" FOREIGN KEY ("Supervisor SSN")
REFERENCES Employee ("SSN Number")
ON DELETE SET NULL
ALTER TABLE DEPT MODIFY "Department Name" NOT NULL
ALTER TABLE DEPT ADD CONSTRAINT "dept pk" PRIMARY KEY
("Department Number")
ALTER TABLE DEPT ADD CONSTRAINT "dept fk 1" FOREIGN KEY
("Manager SSN") REFERENCES Employee ("SSN Number")
ON DELETE SET NULL
ALTER TABLE Project MODIFY "Project Name" NOT NULL
ALTER TABLE Project ADD CONSTRAINT "project pk" PRIMARY KEY
("Project Number")
ALTER TABLE Project ADD CONSTRAINT "project fk1" FOREIGN KEY
("Department Number") REFERENCES DEPT ("Department Number")
ON DELETE SET NULL
NAME- AASHAY KUMAR REG.NO- 20BCE2180
ALTER TABLE Employee
ADD CONSTRAINT "employee fk2" FOREIGN KEY ("Department Number")
REFERENCES DEPT ("Department Number")
ON DELETE CASCADE
NAME- AASHAY KUMAR REG.NO- 20BCE2180
II. Execute the following Query on the Db to display and discuss the integrity constraints violated
by any of the following operations
1. Insert ('Robert', 'F', 'Scott', '235', '21-JUN-1990', 'Bangalore', M, 58000, '100', 1 ) into
EMPLOYEE.
insert into EMPLOYEE values ('Robert', 'F', 'Scott', '235', '21-JUN-1990', 'Bangalore',
M, 58000, '100', 1 )
2. Insert ( 'ProjectF', null, 'Chennai', 3 ) into Project.
insert into Project values ('ProjectF', NULL, 'Chennai', 3)
3. Insert ( 'ProjectF', 1234, 'Chennai', 4 ) into Project.
INSERT INTO Project
VALUES ('ProjectF', 1234, 'Chennai', 4)
III Alter the tables to
1. Drop Foreign key defined on SuperSSN and add it using Alter table command.
alter table Employee
drop constraint "Employee fk1"
NAME- AASHAY KUMAR REG.NO- 20BCE2180
2. Make name of Project as Unique and sex of employee as not null.
alter table Project add constraint "Project uq1" UNIQUE ("Project Name")
alter table Employee modify Sex not null
3. In the copy table add the columns door no, street, city, State, Continent.
4. Make salary of employee to accept real values.
alter table Employee modify Salary NUMBER
NAME- AASHAY KUMAR REG.NO- 20BCE2180
Exercise: III
Operators and Functions
Aim: To understand different operators and types of function in SQL
1. Find the employee names whose salary lies in the range between 30000 and 70000.
select "First Name","Mid Name" ,"Last Name" , Salary from Employee where Salary >=
30000 and Salary <= 70000
2.Find the employees who have no supervisor.
select "First Name","Mid Name","Last Name" from employee where "Supervisor SSN" is
null
3.Display the bdate of all employee s in the format ‘DDthMonthYYYY’.
select to_char(Birthday, 'DDth Month YYYY') Birthday from Employee
NAME- AASHAY KUMAR REG.NO- 20BCE2180
4. Display the employee names whose bdate is on or before 1978.
select "First Name", "Mid Name", "Last Name" from Employee where Birthday <= date
'1978-12-31'
5. Display the department name that starts with ’M’.
select "Department Name" from DEPT where "Department Name" like'm%'
6. Display the department names’ that ends with ‘E’.
select "Department Name" from DEPT where "Department Name" like'%e'
NAME- AASHAY KUMAR REG.NO- 20BCE2180
7. Display the names of all the employees having supervisor with any of the following SSN
123, 124.
select "First Name","Mid Name","Last Name" from Employee where " SSN Number" =
123 or "SSN Number" = 124
8. Display all the department names in upper case and lower case.
select upper(“Department Name”) from DEPT
select lower(“Department Name”)from DEPT
9. Display the first four characters and last four of the department names using substring
function.
select SUBSTR("Department Name", 1, 4) as "First fOUR", SUBSTR("Department Name",
-4) as "Last four" from DEPT
NAME- AASHAY KUMAR REG.NO- 20BCE2180
10. Display the substring of the Address (starting from 5th position to 11 th position) of all
employees.
select SUBSTR(Address, 5, 7) as address from Employee
11. Display the Mgrstartdate on adding three months to it.
select ADD_MONTHS(ManagerStartDate, 3) AS MgrStartdate from DEPT
12. Display the age of all the employees rounded to two digits.
select ROUND((SYSDATE - Birthday)/365, 2) AS age FROM Employee
NAME- AASHAY KUMAR REG.NO- 20BCE2180
13. Find the last day and next day of the month in which each manager has joined.
select LAST_DAY(ManagerStartDate) as NEXT_DAY, Managerstartdate + 1 AS
LAST_DAY FROM DEPT
14. Print a substring from the string ‘Harini’.
select substr('Harini', 2, 4) as substring FROM DUAL
15. Replace the string ‘ni’ from ‘Harini’ by ‘sh’.
select replace('Harini', 'ni', 'sh') from DUAL
NAME- AASHAY KUMAR REG.NO- 20BCE2180
16. Print the length of all the department names.
select LENGTH("Department Name") From Department
17. Display the date after 10 months from current date.
SELECT ADD_MONTHS(SYSDATE, 10) FROM DUAL
18. Display the next occurrence of Friday in this month.
select NEXT_DAY(SYSDATE, 'Friday') FROM DUAL
where NEXT_DAY(SYSDATE, 'Friday') <= date '2021-08-29'
19. Display the project location padded with **** on left side.
select LPAD("Project Location", LENGTH("Project Location") + 4, '****') from Project
NAME- AASHAY KUMAR REG.NO- 20BCE2180