Uploaded by Subhodeep Dey

20BCE2618 VL2021220104242 AST01 2 converted.pdf

advertisement
DBMS: DIGITAL ASSIGNMENT 1:
EXERCISE 1:
CREATING TABLES:
EMPLOYEE TABLE:
CREATE TABLE EMPLOYEE (
SSN CHAR(9),
NUMBER(7),
FName VARCHAR(15),
Minit CHAR(2),
LName VARCHAR(15),
BDate DATE,
Address VARCHAR2(50),
Sex CHAR(1),
Salary
SuperSSN CHAR(9),
DepNo NUMBER(5))
Table created.
DEPARTMENT TABLE:
CREATE TABLE DEPARTMENT (
DName VARCHAR2(15),
DepNo NUMBER(5),
MgrSSN CHAR(9),
MgrStartDate DATE)
Table created.
PROJECT TABLE:
CREATE TABLE PROJECT(
VARCHAR2(15),
Table created.
PName VARCHAR2(15),
DepNo NUMBER(5))
PNumber NUMBER(5),
PLoaction
Exercise-I:
1. Insert the data given above in both employee, department and project tables.
EMPLOYEE TABLE:
INSERT INTO EMPLOYEE VALUES('Doug', 'E', 'Gilbert',
E, Salt Lake City, UT', 'M', 80000, NULL , 3)
554433221,
'09-JUN-60',
'11 S 59
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Joyce', NULL,
'PAN',
Salt Lake City, UT',
'F', 70000, NULL, 2)
543216789, '07-FEB-78',
'35 S 18 E,
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Frankin', 'T',
Houston, TX', 'M', 40000, 554433221, 5)
'Wong',
333445555,
'08-DEC-45',
'638 Voss,
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Jennifer',
'S', 'Wallace',
Berry, Bellaire, TX', 'F', 43000,
554433221, 4)
987654321,
'20-JUN-31',
'291
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('John', 'B', 'Smith',
Houston, TX', 'M', 30000, 333445555, 5)
123456789,
'09-JAN-55',
'731 Fondren,
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Ramesh',
Fire Oak, Humble, TX',
'M',
38000,
'K',
'Narayan',
333445555,
666884444,
'15-SEP-52',
'975
5)
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Joyce',
'A', 'English',
Rice, Houston, TX',
'F', 25000, 333445555,
5)
453453453,
'31-JUL-62',
'5631
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('James',
'E', 'Borg',
Houston, TX', 'M', 55000, 543216789, 1)
888665555,
'10-NOV-27',
'450 Stone,
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Alicia',
'J', 'Zelaya',
Castle, Spring, TX',
'F', 25000, 987654321,
4)
999887777,
'19-JUL-58',
'3321
1 row(s) inserted.
INSERT INTO EMPLOYEE VALUES('Ahmad',
'V', 'Jabbar',
Dallas, Houston, TX', 'M', 25000,
987654321, 4)
1 row(s) inserted.
987987987,
'29-MAR-59',
'980
SELECT * FROM EMPLOYEE
DEPARTMENT TABLE:
INSERT INTO DEPARTMENT VALUES('Manufacture',
1,
888665555,
'19-JUN-71')
1 row(s) inserted.
INSERT INTO DEPARTMENT VALUES('Administration',
2,
543216789,
'04-JAN-99')
1 row(s) inserted.
INSERT INTO DEPARTMENT VALUES('Headquarter',
3,
554433221,
'22-SEP-55')
1 row(s) inserted.
INSERT INTO DEPARTMENT VALUES('Finance',
4,
987654321,
'01-JAN-85')
1 row(s) inserted.
INSERT INTO DEPARTMENT VALUES('Research',
1 row(s) inserted.
SELECT * FROM DEPARTMENT
5,
333445555,
'22-MAY-78')
PROJECT TABLE:
INSERT INTO PROJECT VALUES('ProjectA',
3388,
'Houston',
1
)
1945,
'Salt Lake City',
6688,
'Houston',
2423,
'Bellaire',
4 )
7745, 'Sugarland',
5 )
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectB',
3
)
3
)
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectC',
5
)
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectD',
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectE',
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectF',
1566,
'Salt Lake City',
1234,
'New York',
2 )
3467,
'Stafford',
4 )
4345,
'Chicago',
2212,
'San Francisco',
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectG',
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectH',
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectI',
1
)
1 row(s) inserted.
INSERT INTO PROJECT VALUES('ProjectJ',
1 row(s) inserted.
2 )
SELECT * FROM PROJECT
2. Display all the employees’ information.
SELECT * FROM EMPLOYEE
3. Display Employee name along with his SSN and Supervisor SSN.
SELECT FName, Minit, LName, SSN, SuperSSN FROM EMPLOYEE
4. Display the employee names whose bdate is ’29-MAR-1959’.
SELECT FName, Minit, LName FROM EMPLOYEE WHERE BDATE = '29-MAR-59'
5. Display salary of the employees without duplications.
SELECT DISTINCT Salary, FName, LName FROM EMPLOYEE
6. Display the MgrSSN, MgrStartDate of the manager of ‘Finance’ department.
SELECT MgrSSN, MgrStartDate FROM DEPARTMENT WHERE DName = 'Finance'
7. Modify the department number of an employee having fname as ‘Joyce’ to 5
UPDATE EMPLOYEE SET DepNo = 5 WHERE FName = 'Joyce'
2 row(s) updated.
SELECT * FROM EMPLOYEE
8. Alter Table department add column DepartmentPhoneNum of NUMBER data type and insert values into this
column only.
ALTER TABLE DEPARTMENT ADD DepPhoneNo NUMBER(10)
Table altered.
UPDATE DEPARTMENT SET DepPhoneNo = 1234567890 WHERE DepNo = 1
1 row(s) updated.
UPDATE DEPARTMENT SET DepPhoneNo = 1234567890 WHERE DepNo = 2
1 row(s) updated.
UPDATE DEPARTMENT SET DepPhoneNo = 1234567890 WHERE DepNo = 3
1 row(s) updated.
UPDATE DEPARTMENT SET DepPhoneNo = 1234567890 WHERE DepNo = 4
1 row(s) updated.
UPDATE DEPARTMENT SET DepPhoneNo = 1234567890 WHERE DepNo = 5
1 row(s) updated.
select * from department
9. Alter table orders modify the size of DepartmentPhoneNum.
ALTER TABLE DEPARTMENT MODIFY DepPhoneNo NUMBER(11)
Table altered.
desc department
10. Modify the field name DepartmentPhoneNum of departments table to PhNo.
ALTER TABLE DEPARTMENT RENAME COLUMN DepPhoneNo TO PhNo
Table altered.
desc department
11. Rename Table Department as DEPT.
RENAME DEPARTMENT TO DEPT
Statement processed.
desc dept
12. Alter Table department remove column DepartmentPhoneNum.
ALTER TABLE DEPT DROP COLUMN PhNo
Table altered.
desc dept
13. Create a table COPYOFDEPT as a copy of the table DEPT.
CREATE TABLE COPYOFDEPT AS SELECT * FROM DEPT
Table created.
SELECT * FROM COPYOFDEPT
14. Delete all the rows from COPYOF DEPT table.
delete from copyofdept
15. Remove COPYOF DEPT table.
DROP TABLE COPYOFDEPT
Table dropped.
EXERCISE 2:
ALTERING TABLES:
EMPLOYEE TABLE:
ALTER TABLE EMPLOYEE MODIFY FName NOT NULL
Table altered.
ALTER TABLE EMPLOYEE MODIFY LName NOT NULL
Table altered.
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP_PK PRIMARY KEY(SSN)
Table altered.
ALTER TABLE EMPLOYEE
MODIFY Salary DEFAULT (800)
Table altered.
ALTER TABLE EMPLOYEE
ADD CONSTRAINT EMP_CK1 CHECK (Sex in ('M','F','m','f'))
Table altered.
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP_FK1 FOREIGN KEY (SuperSSN) REFERENCES
EMPLOYEE(SSN) ON DELETE SET NULL
Table altered.
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP_FK2 FOREIGN KEY (DepNo) REFERENCES DEPT(DepNo) ON
DELETE CASCADE
Table altered.
DEPT TABLE:
ALTER TABLE DEPT MODIFY DName NOT NULL
Table altered.
ALTER TABLE DEPT ADD CONSTRAINT DP_PK1 PRIMARY KEY(DepNo)
Table altered.
ALTER TABLE DEPT ADD CONSTRAINT DP_FK1 FOREIGN KEY (MgrSSN) REFERENCES EMPLOYEE(SSN) ON
DELETE SET NULL
Table altered.
PROJECT TABLE:
ALTER TABLE PROJECT MODIFY PName NOT NULL
Table altered.
ALTER TABLE PROJECT ADD CONSTRAINT PJ_PK1 PRIMARY KEY (PNumber)
Table altered.
ALTER TABLE PROJECT ADD CONSTRAINT PJ_FK1 FOREIGN KEY (DepNo) REFERENCES DEPT(DepNo) ON
DELETE SET NULL
Table altered.
WORKS_ON TABLE:
CREATE TABLE WORKS_ON(
ESSN CHAR(9),
Pno NUMBER(5),
Hours NUMBER(3,1) NOT NULL)
Table created.
ALTER TABLE WORKS_ON ADD CONSTRAINT WO_FK1 FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN) ON
DELETE CASCADE ADD CONSTRAINT WO_FK2 FOREIGN KEY (PNo) REFERENCES PROJECT(PNumber) ON
DELETE CASCADE
Table altered.
insert into Works_On values('123456789',3388,32.5)
1 row(s) inserted.
insert into Works_On values('123456789',1945,7.5)
1 row(s) inserted.
insert into Works_On values('666884444',3388,40.0)
1 row(s) inserted.
insert into Works_On values('453453453',7745,20.0)
1 row(s) inserted.
insert into Works_On values('453453453',2212,20.0)
1 row(s) inserted.
insert into Works_On values('333445555',7745,10.0)
1 row(s) inserted.
insert into Works_On values('333445555',6688,10.0)
1 row(s) inserted.
insert into Works_On values('333445555',4345,35.0)
1 row(s) inserted.
insert into Works_On values('333445555',2212,28.5)
1 row(s) inserted.
insert into Works_On values('999887777',1945,11.5)
1 row(s) inserted.
insert into Works_On values('999887777',1234,13.0)
1 row(s) inserted.
insert into Works_On values('543216789',2212,17.0)
1 row(s) inserted.
insert into Works_On values('554433221',1945,21.5)
1 row(s) inserted.
SELECT * FROM WORKS_ON
DEPT_LOCATIONS TABLE:
CREATE TABLE DEPT_LOCATIONS(
)
DepNo NUMBER(5) NOT NULL,
DLocation VARCHAR2(15)
Table created.
ALTER TABLE DEPT_LOCATIONS ADD CONSTRAINT DPL_FK1 FOREIGN KEY (DepNo) REFERENCES
DEPT(DepNo) ON DELETE CASCADE
Table altered.
INSERT INTO DEPT_LOCATIONS VALUES(1,'Houston')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(1,'Chicago')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(2,'New York')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(2,'San Francisco')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(3,'Salt Lake City')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(4,'Stafford')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(4,'Bellaire')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(5,'Sugarland')
1 row(s) inserted.
INSERT INTO DEPT_LOCATIONS VALUES(5,'Houston')
1 row(s) inserted.
SELECT * FROM DEPT_LOCATIONS
DEPENDENT TABLE:
CREATE TABLE DEPENDENT(
ESSN CHAR(9),
Dependent_Name VARCHAR2(15),
Sex CHAR(1)
CHECK (Sex in ('M','F','m','f')),
BDate DATE,
Relationship VARCHAR2(8) )
Table created.
ALTER TABLE DEPENDENT
ON DELETE CASCADE
ADD CONSTRAINT DPT_FK1 FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN)
Table altered.
INSERT INTO DEPENDENT VALUES('333445555','Alice','F','05-apr-76','Daughter')
1 row(s) inserted.
INSERT INTO DEPENDENT VALUES('333445555','Theodore','M','25-oct-73','Son')
1 row(s) inserted.
INSERT INTO DEPENDENT VALUES('333445555','Joy','F','03-may-48','Spouse')
1 row(s) inserted.
INSERT INTO DEPENDENT VALUES('987654321','Abner','M','29-feb-32','Spouse')
1 row(s) inserted.
INSERT INTO DEPENDENT VALUES('123456789','Alice','F','31-dec-98','Daughter')
1 row(s) inserted.
INSERT INTO DEPENDENT VALUES('123456789','Elizabeth','F','05-may-57','Spouse')
1 row(s) inserted.
SELECT * FROM DEPENDENT
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', '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)
1 row(s) inserted.
2. Insert ( '677678989', null, '40.0' ) into WORKS_ON.
INSERT INTO WORKS_ON VALUES('677678989',null,'40.0')
ORA-02291: integrity constraint (SQL_WIGOAIVCWEKKQNGHENXWIPHPR.WO_FK1) violated - parent key not found
ORA-06512: at "SYS.DBMS_SQL", line 1721
In the table WORKS_ON, we have the column PNo that is referenced to the column PNumber from the Project table using
foreign key. Now since it is a foreign key referenced to a primary key, null values cannot be added that violates the integrity
of the constraint. There has to be a value given to the column being referenced to the Primary Key column in another table.
3. Insert ( '453453453', 'John', M, '12-DEC-60', 'SPOUSE' ) into DEPENDENT
INSERT INTO DEPENDENT VALUES('453453453','John','M','12-DEC-60','SPOUSE')
1 row(s) inserted.
4. Delete the WORKS_ON tuples with ESSN= '333445555'.
DELETE FROM WORKS_ON WHERE ESSN='333445555'
4 row(s) deleted.
SELECT * FROM WORKS_ON
5. Modify the MGRSSN and MGRSTARTDATE of the DEPARTMENT tuple with DNUMBER=5 to '123456789' and '01OCT-88', respectively.
UPDATE DEPT SET MgrSSN='123456789', MgrStartDate='01-OCT-88' WHERE DepNo=5
1 row(s) updated.
Alter the tables to
1. Add Foreign Keys using Alter Table [if not done earlier].
Alter operations for constraints already performed.
2. Drop Foreign key defined on SuperSSN and add it using Alter table command.
ALTER TABLE EMPLOYEE
DROP CONSTRAINT EMP_FK1
Table altered.
ALTER TABLE EMPLOYEE
ADD CONSTRAINT EMP_FK1 FOREIGN KEY(DepNo) REFERENCES DEPT(DepNo)
Table altered.
3. Make name of Project as Unique and sex of employee as not null.
ALTER TABLE PROJECT ADD CONSTRAINT PJ_UQ1 UNIQUE(PName)
Table altered.
ALTER TABLE EMPLOYEE MODIFY Sex NOT NULL
Table altered.
4. Make Address as a new type containing door no, street, city, State, Continent.
CREATE TABLE EMP AS SELECT * FROM EMPLOYEE
Table created.
ALTER TABLE EMP ADD CONSTRAINT EP_PK PRIMARY KEY(Address)
Table altered.
create table AddressDetail( Address varchar(50), DoorNo char(14), Street varchar(20),
City varchar(10), State varchar(15), Continent varchar(20))
Table created.
alter table AddressDetail add constraint ad_fk foreign key(Address) references EMP(Address)
Table altered.
5. Make salary of employee to accept real values.
ALTER TABLE EMPLOYEE ADD CONSTRAINT EMP_CK2 CHECK(Salary > 0)
Table altered.
Download