0% found this document useful (0 votes)
28 views

21bce0400 Ass1

This document contains the output of various SQL queries performed by a student on database tables like Employee, Department, Project, etc. as part of an assessment exercise. It includes queries to create tables, insert data, display data based on certain conditions, modify table structures by adding constraints and columns. Various integrity constraints added to the tables are also tested by executing insert queries that violate them. In the end, additional tables like Dept_Locations, Works_On, Dependent are created and sample data is inserted.

Uploaded by

Priyanshu Goutam
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)
28 views

21bce0400 Ass1

This document contains the output of various SQL queries performed by a student on database tables like Employee, Department, Project, etc. as part of an assessment exercise. It includes queries to create tables, insert data, display data based on certain conditions, modify table structures by adding constraints and columns. Various integrity constraints added to the tables are also tested by executing insert queries that violate them. In the end, additional tables like Dept_Locations, Works_On, Dependent are created and sample data is inserted.

Uploaded by

Priyanshu Goutam
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/ 26

DBMS LAB

ASSESSMENT -1
PRIANSHU GOUTAM
21BCE0400
Slot: L29 + L30

EXERCISE 1

Query
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));

OUTPUT
QUERY
CREATE TABLE Project(Project_Name VARCHAR(15),Project_Number
NUMBER(5),Project_Location VARCHAR(15),Department_Number NUMBER(5));

OUTPUT
QUERY
CREATE TABLE Department(Department_Name VARCHAR(15),Department_Number NUMBER(5),ManagerSSN

CHAR(9),ManagerStartDate DATE);

OUTPUT:

QUESTION 1
Insert the data given above in both employee, department and project tables
QUERY
EMPLOYEE TABLE:
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',NULL,'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-45','638
Voss, Housten, TX ','M',40000, '554433221' , 5);
INSERT INTO Employee VALUES('Jennifer','S','Wallace','987654321','19-JUN-
31','291 Berry, Bellaire,
TX','F',43000, '554433221', 4);
INSERT INTO Employee VALUES('John','B','Smith','123456789','9-JAN-55','731
Fondern,
Housten, 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,Houstan, TX ','F',25000, '333445555' , 5);
INSERT INTO Employee VALUES('James','E','Borg','888665555','10-NOV-27','450
Stone,Houstan, TX ','M',55000, '543216789' , 1);
INSERT INTO Employee VALUES('Alicia','J','Zerlaya','999887777','19-JUL-58','3321
Castle, Spring, TX ','F',25000, '987654321' , 4);
INSERT INTO Employee VALUES('Ahmad','V','Jabbar','987987987','29-MAR-59','980
Dallas,
Houstan, TX','M',25000, '987654321' , 4);
OUTPUT:

PROJECT TABLE:
QUERY
INSERT INTO Project VALUES('ProjectA',3388,'Housten',1);
INSERT INTO Project VALUES('ProjectB',1945,'Salt Lake City',3);
INSERT INTO Project VALUES('ProjectC',6688,'Housten',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);
OUTPUT:

DEPARTMENT TABLE:
QUERY
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');
OUTPUT

QUESTION 2
Display all the employees’ information.
QUERY
SELECT *FROM Employee;
Output:

QUESTION 3
Display Employee name along with his SSN and Supervisor SSN.

QUERY
SELECT First_Name || ' ' ||Mid_Name||' '|| Last_Name AS Employee_Name, SSN_Number,
Supervisor_SSN

FROM Employee;

OUTPUT

QUESTION 4:
Display the employee names whose bdate is ’29-MAR-1959’
QUERY
SELECT First_Name || ' ' || Last_Name AS Employee_Name

FROM Employee

WHERE Birthday = '29-MAR-1959';


OUTPUT

QUESTION 5
Display salary of the employees without duplications.
QUERY
SELECT DISTINCT Salary

FROM Employee;

OUTPUT

QUESTION 6
Display the MgrSSN, MgrStartDate of the manager of ‘Finance’ department

QUERY
SELECT ManagerSSN,ManagerStartDate

FROM Department

WHERE Department_Name = 'Finance';

OUTPUT
QUESTION 7
Modify the department number of an employee having fname as ‘Joyce’ to 5

QUERY
UPDATE Employee

SET Department_Number = 5

WHERE First_Name = 'Joyce';

OUTPUT

QUESTION 8
Alter Table department add column DepartmentPhoneNum of NUMBER data type and insert values into
this column only.

QUERY
alter table Department add DepartmentPhoneNum number(10)

UPDATE Department
SET DepartmentPhoneNum = 123456789;

OUTPUT

QUESTION 9
Alter table orders modify the size of DepartmentPhoneNum.

QUERY
ALTER TABLE Department

MODIFY DepartmentPhoneNum NUMBER(15);

OUTPUT

QUESTION 10
Modify the field name DepartmentPhoneNum of departments table to PhNo.

QUERY
alter table Department rename column DepartmentPhoneNum to PhNo

OUTPUT
QUESTION 11
Rename Table Department as DEPT.

QUERY
alter table Department rename to DEPT

OUTPUT

QUESTION 12
Alter Table department remove column DepartmentPhoneNum.

QUERY
alter table Dept drop column DepartmentPhoneNum

OUTPUT
Reason:
Since the column name was changed from DepartmentPhoneNum to phno we
can’t delete columnt DepartmentPhoneNum as it doesn’t exist.

EXERCISE 2

QUERY
Employee Table alteration:
ALTER TABLE Employee MODIFY First_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 ADD CONSTRAINT Employee_fk1 FOREIGN KEY (Supervisor_SSN) REFERENCES
Employee (SSN_Number) ON DELETE SET NULL;

Department Table alteration


ALTER TABLE DEPT MODIFY Department_Name NOT NULL

ALTER TABLE DEPT ADD CONSTRAINT dept_fk_1 FOREIGN KEY(ManagerSSN) REFERENCES Employee
(SSN_Number) ON DELETE SET NULL

Project Table alteration


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

ALTER TABLE Employee ADD CONSTRAINT employee_fk2 FOREIGN KEY


(Department_Number)REFERENCES DEPT (Department_Number)

ALTER TABLE Employee ADD CONSTRAINT employee_fk2 FOREIGN KEY


(Department_Number)REFERENCES DEPT (Department_Number) ON DELETE CASCADE
OUTPUT

DEPT_LOCATIONs TABLE
QUERY
CREATE TABLE Dept_locations(DepNo NUMBER(5) REFERENCES
DEPT(Department_Number),D_Location VARCHAR(15));
OUTPUT

WORKS_ON TABLE
QUERY
CREATE TABLE Works_On (
ESSN CHAR(9) REFERENCES Employee(SSN_Number) ON DELETE CASCADE,
Pno INT REFERENCES Project(Project_Number),
Hours DECIMAL(3,1) NOT NULL
);
OUTPUT

DEPENDENT TABLE
QUERY
CREATE TABLE DEPENDENT( EMPLOYEE CHAR(9), DEPENDENT_NAME VARCHAR(15), SEX CHAR(1) CHECK
(SEX IN ('M','F','m','f')), BIRTHDAY DATE, RELATIONSHIP VARCHAR(8) );
ALTER TABLE DEPENDENT ADD CONSTRAINT dependent_fk1 FOREIGN KEY ("EMPLOYEE") REFERENCES EMPLOYEE
("SSN_NUMBER") ON DELETE CASCADE;

OUTPUT

INSERTION OF VALUES
DEPT_LOCATIONS
QUERY: -
INSERT INTO DEPT_LOCATIONS VALUES (1,'Houston');

INSERT INTO DEPT_LOCATIONS VALUES (1,'Chicago');

INSERT INTO DEPT_LOCATIONS VALUES (2,'New York');

INSERT INTO DEPT_LOCATIONS VALUES(2,'San Francisco');

INSERT INTO DEPT_LOCATIONS VALUES (3,'Salt Lake City');

INSERT INTO DEPT_LOCATIONS VALUES(4,'Stafford');

INSERT INTO DEPT_LOCATIONS VALUES(4,'Bellaire');

INSERT INTO DEPT_LOCATIONS VALUES(5,'Sugarland');

INSERT INTO DEPT_LOCATIONS VALUES (5,'Houston');

OUTPUT
DEPENDENT
QUERY: -
INSERT INTO DEPENDENT VALUES('333445555','Alice','F','05-APR-76','Daughter');

INSERT INTO DEPENDENT VALUES ('333445555','Theodore','M','25-OCT-73','Son');

INSERT INTO DEPENDENT VALUES ('333445555','Joy','F','03-MAY-48','Spouse');

INSERT INTO DEPENDENT VALUES ('987654321','Abner','M','29-FEB-32','Spouse');

INSERT INTO DEPENDENT VALUES ('123456789','Alice','F','31-DEC-78','Daughter');

INSERT INTO DEPENDENT VALUES ('123456789','Elizabeth','F','05-MAY-57','Spouse');

OUTPUT: -

WORKS_ON
QUERY: -
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);

OUTPUT: -
Execute the following Query on the DB to display and discuss the integrity
constraints
violated by any of the following operations.
QUESTION 1
Insert ('Robert', 'F', 'Scott', '943775543', '21-JUN-42', '2365 Newcastle Rd,
Bellaire, TX', M,
58000, '888665555', 1 ) into EMPLOYEE.
QUERY: -
INSERT INTO EMPLOYEE VALUES ('Robert', 'F', 'Scott', '943775543', '21-JUN-42',
'2365 Newcastle Rd, Bellaire, TX', M, 58000, '888665555', 1 );
OUTPUT:
Reason:
Given constraint that gender should be from ‘m’ or ‘M’ or ‘f’ or ‘F’
Where M doesn’t belong to given constraint.
2) Insert ( '677678989', null, '40.0' ) into WORKS_ON. QUERY: - INSERT INTO
WORKS_ON VALUES ( '677678989', null, '40.0' );
QUERY:
INSERT INTO WORKS_ON VALUES ( '677678989', null, '40.0' );

OUTPUT:

REASON:

Since PROJECT_NAME has not null constraint we can’t give null value

3) Insert ( '453453453', 'John', M, '12-DEC-60', 'SPOUSE' ) into DEPENDENT


QUERY: -
INSERT INTO DEPENDENT VALUES ( '453453453', 'John', M, '12-DEC-60', 'SPOUSE' );
OUTPUT: -

REASON:
Gender is a character and it should be from ‘m’ ,’f’, ‘M’,’m’
In the query we are giving M which is not allowed
4) Delete the WORKS_ON tuples with ESSN= '333445555'.
QUERY: -
DELETE FROM WORKS_ON WHERE ESSN = '333445555';
OUTPUT: -

5) Modify the MGRSSN and MGRSTARTDATE of the DEPARTMENT


tuple with DNUMBER=5 to '123456789' and '01-OCT-88',
respectively.

QUERY: -
UPDATE DEPT SET MANAGERSSN = '123456789', MANAGERSTARTDATE = '01- OCT-88' WHERE
DEPARTMENT_NUMBER = 5;
OUTPUT: -

6) Add Foreign Keys using Alter Table [if not done earlier].
ALREADY ADDED

7) Drop Foreign key defined on SuperSSN and add it using


Alter table command.
QUERY: -

ALTER TABLE EMPLOYEE DROP CONSTRAINT employee_fk1;


OUTPUT: -
ALTER TABLE EMPLOYEE ADD CONSTRAINT "employee fk1" FOREIGN
KEY("SUPERVISOR_SSN") REFERENCES employee ("SSN_NUMBER")
ON DELETE SET NULL;

8) Make name of Project as Unique and sex of employee as not


null.QUERY: -
ALTER TABLE PROJECT ADD CONSTRAINT "Project uq1" UNIQUE ("PROJECT_NAME");
OUTPUT: -

ALTER TABLE EMPLOYEE MODIFY SEX NOT NULL;

9) Make Address as a new type containing door no, street,


city, State, Continent.
QUERY: -
ALTER TABLE EMPLOYEE ADD DOOR_NO VARCHAR(5);
ALTER TABLE EMPLOYEE ADD STREET VARCHAR(20);
ALTER TABLE EMPLOYEE ADD CITY VARCHAR(20);
ALTER TABLE EMPLOYEE ADD STATE VARCHAR(20);
ALTER TABLE EMPLOYEE ADD CONTINENT VARCHAR(20);
OUTPUT: - COMMON OUTPUT

10) Make salary of employee to accept


real values.
QUERY: -
ALTER TABLE EMPLOYEE MODIFY SALARY NUMBER ;
OUTPUT: -
EXERCISE -3

CREATION OF TABLES
QUERY:
CREATE TABLE physicians ( employeeid CHAR(2) PRIMARY KEY, name VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL, ssn VARCHAR(9) NOT NULL);

OUTPUT:

INSERTION OF VALUES:

QUERY:

INSERT INTO physicians VALUES ('E1', 'Dr. PRIANSHU', 'DENTIST', '012345678');

INSERT INTO physicians (employeeid, name, position, ssn) VALUES ('P2', 'Dr. dibyanshu', 'Surgeon',
'929292');

INSERT INTO physicians (employeeid, name, position, ssn) VALUES ('P3', 'Dr. ragini', 'Pediatrician',
'123456782'); INSERT INTO physicians (employeeid, name, position, ssn) VALUES ('P4', 'Dr. harshitha',
'Cardiologist', '123456783');

INSERT INTO physicians (employeeid, name, position, ssn) VALUES ('P5', 'Dr. girish', 'Oncologist',
'123456784');

OUTPUT:
For
Department
Query
CREATE TABLE Department1 ( departmentid CHAR(2) PRIMARY KEY, name VARCHAR(50) NOT
NULL,head CHAR(2) NOT NULL,FOREIGN KEY (head) REFERENCES physicians(employeeid) );

INSERTION OF VALUES:

INSERT INTO department1 (departmentid, name,


head)VALUES ('D1', 'Neurology', 'P1');
INSERT INTO department1 (departmentid, name,
head)VALUES ('D2', ' Cardiology ', 'P2');
INSERT INTO department1 (departmentid, name,
head)VALUES ('D3', 'Orthopedics', 'P3');
INSERT INTO department1 (departmentid, name,
head)VALUES ('D4', 'Ophthalmology', 'P4');
INSERT INTO department1 (departmentid, name,
head)VALUES ('D5', 'Gastroenterology', 'P5');
OUTPUT:

For
Physician_Department
Query

CREATE TABLE physician_department (


physician_id CHAR(2) REFERENCES physicians(employeeid),
department_id CHAR(2) REFERENCES
department1(departmentid),primary_affiliation CHAR(5),
PRIMARY KEY (physician_id, department_id)
);
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P1', 'D1','TRUE');
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P2', 'D2','NULL');
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P3', 'D3','NULL');
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P4', 'D4','FALSE');
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P5', 'D5','TRUE');
INSERT INTO physician_department (physician_id, department_id, primary_affiliation)
VALUES ('P6', 'D6','NULL');

OUTPUT:

For Procedure Query

CREATE TABLE Procedure ( QUERY CHAR(3) PRIMARY KEY, name VARCHAR(50) NOT NULL, cost
DECIMAL(10, 2) NOT NULL);

INSERT INTO procedure VALUES ('PY1', 'Physiotherapy',4000);

INSERT INTO procedure VALUES ('GS1', 'Gas',3000);

INSERT INTO procedure VALUES ('PR1', 'Catheterization', 5000);

INSERT INTO procedure VALUES ('XR1', 'XRAY', 2000);

INSERT INTO procedure VALUES ('MR1', 'MRI', 600);

OUTPUT:
For Patient Query

CREATE TABLE patient (

ssn VARCHAR(9) PRIMARY KEY, name VARCHAR(50) NOT NULL, address VARCHAR(100) NOT
NULL, phone VARCHAR(20) NOT NULL,

insuranceid VARCHAR(20) NOT NULL, pcp CHAR(2),

FOREIGN KEY (pcp) REFERENCES physician(employeeid)

);

INSERT INTO patient VALUES ('S1', 'RAM', 'XYZ AREA ', '323-2344', 'I1', 'P1');

INSERT INTO patient VALUES ('S2', 'SHYAM', 'ABC AREA ', '393992-33', 'I2', 'P2');

INSERT INTO patient VALUES ('S3', 'SUMAN', 'XXX ', '392-33', 'I3', 'P3');

INSERT INTO patient VALUES ('S4', 'AMAN', 'ORI STREET ', '2000-34', 'I4', 'P4');

INSERT INTO patient VALUES ('S5', 'MAHIM', 'ORACLE STREET ', '232-34', 'I5', 'P5');

OUTPUT:

For Nurse Query

CREATE TABLE nurse (

employeeid CHAR(2) PRIMARY KEY, name VARCHAR(50),

position VARCHAR(50), registered CHAR(3), ssn VARCHAR(9)

);

INSERT INTO nurse (employeeid, name, position, registered, ssn) VALUES ('N1', 'Nurse ISHA',
'Registered Nurse', 'YES', '921993941');

INSERT INTO nurse (employeeid, name, position, registered, ssn) VALUES ('N2', 'Nurse RAMIYA',
'Registered Nurse', 'YES', '921293941');

INSERT INTO nurse (employeeid, name, position, registered, ssn) VALUES ('N3', 'Nurse SIYA',
'Registered Nurse', 'YES', '922293941');

INSERT INTO nurse (employeeid, name, position, registered, ssn) VALUES ('N4', 'Nurse RIYA',
'Registered Nurse', 'YES', '922233341');
INSERT INTO nurse (employeeid, name, position, registered, ssn) VALUES ('N5', 'Nurse PRIYA',
'Registered Nurse', 'YES', '92223431');

OUTPUT:

For Appointment Query

CREATE TABLE appointment ( appointmentid CHAR(2) PRIMARY KEY,

patient VARCHAR(9) REFERENCES patient(ssn), prepnurse CHAR(2) REFERENCES


nurse(employeeid), physician CHAR(2) REFERENCES physicians(employeeid), appointment_date
DATE,

start_time TIMESTAMP, end_time TIMESTAMP, examinationroom VARCHAR(50)

);

INSERT INTO appointment VALUES ('A1', 'S1', 'N1', 'P1', DATE '2001-2-1', TIMESTAMP '2001-2-1
10:00:00', TIMESTAMP '2001-2-1 10:30:00', '111');

INSERT INTO appointment VALUES ('A2', 'S2', 'N2', 'P2', DATE '2001-2-3', TIMESTAMP '2001-2-3
10:30:00', TIMESTAMP '2001-2-1 10:50:00', '112');

INSERT INTO appointment VALUES ('A3', 'S3', 'N3', 'P3', DATE '2001-2-4', TIMESTAMP '2001-2-4
10:20:00', TIMESTAMP '2001-2-1 10:50:00', '221');

INSERT INTO appointment VALUES ('A5', 'S5', 'N4', 'P4', DATE '2001-2-5', TIMESTAMP '2001-2-5
10:20:00', TIMESTAMP '2001-2-1 10:50:00', '255');

INSERT INTO appointment VALUES ('A4', 'S4', 'N5', 'P5', DATE '2001-2-6', TIMESTAMP '2001-2-6
10:50:00', TIMESTAMP '2001-2-1 10:50:00', '222');

OUTPUT
1) Write a SQL statement to display all the information of all physicians.
Query
SELECT *FROM physicians;

Output:

2) List the cost of medical procedure for ‘Physiotherapy’ and ‘Gastric problem’ Query

Query:

SELECT name, cost FROM Procedure WHERE name IN ('Physiotherapy', 'Gastric');


OUTPUT:

3)Find the list of the physician who does not have any affiliations. (use NULL for not affiliated)

Query

SELECT * FROM physician_department WHERE primary_affiliation = 'NULL';

OUTPUT:
4) Write a SQL query to find the employees whose name begins with the character 'C'. Return
empid, name and ssn.

Query

SELECT employeeid, name, ssn FROM physicians

WHERE name LIKE 'C%' AND name NOT LIKE 'Dr.%';

Output:

REASON:

Since no physicians whose name starts with C.

5) Write a SQL query to find the details of the patients whose appointment time is greater
than

12.30 PM for a corresponding date.

Query

SELECT * FROM patient p JOIN appointment a ON p.ssn = a.patient WHERE a.start_time >
TO_TIMESTAMP('12:30 PM', 'HH:MI AM');

OUTPUT:

You might also like