Name: Vinayak Nagar Reg - No.: 21MCA0015 Subject: Database Technology Topic: Assignment-1

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

Name: Vinayak Nagar

Reg.No.: 21MCA0015
Subject: Database Technology
Topic: Assignment-1
Exercise:1
Q1)Insert the data given above in both employee, department
and project tables.
Employee Table
create table Employee(Fname varchar2(15),Mname char(2),Lname
varchar2(15),SSN char(9),birthday date,Address varchar2(50),sex char(1),Salary
number(7),Sup_SSN char(9),Dept_no number(5));

insert into Employee values('Doug','E','Gilbert','554433221','09-JUN-60','11 S 59


E,Salt Lake City, UT','M',80000,NULL,3);

insert into Employee values('Joyce','','PAN','543216789','07-FEB-78','35 S 18


E,Salt Lake City, UT','F',70000,NULL,2);

insert into Employee values('Frankin','T','Wong','333445555','08-DEC-31','638


Voss,Houston, TX','M',40000,'554433221',5);

insert into Employee values('Jennifer','S','Wallace','987654321','20-JUN-31','291


Berry,Bellaire, TX','F',43000,'554433221',4);

insert into Employee values('John','B','Smith','123456789','09-JAN-55','731


Fondren,Houston, TX','M',30000,'333445555',5);
insert into Employee values('Ramesh','K','Narayan','666884444','15-SEP-52','975
Fire Oak, Humble, TX','M',38000,'333445555',5);

insert into Employee values('Joyce','A','English','453453453','31-JUL-62','5631


Rice, Houston, TX','F',25000,'333445555',5);

insert into Employee values('James','E','Borg','888665555','10-NOV-27','450


Stone, Houston, TX','M',55000,'543216789',1);

insert into Employee values('Alicia','J','Zelaya','999887777','19-JUL-58','3321


Castel, Spring, TX','F',25000,'987654321',4);

insert into Employee values('Ahmad','V','Jabbar','987987987','29-MAR-59','980


Dallas, Houston, TX','M',25000,'987654321',4);

create table Department(Dname varchar2(15),D_No number(5),M_SSN


char(9),MSD date);

insert into Department values('Manufacture',1,'888665555','19-JUN-71');

insert into Department values('Administration',2,'543216789','04-JAN-99');

insert into Department values('Headquarter',3,'554433221','22-SEP-55');

insert into Department values('Finance',4,'987654321','01-JAN-85');

insert into Department values('Research',5,'333445555','22-MAY-78');

select * from Department;


Project Table
INSERT INTO project VALUES('ProjectA', 3388, 'Houston', 1 );

INSERT INTO project VALUES('ProjectB', 1945, 'Salt Lake City', 3 );

INSERT INTO project VALUES('ProjectC', 6688, 'Houston', 5 );

INSERT INTO project VALUES('ProjectD', 2423, 'Bellaire', 4 );

INSERT INTO project VALUES('ProjectE', 7745, 'Sugarland', 5 );

INSERT INTO project VALUES('ProjectF', 1566, 'Salt Lake City', 3 );

INSERT INTO project VALUES('ProjectG', 1234, 'New York', 2 );

INSERT INTO project VALUES('ProjectH', 3467, 'Stafford', 4 );

INSERT INTO project VALUES('ProjectI', 4345, 'Chicago', 1 );

INSERT INTO project VALUES('ProjectJ', 2212, 'San Francisco', 2 );


Q2)Display all the employees’ information.
select * from employee;
3)Display Employee name along with his SSN and Supervisor
SSN.
select Fname,Mname,Lname, SSN, Sup_SSN from Employee;
Q4)Display the employee names whose bdate is ’29-MAR-1959’.
select Fname, Mname, Lname from Employee where Birthday=’29-MAR-1959’;
Q5)Display salary of the employees without duplications.
select Distinct(Salary) from Employee;
Q6)Display the MgrSSN, MgrStartDate of the manager of
‘Finance’ department.
select M_SSN, MSD from Department where Dname=’Finance’;
Q7)Modify the department number of an employee having
fname as ‘Joyce’ to 5.
update Employee set Dept_No=5 where Fname=’Joyce’;
Q8)Alter Table department add column DepartmentPhoneNum
of NUMBER data type and insert values into this column only.
Alter table Department ADD DeptPhoneno number(10);

desc Department;

UPDATE department SET DeptPhoneno=1236547890 WHERE D_no=1;

UPDATE department SET DeptPhoneno=1235468790 WHERE D_no=2;

UPDATE department SET DeptPhoneno=9564871230 WHERE D_no=3;

UPDATE department SET DeptPhoneno=9264780132 WHERE D_no=4;

UPDATE department SET DeptPhoneno=7548796123 WHERE D_no=5;

select * from Department;


Q9)Alter table orders modify the size of DepartmentPhoneNum.
ALTER TABLE department MODIFY DeptPhoneno NUMBER(15);
Q10)Modify the field name DepartmentPhoneNum of
departments table to PhNo.
ALTER TABLE department RENAME COLUMN DeptPhoneno TO PhNo;
Q11)Rename Table Department as DEPT.
RENAME department to dept;
Q12)Alter Table department remove column
DepartmentPhoneNum.
ALTER TABLE dept DROP COLUMN phno;

Q13)Create a table COPYOFDEPT as a copy of the table DEPT.


CREATE TABLE COPYOFDEPT AS SELECT * FROM dept;
Q14)Delete all the rows from COPYOF DEPT table.
delete from COPYOFDEPT;
Q15)Remove COPYOF DEPT table.
drop table COPYOFDEPT;
EXERCISE-2
:-
ALTERING EXISTING TABLES AND ADDING CONSTRAINTS:
Employee Table:
ALTER TABLE employee MODIFY fname VARCHAR(15) NOT NULL;
ALTER TABLE employee MODIFY lname VARCHAR(15) NOT NULL;

ALTER TABLE employee MODIFY ssn CHAR(9) PRIMARY KEY;

ALTER TABLE employee MODIFY Sex CHECK(Sex IN ('M','F'));

ALTER TABLE employee MODIFY Salary Number(7) DEFAULT 800;

ALTER TABLE employee ADD FOREIGN KEY(dno) REFERENCES dept (dno) ON


DELETE CASCADE;
Dept Table:
ALTER TABLE dept MODIFY dname varchar(20) NOT NULL;

ALTER TABLE dept MODIFY dno Number(10) PRIMARY KEY NOT NULL;
Project table:
ALTER TABLE project MODIFY pname varchar(15) NOT NULL;

ALTER TABLE project MODIFY pno Number(5) PRIMARY KEY;

ALTER TABLE project ADD FOREIGN KEY(d_no)REFERENCES dept(d_no) ON


DELETE SET NULL;
Dept_loc table:
Create table dept_loc(D_no number(5),Dloc varchar2(15),FOREIGN KEY(D_no)
REFERENCES dept(D_no) on DELETE CASCADE);
INSERT INTO dept_loc VALUES(1,’Houston’);

INSERT INTO dept_loc VALUES (1,’Chicago’);

INSERT INTO dept_loc VALUES ( 2,’New York’);

INSERT INTO dept_loc VALUES (2,’San Francisco ’);

INSERT INTO dept_loc VALUES (3,’Salt Lake City’);

INSERT INTO dept_loc VALUES ( 4,’Stafford’);

INSERT INTO dept_loc VALUES ( 4,’Bellaire’);

INSERT INTO dept_loc VALUES ( 5,’Sugarland’);

INSERT INTO dept_loc VALUES ( 5,’Houston’);


Works_on table:
Create table works_on(SSN char(9),Pno number(5),hours number(3,1),FOREIGN
KEY (SSN) REFERENCES Employee(SSN) on DELETE CASCADE,FOREIGN KEY(Pno)
REFERENCES Project(Pno) on DELETE CASCADE);

INSERT INTO works_on VALUES(123456789,3388,32.5);

INSERT INTO works_on VALUES(123456789,1945,7.5);

INSERT INTO works_on VALUES(666884444,3388,40.0);

INSERT INTO works_on VALUES(453453453,7745,20.0);

INSERT INTO works_on VALUES(453453453,2212,20.0);

INSERT INTO works_on VALUES(333445555,7745,10.0);

INSERT INTO works_on VALUES(333445555,6688,10.0);

INSERT INTO works_on VALUES(333445555,4345,35.0);


INSERT INTO works_on VALUES(333445555,2212,28.5);

INSERT INTO works_on VALUES(999887777,1566,11.5);

INSERT INTO works_on VALUES(999887777,1234,13.0);

INSERT INTO works_on VALUES(543216789,2212,17.0);

INSERT INTO works_on VALUES(554433221,1945,21.5);


Dependant table:
Create table dependant(emp char(9),depd_name varchar2(15),sex char(1) CHECK
(sex in('M','F','m','F')),Birthday date,rel varchar2(8));

INSERT INTO dependant VALUES( 333445555, ’Alice’, ’F’, ’05-Apr-76’, ’Daughter’);

INSERT INTO dependant VALUES( 333445555, 'Theodore', 'M', '25-Oct-73', 'Son');

INSERT INTO dependant VALUES(333445555, 'Joy', 'F', '03-May-48', 'Spouse');

INSERT INTO dependant VALUES(987654321, 'Abner', 'M', '29-Feb-32', 'Spouse' );

INSERT INTO dependant VALUES(123456789, 'Alice', 'F', '31-Dec-78', 'Daughter' );

INSERT INTO dependant VALUES( 123456789, 'Elizabeth', 'F', '05-may-57',


'Spouse');
:-
Execute the following Query on the Db to display and discuss the
integrity constraints violated by any of the following operations
Q1)Insert ('Robert', 'F', 'Scott', '943775543', '21-JUN-42', '2365
Newcastle Rd, Bellaire, TX', M, 58000, '888665555', 1 ) into
EMPLOYEE.
insert INTO employee VALUES('Robert','F','Scott','943775543','21-JUN-42','2365
Newcastle Rd, Bellaire, TX','M',58000,'888665555',1);
Q2)Insert ( '677678989', null, '40.0' ) into WORKS_ON.
Insert into works_on values( 677678989, null,40.0);

Value doesn’t exist in parent table.


Q3)Insert ( '453453453', 'John', M, '12-DEC-60', 'SPOUSE' ) into
DEPENDENT
insert into dependant values('453453453','John','M','12-DEC-60','Spouse');
Q4)Delete the WORKS_ON tuples with ESSN= '333445555'.
delete from works_on where SSN='333445555';
Q5)Modify the MGRSSN and MGRSTARTDATE of the
DEPARTMENT tuple with DNUMBER=5 to '123456789' and '01-
OCT-88', respectively.
UPDATE dept set M_SSN='123456789',MSD='01-OCT-88'where d_no=5;
:-
Alter the tables to
Q1)Add Foreign Keys using Alter Table [if not done earlier].
ALTER TABLE dept ADD CONSTRAINT m_ssn FOREIGN KEY(m_ssn) REFERENCES
employee(ssn) ON DELETE SET NULL;

ALTER TABLE employee ADD CONSTRAINT sup_ssn FOREIGN KEY(sup_ssn)


REFERENCES employee(ssn) ON DELETE
Q2)Drop Foreign key defined on SuperSSN and add it using Alter
table command.
ALTER TABLE employee DROP CONSTRAINT sup_ssn;

ALTER TABLE employee ADD CONSTRAINT sup_ssn FOREIGN KEY(sup_ssn)


REFERENCES employee(ssn) ON DELETE SET NULL;
Q3)Make name of Project as Unique and sex of employee as not
null.
ALTER TABLE project ADD CONSTRAINT pn UNIQUE(pname);

ALTER TABLE employee MODIFY(sex char(1) CONSTRAINT sex NOT NULL);


Q4)Make Address as a new type containing door no, street, city,
State, Continent.
CREATE TABLE cemp as SELECT * FROM employee;

ALTER TABLE cemp add door_no NUMBER(4);

ALTER TABLE cemp add street number(15);

ALTER TABLE cemp add city number(15);

ALTER TABLE cemp add State number(15);

ALTER TABLE cemp add Continent number(15);

desc cemp;
Q5)Make salary of employee to accept real values.
Alter table cemp modify Salary(7,4);
EXERCISE-3
Operators and Functions
:-
Aim: To understand different operators and types of function in
SQL
Execute the following queries based on the schema specified in
exercise 1
Q1)Find the employee names having salary greater than
Rs.25000.
select Fname,Mname,Lname from Employee where Salary>25000;
Q2)Find the employee names whose salary lies in the range
between 30000 and 70000.
select Fname,Mname,Lname from Employee where Salary between 30000 and
70000;
Q3)Find the employees who have no supervisor.
select Fname,Mname,Lname from Employee where sup_SSN IS NULL;
Q4)Display the bdate of all employee s in the format
‘DDthMonthYYYY’.
select Fname,Birthday from employee;
Q5)Display the employee names whose bdate is on or before
1978.
select Fname,Mname,Lname from Employee where Birthday<'01-JAN-78';
Q6)Display the employee names having ‘salt lake’ in their
address.
select Fname,Mname,Lname,Address from Employee where Address like '%Salt
Lake%';
Q7)Display the department name that starts with ’M’.
select Dname from Dept where Dname like 'M%';
Q8)Display the department names’ that ends with ‘E’.
select Dname from Dept where Dname like '%e';
Q9)Display the names of all the employees having supervisor
with any of the following SSN 554433221, 333445555.
select fname,sup_SSN from employee where sup_SSN
IN(554433221,333445555);
Q10)Display all the department names in upper case and lower
case.
select UPPER(Dname),LOWER(Dname) from Dept;

Q11)Display the first four characters and last four of the


department names using ltrim and rtrim.
Q12)Display the substring of the Address (starting from 5th
position to 11 th position) of all employees.
select SUBSTR(Address,5,11) from Employee;
Q13)Display the Mgrstartdate on adding three months to it.
select ADD_MONTHS(MSD,3) from Dept;
Q14)Display the age of all the employees rounded to two digits.
select ROUND((MONTHS_BETWEEN(SYSDATE,Birthday)/12),2) from Employee;
Q15)Find the last day and next day of the month in which each
manager has joined.
select MSD,LAST_DAY(MSD) from dept;

select MSD,MSD+1 as NEXT_DAY from Dept;


Q16)Print a substring from the string ‘Harini’.
select SUBSTR('Harini',2,3) from dual;
Q17)Replace the string ‘ni’ from ‘Harini’ by ‘sh’.
select REPLACE('HARINI','NI','SH') from dual;
Q18)Print the length of all the department names.
select LENGTH(Dname) from Dept;
Q19)Print the system date in the format 25 th May 2007.
select TO_CHAR(SYSDATE,'DDth fmmonth yyyy') from dual;
Q20)Display the date after 10 months from current date.
select ADD_MONTHS(SYSDATE, 10) from dual;
Q21)Display the next occurrence of Friday in this month.
select NEXT_DAY(SYSDATE,'Friday') from dual;
Q22)Convert SSN of employee to Number format and display.
alter table employee modify Sup_SSN number(9);
Q23)Display the project location padded with **** on left side.
select LPAD(plocation,LENGTH(plocation)+4,'****') from Project;
Q24)Remove the word ‘Project’ from the project name and
display it.
select SUBSTR(Pname,8,1) from Project;
Q25)Select the SSN of the employee whose dependent name is
either Michaelor Abner.
select m_ssn from dept where Dname like 'Michael' or Dname like 'Abner';
EXERCISE-4
Group Functions
Q1)How many different departments are there in the ‘employee’
table
select count(Distinct(D_no)) from employee;
Q2)For each department display the minimum and maximum
employee salaries
select D_no, min(Salary),Max(salary) from employee group by D_no;
Q3)Print the average annual salary.
select avg(salary*12) from employee;
Q4)Count the number of employees over 30 age.
SELECT count(ssn) FROM employee where (abs(extract(year FROM sysdate)-
(extract(year FROM birthday)))>30);
Q6)Display the department name which contains more than 30
employees.
SELECT Dname FROM dept, employee where dept.d_no=employee.d_no group
BY dname having count(employee.ssn)>30;
Q7)Calculate the average salary of employees by department
and age
SELECT distinct d_no,months_between(sysdate,birthday)/12,avg(salary) FROM
employee group BY d_no,months_between(sysdate,birthday)/12;
Q8)Count separately the number of employees in the finance
and research department.
SELECT count(ssn) FROM employee where d_no =(SELECT d_no FROM dept
where dname='Finance');

SELECT count(ssn) FROM employee where d_no =(SELECT d_no FROM dept
where dname='Research');
Q9)List out the employees based on their seniority.
SELECT fname,lname,months_between(sysdate,birthday)/12 FROM employee
order BY months_between(sysdate,birthday)/12 desc;
Q10)List out the employees who works in ‘manufacture’
department group by first name
SELECT fname,lname,ssn FROM employee where d_no=(SELECT d_no FROM dept
where Dname='Manufacture');

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy