You are on page 1of 22

School of Computing Science and

Engineering
Database Systems Lab

Reg No: 21BCE0968

Name:Sarthak Goyal

Course Code: BCSE302P

Faculty Name:Dr. Govinda K

Date Of Submission: 07-05-2023

Lab Slot: L39+L40

Semester: Fall Semester 2023-2024


Aim: To study Data De nition and Data Manipulation commands.

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: Employee


Table Name: Department

Attribute Data Type


Department Name Varchar(1
5)
Department Number(5
Number )
ManagerSSN CHAR(9)
ManageStartDate DATE

Table Name: Project

Attribute Data Type


Project Name VARCHAR(15
)

fi
Project Number NUMBER(5)
Project Location VARCHAR(15
)
Department Number NUMBER(5)

Data For Employee Table

FNam Min LName SSN BDate Address Sex Salar SuperSS Dep
e it y N No
Doug E Gilbert 5544332 09- 11 S 59 E, Salt M 8000 NULL 3
21 JUN-60 Lake City, UT 0

Joyce PAN 5432167 07- 35 S 18 E, Salt F 7000 NULL 2


89 FEB-78 Lake City, UT 0

Franki T Wong 3334455 08-DEC- 638 Voss, M 4000 5544332 5


n 55 45 Houston, TX 0 21

Jennife S Wallac 9876543 20- 291 Berry, F 4300 5544332 4


r e 21 JUN-31 Bellaire, TX 0 21

John B Smith 1234567 09- 731 Fondren, M 3000 3334455 5


89 JAN-55 Houston, TX 0 55

Rames K Naraya 6668844 15- 975 Fire Oak, M 3800 3334455 5


h n 44 SEP-52 Humble, TX 0 55

Joyce A English 4534534 31- 5631 Rice, F 2500 3334455 5


53 JUL-62 Houston, TX 0 55

James E Borg 8886655 10-NOV- 450 Stone, M 5500 5432167 1


55 27 Houston, TX 0 89

Alicia J Zelaya 9998877 19- 3321 Castle, F 2500 9876543 4


77 JUL-58 Spring, TX 0 21

Ahmad V Jabbar 9879879 29-MAR- 980 Dallas, M 2500 9876543 4


87 59 Houston, TX 0 21

Data For Department table

DName Dep MgrSSN MgrStartDat


No e
Manufacture 1 8886655 19-JUN-71
55
Administratio 2 5432167 04-JAN-99
n 89
Headquarter 3 5544332 22-SEP-55
21
Finance 4 9876543 01-JAN-85
21
Research 5 3334455 22-MAY-78
55

Data For Project

PName PNumbe Plocation Dep


r No
Project 3388 Houston 1
A
Project 1945 Salt Lake City 3
B
Project 6688 Houston 5
C
Project 2423 Bellaire 4
D
Project 7745 Sugarland 5
E
Project 1566 Salt Lake City 3
F
Project 1234 New York 2
G
Project 3467 Stafford 4
H
ProjectI 4345 Chicago 1
ProjectJ 2212 San Francisco 2

Exercise-I:
1. Insert the data given above in both employee, department and project tables.
2. Display all the employees’ information.
3. Display Employee name along with his SSN and Supervisor SSN.
4. Display the employee names whose bdate is ’29-MAR-1959’.
5. Display salary of the employees without duplications.
6. Display the MgrSSN, MgrStartDate of the manager of ‘Finance’ department.
7. Modify the department number of an employee having fname as ‘Joyce’ to 5
8. Alter Table department add column DepartmentPhoneNum of NUMBER data
typeand insert values into this column only.
9. Alter table orders modify the size of DepartmentPhoneNum.
10. Modify the field name DepartmentPhoneNum of departments table to PhNo.
11. Rename Table Department as DEPT.
12. Alter Table department remove column DepartmentPhoneNum.

I nsert the data given above in both employee, department and project tables.

D isplay all the employees’ information.

D isplay Employee name along with his SSN and Supervisor SSN.

D isplay the employee names whose bdate is ’29-MAR-1959’.


R ename Table Department as DEPT.


D isplay the MgrSSN, MgrStartDate of the manager of ‘Finance’ department



Exercise: II

Aim: To know how the constraints are used to make table is consistent.

Table Name: Employee


Attribu Data Constrai
te Type nt
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

Attribu Data Constraint


te type
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 : Dept_locations

Attribute Data type Constraint


Department Number Number(5) Department (dep no) onDelete
Cascade

Department Location Varchar (15)

Table Name: Project


Attribu Data Constraint
te type
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

Table Name: Works_On


The combination of Employee SSN and Project Number must be a Primary Key
Attribute Data type Constrai
nt
Employee SSN Char (9) Foreign Key
Employee (SSN) on delete cascade
Project number INT(5) Foreign Key project ( Pnumber ) on
delete cascade
Hours Decimal (3,1) Not null

Name: Dependent
The combination of Employee SSN and Dependent Name must be a Primary
Key.
Attribute Datatype Constrai
nt
Employee Char (9) Foreign Key- Employee (SSN) on Delete
Cascade
Dependent Name Varchar(15)
Sex Char(1) Check Sex in (M,F,m,f)
Birthday Date
Relationship Varchar(8)

Data for table - Dept_Locations

Dep No D Location

1 Houston
1 Chicago
2 New York
2 San Francisco
3 Salt Lake City
4 Stafford
4 Bellaire
5 Sugarland
5 Houston

Data for Table - Works_On

ESSN Pn Hours
o
123456789 3388 32.5
123456789 1945 7.5
666884444 3388 40.0
453453453 77 20.0
453453453 22 20.0
333445555 77 10.0
333445555 6688 10.0
333445555 43 35.0
333445555 22 28.5
999887777 1 11.5
999887777 12 13.0
543216789 22 17.0
554433221 1945 21.5

Data for Table - Dependent

ESSN Dependent_nam Sex Bdate Relationship


e
3334455 Alice F 05- Daughter
55 Apr-76
3334455 Theodore M 25- Son
55 Oct-73

3334455 Joy F 03- Spouse


55 May-48
9876543 Abner M 29- Spouse
21 Feb-32
1234567 Alice F 31- Daughter
89 Dec-78
1234567 Elizabeth F 05- Spouse
89 may-57

Execute the following Query on the Db to display and discuss the integrity constraintsviolated by any of
the following operations

1. Insert ('Robert', 'F', 'Scott', '943775543', '21-JUN-42', '2365 Newcastle Rd, Bellaire,TX', M,
58000, '888665555', 1 ) into EMPLOYEE.
2. Insert ( '677678989', null, '40.0' ) into WORKS_ON.
3. Insert ( '453453453', 'John', M, '12-DEC-60', 'SPOUSE' ) into DEPENDENT
4. Delete the WORKS_ON tuples with ESSN= '333445555'.
5. Modify the MGRSSN and MGRSTARTDATE of the DEPARTMENT tuple with
DNUMBER=5 to '123456789' and '01-OCT-88', respectively.
Alter the tables to
1. Add Foreign Keys using Alter Table [if not done earlier].
2. Drop Foreign key defined on SuperSSN and add it using Alter table command.
3. Make name of Project as Unique and sex of employee as not null.
4. Make Address as a new type containing door no, street, city, State, Continent.
5. Make salary of employee to accept real values.

Creating the Table Employee:

CREATE TABLE EMPLOYE1(FIRST_NAME VARCHAR(15) CONSTRAINT


CFNAME_NN NOTNULL,
MID_NAME CHAR(2),
LAST_NAME VARCHAR(15) CONSTRAINT CLNAME_NN
NOT NULL, SSNNO CHAR(9) CONSTRAINT CSSN_PK
PRIMARY KEY, BIRTHDAYDATE,
ADDRESS VARCHAR(50),
SEX CHAR(1) CONSTRAINT CSEX_SI CHECK (SEX
IN('F','M','f','m')),SALARY NUMBER(7),
SUPERVISORSSN CHAR(9),
CONSTRAINT EMP_FK FOREIGN KEY(SUPERVISORSSN) REFERENCES
EMPLOYE1(SSNNO),DEPT_NO NUMBER(5));

Creating the Table Department:

CREATE TABLE DEPARTMENT(DNAME VARCHAR(15) CONSTRAINT CDNAME_NNNOT


NULL,
DEPT_NO NUMBER(5) CONSTRAINT CDEPT_PK PRIMARY
KEY,MANAGER_SSN CHAR(9),
CONSTRAINT DEPT_FK FOREIGN KEY(MANAGER_SSN) REFERENCES
EMPLOYE1(SSNNO),
START_DATE DATE);
ALTER TABLE EMPLOYE1 ADD CONSTRAINT CODEP_FK FOREIGN KEY(DEPT_NO) REFERENCES
DEPARTMENT(DEPT_NO);

Creating the Table Project:

CREATE TABLE PROJECT1(PROJECT_NAME VARCHAR(15) CONSTRAINT CPNAME_NN NOT NULL,


PROJECT_NO NUMBER(5) CONSTRAINT CPNO_PK PRIMARY KEY,
PRO_LOC
VARCHAR2(50),
DEPT_NO
NUMBER(5),
CONSTRAINT DEPT1_FK FOREIGN KEY(DEPT_NO) REFERENCES DEPARTMENT(DEPT_NO) ON
DELETE CASCADE);

sql: EMPLOYEE.
1.) INSERT INTO EMPLOYE1 VALUES('Robert', 'F',
'Sco ',
‘235’,’21-JUN-1990’,’Banglore’,’N’,50000,100,1);

The table department contains dept_no as primary key and there are novalues inserted into

tt

department. dept_no column is foreign key in tabledepartment ,so it shows an error that its
parent table doesn’t have this value.

Attribut Data Constraint


e Type
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
Dept no Number(5) Foreign key to dept no of department table

2.)Insert ( 'ProjectF', null, 'Chennai', 3 ) into Project.


sql:
INSERT INTO PROJECT1 VALUES( 'ProjectF', NULL, 'Chennai', 3);
Output:

13. Insert ( 'ProjectF', 1234, 'Chennai', 4 ) into Project.

sql:
INSERT INTO PROJECT1 VALUES( 'ProjectF', 1234, 'Chennai', 4 );
Output:

The table department contains dept_no as primary key.dept_no column isforeign key in table
department ,so it shows an error that its parent table doesn’t have this value.

Alter the tables to

1. Add Foreign Keys using Alter Table [if not done earlier].
sql:
ALTER TABLE EMPLOYE1 DROP CONSTRAINT EMP_FK;
2. Drop Foreign key defined on SuperSSN and add it using Alter table command.
sql:
ALTER TABLE EMPLOYE1 ADD CONSTRAINT EMP_FK FOREIGN
KEY(SUPERVISORSSN) REFERENCES EMPLOYE1(SSNNO);

3. Make name of Project as Unique and sex of employee as not null.


sql:
ALTER TABLE PROJECT1 ADD CONSTRAINT CON_UK UNIQUE(PROJECT_NAME);
ALTER TABLE EMPLOYE1 MODIFY(SEX CHAR(1) CONSTRAINT CONS_NN NOT NULL);
4. In the copy table add the columns door no, street, city, State, Continent.
sql:
CREATE TABLE COPY_TABLE AS SELECT *FROM
EMPLOYE1;ALTER TABLE COPY_TABLE
ADD(DOOR_NO VARCHAR(10),
STREET VARCHAR(10),CITY VARCHAR(10), STATE VARCHAR(10),
CONTINENT VARCHAR(10));
5. Make salary of employee to accept real values.
sql:
ALTER TABLE EMPLOYE1 MODIFY(SALARY NUMBER(7,2));

Attribute Data type Constraint


DName Varchar(15) Not Null
Dept no Number(5) Primary key
Manager SSN Char (9) Foreign key-Employee (SSN) on delete set null
Manage start date Date



THANK
YOU!

You might also like