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 First Name Mid Name Last Name SSN Number Birthday Address Sex Salary Supervisor SSN Department Number Data Type VARCHAR(15) CHAR(2) VARCHAR(15) CHAR(9) DATE VARCHAR(50) CHAR(1) NUMBER (7) CHAR(9) NUMBER (5) Table Name: Department Attribute Department Name Department Number ManagerSSN ManageStartDate Data Type Varchar(15) Number(5) CHAR(9) DATE Table Name: Project Attribute Project Name Project Number Project Location Department Number Data Type VARCHAR(15) NUMBER(5) VARCHAR(15) NUMBER(5) NAME- AASHAY KUMAR REG.NO- 20BCE2180 Data For Employee Table FName Doug Joyce Frankin Jennifer John Ramesh Mini t E T S B K LName Gilbert PAN Wong Wallace Smith Narayan SSN 123 124 125 564 678 234 BDate Address 09-JUN-1968 07-FEB-1973 08-DEC-1972 20-JUN-1983 09-JAN-1987 15-SEP-1985 Chennai Vellore Delhi Chennai Madurai Bangalore Data For Department table DName Administration Headquarter Finance IT DepNo 2 1 3 4 MgrSSN 564 678 234 123 MgrStartDate 03-Jan-2012 16-Dec-2014 18-May-2013 12-Jun-2015 Data For Project PName ProjectA ProjectB ProjectC ProjectD ProjectE PNumber 3388 1945 6688 2423 7745 Plocation Delhi Hyderabad Chennai Chennai Bangalore DepNo 1 1 2 2 3 Sex Salary M F M F M M 80000 70000 40000 43000 30000 38000 SuperSSN 123 123 124 124 DepNo 1 1 2 2 1 3 NAME- AASHAY KUMAR 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) ) REG.NO- 20BCE2180 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-Jun1983' 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 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 REG.NO- 20BCE2180 NAME- AASHAY KUMAR 14. Delete all the rows from COPYOF DEPT table. delete from COPYOFDEPT 15. Remove COPYOF DEPT table. drop table COPYOFDEPT REG.NO- 20BCE2180 NAME- AASHAY KUMAR REG.NO- 20BCE2180 Exercise: II Aim: To use constraints and formulate the tables to have consistent data. Table Name: Employee Attribute First Name Mid Name Last Name SSN Number Birthday Address Sex Salary Supervisor SSN Data Type Varchar (15) Char(2) Varchar (15) Char (9) Date Varchar (50) Char(1) Number (7) Char (9) Department number Number(5) Constraint Not Null Not Null Primary Key Sex In (M,F,m,f) Default 800 Foreign Key Employee (SSN) on delete set null Foreign key to department number of department table on delete cascade Table Name : Department Attribute Department Name Department number Manager SSN Data type Varchar(15) INT(5) Char (9) Manage start date Date Constraint Not Null Primary key Foreign key-Employee (SSN) on delete set null Table Name: Project Attribute Project Name Project number Project Location Department Number Data type Varchar2(15) Number(5) Varchar2(50) Number(5) Constraint Not Null Primary key 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