2020
PUNJAB UNIVERSITY COLLEGE OF INFORMATION
TECHNOLOGY
LAB10
DATA-BASE
Prepared for Miss. Hareem Aslam
By. Talha Mazhar Bitf19a024
TASK 1
Create the EMPLOYEESIT19 table based on the structure of the EMPLOYEES table. Include only the EMPLOYEE_ID,
FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID columns. Name the columns in your new table ID, FIRST_NAME,
LAST_NAME, SALARY, and DEPT_ID, respectively.
CREATE TABLE EMPLOYEESIT19
(ID number(5,0) NOT null,
FIRST_NAME varchar2(20),
LAST_NAME varchar2(20),
salary number(9,3),
dept_id number(4,0))
TASK 2
Add a table-level PRIMARY KEY constraint to the EMPLOYEESIT19 table on the ID column. The constraint should be named at
creation. Name the constraint my_emp_id_pk.
alter table EMPLOYEESIT19 add CONSTRAINT my_emp_id_pk PRIMARY KEY(ID);
TASK 3
Modify the EMPLOYEESIT19 table. Add a COMMISSION column of NUMBER data type, precision 2, scale 2. Add a constraint
to the commission column that ensures that a commission value is greater than zero.
alter table EMPLOYEESIT19 add (COMMISSION number(2,2), CONSTRAINT EMPLOYEESIT19_COMMISSION_CK CHECK (COMMISSION >
0));
TASK 4
Remove the commission constraint from EMPLOYEESIT19 table.
alter table EMPLOYEESIT19 drop CONSTRAINT EMPLOYEESIT19_COMMISSION_CK;
Task 5
Drop the EMPLOYEESIT19 table.
drop table EMPLOYEESIT19;
TASK 6
Create a student table with columns (s_id, s_name, s_degreeCode, s_classCode), a degree table with colums(degreeCode,
degreeName, creditHrs), a class table with columns(classCode, capacity, roomNum). In student table, add the not null,
unique and primary-key constraint on s_id, foreign-key constraint on s_degreeCode and s_classCode. Unique, not null
constraint on roomNum.
CREATE table student
(s_id number(6), s_name varchar2(30) NOT null, s_degreeCode number(5), s_classCode number(7), CONSTRAINT student_s_id_PK
PRIMARY KEY (s_id),
CONSTRAINT student_s_degreeCode_FK FOREIGN KEY (s_degreeCode) REFERENCES degree (degreeCode),
CONSTRAINT student_s_classCode_FK FOREIGN KEY (s_classCode) REFERENCES class (classCode));
CREATE table class
(classCode number(10), CAPACITY number(10), roomNum number(3) NOT null, CONSTRAINT class_classCode_PK PRIMARY KEY
(classCode), CONSTRAINT class_roomNum_UK UNIQUE (roomNum));
CREATE table degree
(degreeCode number(10), degreeName varchar2(30) NOT null, creditHrs number(1), CONSTRAINT degree_degreeCode_PK PRIMARY KEY
(degreeCode));
TASK 7
Apply the above constraints at column level, after table creation
Add the constraint on creditHrs to accept values between 1-3 only and roomNum value to accept values below 20 only.
alter table degree add CONSTRAINT degree_creditHrs_ck CHECK (creditHrs IN(3, 5));
alter table class add CONSTRAINT class_roomNum_ck CHECK (roomNum < 30);
TASK 8
Remove the constraints that are applied in question above.
alter table class drop CONSTRAINT class_roomNum_CK;
alter table degree drop CONSTRAINT degree_creditHrs_CK;
TASK 9
Display all the constraints of above tables.
SELECT CONSTRAINT_name, CONSTRAINT_type, search_condition
FROM user_CONSTRAINTS
WHERE table_name = 'class';
SELECT CONSTRAINT_name, CONSTRAINT_type, search_condition
FROM user_CONSTRAINTS
WHERE table_name = 'DEGREE';
SELECT CONSTRAINT_name, CONSTRAINT_type, search_condition
FROM user_CONSTRAINTS
WHERE table_name = 'STUDENT';
TASK 10
Add constraint on degree-name to accept names in (BSCS,BSIT,BSSE)
alter table degree add constraint degree_degreeName_CK CHECK (degreeName IN ('BSCS', 'BSIT', 'BSSE'));