[CRC] SQL
Upcoming SlideShare
Loading in...5
×
 

[CRC] SQL

on

  • 1,094 views

 

Statistics

Views

Total Views
1,094
Views on SlideShare
1,094
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft Word

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

[CRC] SQL [CRC] SQL Document Transcript

  • Sathwik Rai Page 1 of 32 INFO 606 Question 0: Oracle8i Enterprise Edition Release 8.1.5.0.0, LINUX operating system [CRC] SQL> set timing on Question 1: DDL Exercise 1 a. Creating Employee table [CRC] SQL> create table employee ( FNAME VARCHAR2(15) NOT NULL, MINIT CHAR, LNAME VARCHAR2(15) NOT NULL, SSN CHAR(9) NOT NULL, BDATE DATE, ADDRESS VARCHAR2(30), SEX CHAR, SALARY NUMBER(10,2), SUPERSSN CHAR(9), DNO NUMBER(2) NOT NULL, CONSTRAINT EMPPK PRIMARY KEY(SSN) , CONSTRAINT EMPSUPERFK FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE(SSN)); Table created. Elapsed: 00:00:00.25 Disabling all the constraints [CRC] SQL> alter table employee disable constraint EMPPK CASCADE; Table altered. Elapsed: 00:00:00.16 1 b. Creating Department table [CRC] SQL> REM Creating Department table [CRC] SQL> create table DEPARTMENT ( DNAME VARCHAR2(15) NOT NULL, DNUMBER NUMBER(2) NOT NULL, MGRSSN CHAR(9) NOT NULL, MGRSTARTDATE DATE, CONSTRAINT DEPTPK PRIMARY KEY(DNUMBER) DISABLE,
  • Sathwik Rai Page 2 of 32 INFO 606 CONSTRAINT DEPTSK UNIQUE(DNAME), CONSTRAINT DEPTMGRFK FOREIGN KEY(MGRSSN) REFERENCES EMPLOYEE(SSN) DISABLE); Table created. Elapsed: 00:00:00.14 1c. Enable PK constraint of Department table [CRC] SQL> alter table department enable constraint DEPTPK; Table altered. Elapsed: 00:00:00.10 1 d. Modifying Employee table [CRC] SQL> alter table employee add constraint EMPDEPTFK FOREIGN KEY(DNO) REFERENCES DEPARTMENT(DNUMBER); Table altered. Elapsed: 00:00:00.05 1.e Constraints specified so far SQL>select TABLE_NAME ,CONSTRAINT_NAME,CONSTRAINT_TYPE,STATUS from user_constraints where owner like 'SR68%'; TABLE_NAME CONSTRAINT_NAME C STATUS ------------------------------ ------------------------------ -- - -------- DEPARTMENT SYS_C00379726 C ENABLED DEPARTMENT SYS_C00379727 C ENABLED DEPARTMENT SYS_C00379728 C ENABLED DEPARTMENT DEPTPK P ENABLED DEPARTMENT DEPTSK U ENABLED DEPARTMENT DEPTMGRFK R DISABLED EMPLOYEE SYS_C00379715 C ENABLED EMPLOYEE SYS_C00379716 C ENABLED EMPLOYEE SYS_C00379717 C ENABLED EMPLOYEE SYS_C00379718 C ENABLED EMPLOYEE EMPPK P DISABLED EMPLOYEE EMPSUPERFK R DISABLED EMPLOYEE EMPDEPTFK R ENABLED
  • Sathwik Rai Page 3 of 32 INFO 606 13 rows selected. Elapsed: 00:00:00.18 1.f Inserting values into employee table [CRC] SQL> insert into employee values (‘&fname’,’&Minit’,’&lname’,’&SSN’,’&bdate’,’&address’,’&sex’,&Salary, ’&SuperSSN’,&DNO); Enter value for fname: Franklin Enter value for minit: T Enter value for lname: Wong Enter value for ssn: 333445555 Enter value for bdate: 08-DEC-55 Enter value for address: 638 Voss, Houston, TX Enter value for sex: M Enter value for salary: 40000 Enter value for superssn: 888665555 Enter value for dno: 5 old 1: insert into employee values('&fname','&Minit','&lname','&SSN','&bdate','&address','&sex',&Salary, ’&superssn,&dno) new 1: insert into employee values ('Franklin','T','Wong','333445555', ’08-DEC-55','638 Voss, Houston, TX ','M',40000,’888665555’,5) 1 rows inserted Elapsed: 00:00:00.00 Similarly all other values are inserted into employee by repeating the macro at SQL prompt by typing r and ENTER. Select * from employee; FNAME M LNAME SSN BDATE ADDRESS S SALARY --------------- - --------------- --------- --------- ------------------------------ - ---------- SUPERSSN DNO --------- ---------- --------- Franklin T Wong 333445555 08-DEC-55 638 Voss, Houston, TX M 40000 888665555 5
  • Sathwik Rai Page 4 of 32 INFO 606 John B Smith 123456789 09-JAN-65 731 Fondren, Houston, TX M 30000 333445555 5 Alicia J Zelaya 999887777 19-JUL-68 3321 Castle, Spring, TX F 25000 987654321 4 Jennifer S Wallace 987654321 20-JUN-41 291 Berry, Bellare, TX F 43000 888665555 4 Ramesh K Narayan 666884444 15-SEP-62 975 Fire Oak, Humble, TX M 38000 333445555 5 Joyce A English 453453453 31-JUL-72 5631 Rice, Houston, TX F 25000 333445555 5 Ahmad V Jabbar 987987987 29-MAR-69 980 Dallas, Houston, TX M 25000 987654321 4 James E Borg 888665555 10-NOV-37 450Stone, Houston, TX M 55000 1 1.g Inserting values into department table [CRC] SQL> insert into department values ('&DName',&Dno,'&MGRSSN','&MgrStartDate'); Enter value for dname: Research Enter value for dno: 5 Enter value for mgrssn: 33445555 Enter value for mgrstartdate: 22-May-88 old 1: insert into department values ('&DName',&Dno,'&MGRSSN','&MgrStartDate') new 1: insert into department values ('Research',5,'33445555','22-May-88') 1 row inserted Similarly all other values are inserted by repeating the macro command above,
  • Sathwik Rai Page 5 of 32 INFO 606 Select * from department; DNAME DNUMBER MGRSSN MGRSTARTD ------------------------- ----------------- --------- --------- Research 5 333445555 22-MAY-88 Administration 4 987654321 01-JAN-95 Headquarters 1 888665555 19-JUN-81 Elapsed: 00:00:00.20 1h Displaying employee table with Date in DD-MON-YYYY format [CRC] SQL> select FNAME,MINIT,LNAME,SSN,ADDRESS,SUPERSSN,DNO,SEX,TO_CHAR(BDATE, 'DD-MON-YYYY')BDATE from employee; FNAME M LNAME SSN ADDRESS SUPERSSN DNO S --------------- - --------------- --------- ------------------------------ --------- ---------- - BDATE ----------- Franklin T Wong 333445555 638 Voss, Houston, TX 888665555 5 M 08-DEC-1955 John B Smith 123456789 731 Fondren, Houston, TX 333445555 5 M 09-JAN-1965 Alicia J Zelaya 999887777 3321 Castle, Spring, TX 987654321 4F 19-JUL-1968 Jennifer S Wallace 987654321 291 Berry, Bellare, TX 888665555 4 F 20-JUN-1941 Ramesh K Narayan 666884444 975 Fire Oak, Humble, TX 333445555 5M 15-SEP-1962 Joyce A English 453453453 5631 Rice, Houston, TX 333445555 5 F 31-JUL-1972
  • Sathwik Rai Page 6 of 32 INFO 606 Ahmad V Jabbar 987987987 980 Dallas, Houston, TX 987654321 4M 29-MAR-1969 James E Borg 888665555 450Stone, Houston, TX 1M 10-NOV-1937 8 rows selected. Elapsed: 00:00:00.00 Displaying the department table with date in DD-MON-YYYY format [SQL> select DNAME, DNUMBER, MGRSSN, TO_CHAR (MGRSTARTDATE,'DD-MON-YYYY') MGRSTARTDATE from department; DNAME DNUMBER MGRSSN MGRSTARTDAT ------- -------- ---------- -------- ----------- Research 5 333445555 22-MAY-1988 Administration 4 987654321 01-JAN-1995 Headquarters 1 888665555 19-JUN-1981 Elapsed: 00:00:00.00 1 i. Enabling all other constraints of Employee table [CRC] SQL> alter table employee ENABLE constraint EMPPK; Table altered. Elapsed: 00:00:00.10 [CRC] SQL> alter table employee ENABLE constraint EMPSUPERFK; Table altered. 1 j. Enabling all other constraints of Department table Elapsed: 00:00:00.06 [CRC] SQL> alter table department enable constraint DEPTMGRFK;
  • Sathwik Rai Page 7 of 32 INFO 606 Table altered. 1k Constraints specified so far. SQL> select TABLE_NAME ,CONSTRAINT_NAME,CONSTRAINT_TYPE,STATUS from user_constraints where owner like 'SR68%'; TABLE_NAME CONSTRAINT_NAME C STATUS ------------------------------ ------------------------------ - -------- DEPARTMENT SYS_C00379726 C ENABLED DEPARTMENT SYS_C00379727 C ENABLED DEPARTMENT SYS_C00379728 C ENABLED DEPARTMENT DEPTPK P ENABLED DEPARTMENT DEPTSK U ENABLED DEPARTMENT DEPTMGRFK R ENABLED EMPLOYEE SYS_C00379715 C ENABLED EMPLOYEE SYS_C00379716 C ENABLED EMPLOYEE SYS_C00379717 C ENABLED EMPLOYEE SYS_C00379718 C ENABLED EMPLOYEE EMPPK P ENABLED EMPLOYEE EMPSUPERFK R ENABLED EMPLOYEE EMPDEPTFK R ENABLED 13 rows selected. Elapsed: 00:00:00.01 [CRC] SQL> 1 l. All other COMPANY schema tables have already been created: [CRC] SQL> desc works_on; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ ESSN CHAR(9) PNO NUMBER(2) HOURS NUMBER(4,1) [CRC] SQL> desc project; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ PNAME NOT NULL VARCHAR2(20) PNUMBER NUMBER(2) PLOCATION NOT NULL VARCHAR2(12) DNUM NUMBER(2) [CRC] SQL> desc dependent;
  • Sathwik Rai Page 8 of 32 INFO 606 Name Null? Type ----------------------------------------------------- -------- ------------------------------------ ESSN NOT NULL CHAR(9) DEPENDENT_NAME NOT NULL VARCHAR2(14) SEX CHAR(1) BDATE DATE RELATIONSHIP VARCHAR2(10) 1 m. Creating NEW_EMP table with a sequence [CRC] SQL> create sequence EMP_NUM start with 1000 increment by 1; Sequence created. Elapsed: 00:00:00.06 [CRC] SQL> create table new_emp(EMP_NUM,SSN,FNAME,LNAME,BDATE) as select emp_num.nextval,SSN,FNAME,LNAME,BDATE from employee; Table created. Elapsed: 00:00:00.25 [CRC] SQL> select * from NEW_EMP; EMP_NUM SSN FNAME LNAME BDATE ---------- --------- --------------- --------------- --------- 1000 333445555 Franklin Wong 08-DEC-55 1001 123456789 John Smith 09-JAN-65 1002 999887777 Alicia Zelaya 19-JUL-68 1003 987654321 Jennifer Wallace 20-JUN-41 1004 666884444 Ramesh Narayan 15-SEP-62 1005 453453453 Joyce English 31-JUL-72 1006 987987987 Ahmad Jabbar 29-MAR-69 1007 888665555 James Borg 10-NOV-37 1008 123123123 Sathwik Rai 08-JUN-78 9 rows selected. Elapsed: 00:00:00.00
  • Sathwik Rai Page 9 of 32 INFO 606 1 n. Creating table New_EMP2 with EMP_NUM2 field = E || (1000+last 4 digits of SSN) [CRC] SQL> create table NEW_EMP2(EMP_NUM2,SSN,FNAME,LNAME,BDATE)as select 'E'||(1000+SUBSTR(SSN,6)),SSN,FNAME,LNAME,BDATE from employee; Table created. Elapsed: 00:00:00.16 [CRC] SQL> select * from new_emp2; EMP_NUM2 SSN FNAME LNAME BDATE ----------------------------------------- --------- --------------- --------------- --------- E6555 333445555 Franklin Wong 08-DEC-55 E7789 123456789 John Smith 09-JAN-65 E8777 999887777 Alicia Zelaya 19-JUL-68 E5321 987654321 Jennifer Wallace 20-JUN-41 E5444 666884444 Ramesh Narayan 15-SEP-62 E4453 453453453 Joyce English 31-JUL-72 E8987 987987987 Ahmad Jabbar 29-MAR-69 E6555 888665555 James Borg 10-NOV-37 E4123 123123123 Sathwik Rai 08-JUN-78 9 rows selected. Elapsed: 00:00:00.10 1 o. First increase the DNAME field length to accommodate the new Department Name ‘Special Projects’ [CRC] SQL> alter table department modify DNAME VARCHAR2(25); Table altered. Disable the FK constraint DEPTMGRFK because you will be violating the department table NOT NULL and FK constraint on MGRSSN field since the new manger employee is not yet set up in the Employee table. [CRC] SQL> alter table department disable constraint DEPTMGRFK CASCADE; Table altered [CRC] SQL> alter table department disable constraint SYS_C00379728;
  • Sathwik Rai Page 10 of 32 INFO 606 Table altered. . Now insert the new DNAME and DNO without having to enter a MGRSSN yet. [CRC] SQL> insert into department (DNAME,DNUMBER) values ('Special Projects',99); 1 row created. Now you can enter the new employee record without violating any FK constraint: [CRC] SQL> insert into employee values('&Fname','&MInit','&LName','&SSN','&BDATE','&Address','&Sex',&Salary,'&S UPERSSN',&DNO); Enter value for fname: Sathwik Enter value for minit: P Enter value for lname: Rai Enter value for ssn: 123123123 Enter value for bdate: 08-Jun-1978 Enter value for address: 3221,Powelton Ave,Philadelphia Enter value for sex: M Enter value for salary: 58000 Enter value for superssn: Enter value for dno: 99 old 1: insert into employee values('&Fname','&MInit','&LName','&SSN','&BDATE','&Address','&Sex',&Salary,'&S UPERSSN',&DNO) new 1: insert into employee values('Sathwik','P','Rai','123123123','08- Jun-1978','3221,Powelton Ave,Philadelphia','M',58000,'',99) 1 row created. In order to update the MGRSSN field to the new employee Sathwiks SSN and MGRSTARTDATE to the present date do the following: SQL> update department set MGRSSN=(select SSN from employee where fname ='Sathwik'),MGRSTARTDATE = sysdate where DNUMBER = 99; 1 row updated. [CRC] SQL> select * from department;
  • Sathwik Rai Page 11 of 32 INFO 606 DNAME DNUMBER MGRSSN MGRSTARTD ------------------------- ---------- --------- --------- Research 5 333445555 22-MAY-88 Administration 4 987654321 01-JAN-95 Headquarters 1 888665555 19-JUN-81 Special Projects 99 123123123 07-MAR-03 Re Enabling the constraints of Department table: [CRC] SQL> alter table department enable constraint DEPTMGRFK; Table altered. [CRC] SQL> desc department; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ DNAME NOT NULL VARCHAR2(25) DNUMBER NOT NULL NUMBER(2) MGRSSN CHAR(9) MGRSTARTDATE DATE [CRC] SQL> alter table department enable constraint SYS_C00379728; Table altered. [CRC] SQL> desc department; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ DNAME NOT NULL VARCHAR2(25) DNUMBER NOT NULL NUMBER(2) MGRSSN NOT NULL CHAR(9) MGRSTARTDATE DATE
  • Sathwik Rai Page 12 of 32 INFO 606 Question 2 : Queries 2(a) Employees’ ages, names and monthly salaries [CRC] SQL> COLUMN SALARY FORMAT $999,999.99 SQL> select (FNAME ||' '||LNAME)"Employee Name" TRUNC (MONTHS_BETWEEN (SYSDATE,BDATE)/12)"Age", salary from employee ORDER BY BDATE; Employee Name Age SALARY -------------------------------- ---------- ------------ James Borg 65 $55,000.00 Jennifer Wallace 61 $43,000.00 Franklin Wong 47 $40,000.00 Ramesh Narayan 40 $38,000.00 John Smith 38 $30,000.00 Alicia Zelaya 34 $25,000.00 Ahmad Jabbar 33 $25,000.00 Joyce English 30 $25,000.00 Sathwik Rai 24 $58,000.00 9 rows selected. Elapsed: 00:00:00.03 2(b) Employees whose last name > 6 characters long [CRC] SQL> select LNAME ,LENGTH(LNAME) from employee where LENGTH(LNAME)> 6; LNAME LENGTH(LNAME) --------------- ------------- Wallace 7 Narayan 7 English 7 Elapsed: 00:00:00.03
  • Sathwik Rai Page 13 of 32 INFO 606 2 (c ) Subordinates of Borg in hierarchial order [CRC] SQL> select LPAD(' ',4*(LEVEL-1))||LNAME AS "HIERARCHIAL STRUCTURE" from employee start with LNAME='Borg' CONNECT BY PRIOR SSN=SUPERSSN; HIERARCHIAL STRUCTURE ---------------------------------------------------------------------------------------------------- Borg Wong Smith Narayan English Wallace Zelaya Jabbar 8 rows selected. Elapsed: 00:00:00.23 2 (d) Bosses of John Smith SQL> select LPAD(' ',4*(LEVEL-1))||FName||' '||LName AS "BOSSES OF SMITH" from employee start with LNAME ='Smith' CONNECT BY SSN =PRIOR SUPERSSN; BOSSES OF SMITH ---------------------------------------------------------------------------------------------------- John Smith Franklin Wong James Borg Elapsed: 00:00:00.03 2(e) Left outer join Formated output using RPAD so that proper alignment is obtained in the output SQL> select fname,lname,dependent_name,RPAD(d.sex,20,' ') DEPENDENT_SEX, RPAD(TRUNC(MONTHS_BETWEEN(SYSDATE,d.BDATE)/12),15)Dependent_Age from employee e,dependent d where e.SSN=d.ESSN(+); FNAME LNAME DEPENDENT_NAME DEPENDENT_SEX DEPENDENT_AGE --------------- --------------- ------------------- -------------------- - ------------------- Franklin Wong Alice F 26 Franklin Wong Theodore M 29 Franklin Wong Joy F 54 Jennifer Wallace Abner M 71
  • Sathwik Rai Page 14 of 32 INFO 606 John Smith Michael M 25 John Smith Alice F 24 John Smith Elizabeth F 45 Ahmad Jabbar N/A N/A N/A Sathwik Rai N/A N/A N/A Alicia Zelaya N/A N/A N/A James Borg N/A N/A N/A Joyce English N/A N/A N/A Ramesh Narayan N/A N/A N/A 2 f. Left Outer Join with nvl function SQL> select fname,lname,nvl(dependent_name,'N/A')Dep_Name,nvl(RPAD(d.sex,20,' '),'N/A') DEPENDENT_SEX, nvl(RPAD(TRUNC(MONTHS_BETWEEN(SYSDATE,d.BDATE)/12),15),'N/A') Dependent_Age from employee e,dependent d where e.SSN=d.ESSN(+); FNAME LNAME DEP_NAME DEPENDENT_SEX DEPENDENT_AGE --------------- --------------- -------------- -------------------- --------------- Franklin Wong Alice F 26 Franklin Wong Theodore M 29 Franklin Wong Joy F 54 Jennifer Wallace Abner M 71 John Smith Michael M 25 John Smith Alice F 24 John Smith Elizabeth F 45 Ahmad Jabbar N/A N/A N/A Sathwik Rai N/A N/A N/A Alicia Zelaya N/A N/A N/A James Borg N/A N/A N/A Joyce English N/A N/A N/A Ramesh Narayan N/A N/A N/A 13 rows selected. Elapsed: 00:00:00.05 2 g. SOUNDEX function [CRC] SQL> insert into employee values ('Mike','W','Wang','222111333','23- Jun-1977','321,Powelton','M',35000,'123456789',5); 1 row created. Elapsed: 00:00:00.18 [CRC] SQL> select LName from employee where SOUNDEX(LNAME) = SOUNDEX('Wong');
  • Sathwik Rai Page 15 of 32 INFO 606 LNAME --------------- Wong Wang Elapsed: 00:00:00.11 [CRC] SQL> insert into employee values('Mike','W','Weng','212111333','23- Jun-1977','321,Powelton','M',35000,'123456789',5); 1 row created. Elapsed: 00:00:00.01 [CRC] SQL> select LName from employee where SOUNDEX(LNAME) = SOUNDEX('Wong'); LNAME --------------- Wong Wang Weng 2 h. Top 5 salaries SQL> select SSN,FNAME,LNAME,SALARY from employee e1 where 5> (select count(*) from employee e2 where e1.salary<e2.salary) order by salary desc; SSN FNAME LNAME SALARY --------- --------------- --------------- ------------ 123123123 Sathwik Rai $58,000.00 888665555 James Borg $55,000.00 987654321 Jennifer Wallace $43,000.00 333445555 Franklin Wong $40,000.00 666884444 Ramesh Narayan $38,000.00 Elapsed: 00:00:00.25 2 i. Adding column Project_Begin_date to Project table SQL>alter table project ADD Proj_Begin_Date DATE DEFAULT SYSDATE
  • Sathwik Rai Page 16 of 32 INFO 606 Table altered SQL> select PNAME,PNUMBER,PLOCATION ,DNUM, RPAD(Proj_Begin_Date,18,' ') Proj_Begin_Date from project; PNAME PNUMBER PLOCATION DNUM PROJ_BEGIN_DATE -------------------- ---------- ------------ ---------- ------------------ ProductX 1 Bellaire 5 08-MAR-03 ProductY 2 Sugarland 5 08-MAR-03 ProductZ 3 Houston 5 08-MAR-03 Computerization 10 Stafford 4 08-MAR-03 Reorganization 20 Houston 1 08-MAR-03 Newbenefits 30 Stafford 4 08-MAR-03 6 rows selected. Question 3 : More queries 3 a). Assigning new E_No’s [CRC] SQL> select SSN,fname||' '||lname as EMPLOYEE_NAME ,SUBSTR(SSN,6,4)E_NO from employee; SSN EMPLOYEE_NAME E_NO ---------- ------------------------------- ---- 333445555 Franklin Wong 5555 123456789 John Smith 6789 999887777 Alicia Zelaya 7777 987654321 Jennifer Wallace 4321 666884444 Ramesh Narayan 4444 453453453 Joyce English 3453 987987987 Ahmad Jabbar 7987 888665555 James Borg 5555 123123123 Sathwik Rai 3123 9 rows selected.
  • Sathwik Rai Page 17 of 32 INFO 606 Elapsed: 00:00:00.03 3 b. Assigning salary code SQL> select LNAME ||','||FNAME As EMPLOYEE_NAME ,salary , TRUNC(SALARY/1000)SAL_CODE from employee order by sal_code desc; EMPLOYEE_NAME SALARY SAL_CODE ------------------------------- ---------- ---------- Rai,Sathwik 58000 58 Borg,James 55000 55 Wallace,Jennifer 43000 43 Wong,Franklin 40000 40 Narayan,Ramesh 38000 38 Smith,John 30000 30 Zelaya,Alicia 25000 25 English,Joyce 25000 25 Jabbar,Ahmad 25000 25 9 rows selected. 3c DECODE functions The below may not be an optimal solution since it takes the kind of data into consideration. It produces the desired result as far as this problem is concerned SQL> select fname,lname,Pname,hours,DECODE(MOD(Hours,30),20,'B',15,'B',7.5,'C',10,'C',NULL,'N/A','A') CODE from employee e,project p, works_on w where e.ssn = w.essn and p.pnumber= w.pno; FNAME LNAME PNAME HOURS COD --------------- --------------- -------------------- ---------- --- John Smith ProductX 32.5 A John Smith ProductY 7.5 C Ramesh Narayan ProductZ 40 C Joyce English ProductX 20 B Joyce English ProductY 20 B Franklin Wong ProductY 10 C Franklin Wong ProductZ 10 C Franklin Wong Computerization 10 C Franklin Wong Reorganization 10 C Alicia Zelaya Newbenefits 30 A Alicia Zelaya Computerization 10 C Ahmad Jabbar Computerization 35 A Ahmad Jabbar Newbenefits 5A Jennifer Wallace Newbenefits 20 B Jennifer Wallace Reorganization 15 B James Borg Reorganization N/A 16 rows selected.
  • Sathwik Rai Page 18 of 32 INFO 606 3 (d) Decode function When you want separate columns for the codes it is easy to get the desired logic into the query using DECODE function as below [CRC] SQL> select fname,lname,Pname,hours,LPAD(DECODE(SIGN((HOURS/30)-1),0,'A',1,'A','N/A'),6) CODE_A, LPAD(DECODE(MOD(HOURS,30),20,'B',15,'B','N/A'),6)CODE_B, LPAD(DECODE(ROUND(HOURS/30),0,'C','N/A'),6)CODE_C from employee e,project p, works_on w where e.ssn = w.essn and p.pnumber= w.pno;[CRC] SQL> 2 3 4 FNAME LNAME PNAME HOURS CODE_A CODE_B CODE_C --------------- --------------- -------------------- --------- - ------ ------ ------ John Smith ProductX 32.5 A N/A N/A John Smith ProductY 7.5 N/A N/A C Ramesh Narayan ProductZ 40 A N/A N/A Joyce English ProductX 20 N/A B N/A Joyce English ProductY 20 N/A B N/A Franklin Wong ProductY 10 N/A N/A C Franklin Wong ProductZ 10 N/A N/A C Franklin Wong Computerization 10 N/A N/A C Franklin Wong Reorganization 10 N/A N/A C Alicia Zelaya Newbenefits 30 A N/A N/A Alicia Zelaya Computerization 10 N/A N/A C Ahmad Jabbar Computerization 35 A N/A N/A Ahmad Jabbar Newbenefits 5 N/A N/A C Jennifer Wallace Newbenefits 20 N/A B N/A Jennifer Wallace Reorganization 15 N/A B N/A James Borg Reorganization N/A N/A N/A 16 rows selected.3(e) Employees born on weekends. SQL> select FNAME,LNAME , TO_CHAR(BDATE,'DAY')"DAY BORN" from employee where TO_CHAR(BDATE,'DAY') like 'SAT%' or TO_CHAR(BDATE,'DAY') like 'SUN%'; FNAME LNAME DAY BORN --------------- --------------- --------- John Smith SATURDAY Ramesh Narayan SATURDAY Ahmad Jabbar SATURDAY Elapsed: 00:00:00.01 3(f) Finding out my birth day [CRC] SQL> select FNAME,LNAME , TO_CHAR(BDATE,'DAY')"DAY BORN" from employee where lname='Rai';
  • Sathwik Rai Page 19 of 32 INFO 606 FNAME LNAME DAY BORN --------------- --------------- --------- Sathwik Rai THURSDAY Elapsed: 00:00:00.01 3(g) First find out the constraints on PROJECT table; [CRC] SQL> desc PROJECT; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ PNAME NOT NULL VARCHAR2(20) PNUMBER NUMBER(2) PLOCATION NOT NULL VARCHAR2(12) DNUM NOT NULL NUMBER(1) PROJ_BEGIN_DATE DATE [CRC] SQL> select CONSTRAINT_NAME,CONSTRAINT_TYPE,TABLE_NAME,STATUS from user_constraints where TABLE_NAME='PROJECT'; CONSTRAINT_NAME C TABLE_NAME STATUS ------------------------------ - ------------------------------ -------- SYS_C00379762 C PROJECT ENABLED SYS_C00379763 C PROJECT ENABLED SYS_C00379764 C PROJECT ENABLED Elapsed: 00:00:00.03 Now disable the RI constraint from PROJECT to DEPARTMENT. SQL> alter table project disable constraint SYS_C00379764; Table altered. Elapsed: 00:00:00.10 [CRC] SQL> desc PROJECT; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ PNAME NOT NULL VARCHAR2(20) PNUMBER NUMBER(2) PLOCATION NOT NULL VARCHAR2(12) DNUM NUMBER(1) PROJ_BEGIN_DATE DATE
  • Sathwik Rai Page 20 of 32 INFO 606 [CRC] SQL> alter table PROJECT modify DNUM NUMBER(2); Table altered. Elapsed: 00:00:00.05 [CRC] SQL> insert into project(PNAME,PNUMBER,PLOCATION,DNUM) values 2 ('OOSAD',99,'Phila',10); 1 row created. Elapsed: 00:00:00.01 [CRC] SQL> insert into DEPARTMENT values 2 ('Virtual',8,'888665555','09-May-1996'); 1 row created. 3 g-1 Formated left outer join between DEPARTMENT and PROJECT [CRC] SQL> select DNAME,DNUMBER,MGRSSN,MGRSTARTDATE,RPAD(PNAME,15,' ')PNAME, RPAD(PNUMBER,7,' ')PNUMBER,PLOCATION, RPAD(DNUM,4,' ')DNUM from department d, project p where d.dnumber=p.dnum(+); DNAME DNUMBER MGRSSN MGRSTARTD PNAME PNUMBER PLOCATION DNUM ------------------------- ---------- --------- --------- ------------- --------------- ------- ------------ --- Research 5 333445555 22-MAY-88 ProductX 1 Bellaire 5 Research 5 333445555 22-MAY-88 ProductY 2 Sugarland 5 Research 5 333445555 22-MAY-88 ProductZ 3 Houston 5 Administration 4 987654321 01-JAN-95 Computerization 10 Stafford 4 Headquarters 1 888665555 19-JUN-81 Reorganization 20 Houston 1 Administration 4 987654321 01-JAN-95 Newbenefits 30 Stafford 4 Special Projects 99 123123123 07-MAR-03 N/A N/A N/A N/A Virtual 8 888665555 09-MAY-96 N/A N/A N/A N/A 8 rows selected. Elapsed: 00:00:00.08 3 g-2 Formatted right outer join between DEPARTMENT and PROJECT
  • Sathwik Rai Page 21 of 32 INFO 606 [CRC] SQL> select DNAME,RPAD(DNUMBER,7,' ')DNUMBER,MGRSSN,MGRSTARTDATE,RPAD(PNAME,15,' ')PNAME, RPAD(PNUMBER,7,' ')PNUMBER,PLOCATION, RPAD(DNUM,4,' ')DNUM from department d, project p where d.dnumber(+)=p.dnum; 2 3 DNAME DNUMBER MGRSSN MGRSTARTD PNAME PNUMBER PLOCATION DNUM ------------------------- ------- --------- --------- --------------- ------- ------------ ---- Research 5 333445555 22-MAY-88 ProductX 1 Bellaire 5 Research 5 333445555 22-MAY-88 ProductY 2 Sugarland 5 Research 5 333445555 22-MAY-88 ProductZ 3 Houston 5 Administration 4 987654321 01-JAN-95 Computerization 10 Stafford 4 Headquarters 1 888665555 19-JUN-81 Reorganization 20 Houston 1 Administration 4 987654321 01-JAN-95 Newbenefits 30 Stafford 4 N/A N/A N/A N/A OOSAD 99 Phila 10 7 rows selected. Elapsed: 00:00:00.00 3 g-3 Full outer join.. [CRC] SQL> select * from department,project where dnumber=dnum(+) UNION select * from department,project where dnumber(+)=dnum; DNAME DNUMBER MGRSSN MGRSTARTD PNAME PNUMBER PLOCATION ------------------------- ------- --------- --------- -------------------- ------- ------------ DNUM PROJ_BEGI ------ --------- Administration 4 987654321 01-JAN-95 Computerization 10 Stafford 4 08-MAR-03 Administration 4 987654321 01-JAN-95 Newbenefits 30 Stafford 4 08-MAR-03 Headquarters 1 888665555 19-JUN-81 Reorganization 20 Houston 1 08-MAR-03 Research 5 333445555 22-MAY-88 ProductX 1 Bellaire 5 08-MAR-03 Research 5 333445555 22-MAY-88 ProductY 2 Sugarland 5 08-MAR-03
  • Sathwik Rai Page 22 of 32 INFO 606 Research 5 333445555 22-MAY-88 ProductZ 3 Houston 5 08-MAR-03 Special Projects 99 123123123 07-MAR-03 N/A N/A N/A N/A N/A Virtual 8 888665555 09-MAY-96 N/A N/A N/A N/A N/A N/A N/A N/A N/A OOSAD 99 Phila DNAME DNUMBER MGRSSN MGRSTARTD PNAME PNUMBER PLOCATION ------------------------- ------- --------- --------- -------------------- ------- ------------ DNUM PROJ_BEGI ------ --------- 10 09-MAR-03 9 rows selected. Elapsed: 00:00:00.01 3(h) Adding RETIREMENT_DATE field in Employee table and setting values for it as below SQL.> alter table employee ADD RETIREMENT_DATE DATE ; Table altered [CRC] SQL> desc employee; Name Null? Type ----------------------------------------------------- -------- ------------------------------------ FNAME NOT NULL VARCHAR2(15) MINIT CHAR(1) LNAME NOT NULL VARCHAR2(15) SSN NOT NULL CHAR(9) BDATE DATE ADDRESS VARCHAR2(30) SEX CHAR(1) SALARY NUMBER(10,2) SUPERSSN CHAR(9) DNO NOT NULL NUMBER(2) RETIREMENT_DATE DATE Retirement Dates have been inserted as instructed.
  • Sathwik Rai Page 23 of 32 INFO 606 Calculating the no.of days left to retire for each employee from Jan 1st 2003 [CRC] SQL> select FNAME ,LNAME,RETIREMENT_DATE RETIRE_DATE , (RETIREMENT_DATE-TO_DATE('01-Jan-2003'))"DAYS TO RETIRE" from employee; FNAME LNAME RETIRE_DATE DAYS TO RETIRE --------------- --------------- --------------- - ----------------- Franklin Wong 31-DEC-03 364 John Smith 31-DEC-04 730 Alicia Zelaya 31-DEC-05 1095 Jennifer Wallace 31-DEC-06 1460 Ramesh Narayan 31-DEC-07 1825 Joyce English 31-DEC-08 2191 Ahmad Jabbar 31-DEC-09 2556 James Borg 31-DEC-10 2921 Elapsed: 00:00:00.01 3(i) ROWID and ROWNUM [CRC] SQL> select ROWNUM,ROWID,FNAME,LNAME,SALARY from employee where salary > 35000; ROWNUM ROWID FNAME LNAME SALARY ---------- ------------------ --------------- --------------- ---------- 1 AABId0AAFAAAA6QAAA Franklin Wong 40000 2 AABId0AAFAAAA6QAAD Jennifer Wallace 43000 3 AABId0AAFAAAA6QAAE Ramesh Narayan 38000 4 AABId0AAFAAAA6QAAH James Borg 55000 5 AABId0AAFAABhZWAAA Sathwik Rai 58000 ---------------------------------------------------------------------------- Question 4 (Data Dictionary) 4 (a) SQL> select table_name, TABLESPACE_NAME, num_rows, avg_row_len, blocks, avg_space,num_rows/blocks block_size from TABS where table_name in ('DEPENDENT','EMPLOYEE','WORKS_ON','PROJECT','DEPARTMENT'); TABLE_NAME TABLESPACE_NAME NUM_ROWS AVG_ROW_LEN BLOCKS ------------------------------ ------------------------------ ---------- ----------- ---------- AVG_SPACE BLOCK_SIZE ---------- -----------
  • Sathwik Rai Page 24 of 32 INFO 606 DEPARTMENT USERS DEPENDENT USERS EMPLOYEE USERS PROJECT USERS WORKS_ON USERS Elapsed: 00:00:00.03 4(b) [CRC] SQL> select segment_name, bytes, blocks, bytes/blocks segment_size,extents , (bytes/(blocks*extents) )Extent_Size from user_segments where segment_name in ('EMPLOYEE','DEPARTMENT','PROJECT','WORKS_ON','DEPENDENT'); SEGMENT_NAME BYTES --------------------------------------------------------------------------------- ---------- BLOCKS SEGMENT_SIZE EXTENTS EXTENT_SIZE ---------- ------------ ---------- ----------- DEPARTMENT 51200 25 2048 1 2048 EMPLOYEE 51200 25 2048 1 2048 WORKS_ON 51200 25 2048 1 2048 PROJECT 51200 25 2048 1 2048 DEPENDENT 51200 25 2048 1 2048 Elapsed: 00:00:03.08
  • Sathwik Rai Page 25 of 32 INFO 606 Elapsed: 00:00:23.38 4c ANALYZING TABLES [CRC] SQL> [CRC] SQL> ANALYZE TABLE employee COMPUTE STATISTICS ; Table analyzed. Elapsed: 00:00:03.50 [CRC] SQL> ANALYZE TABLE department COMPUTE STATISTICS; Table analyzed. Elapsed: 00:00:00.68 [CRC] SQL> ANALYZE TABLE project COMPUTE STATISTICS; Table analyzed. Elapsed: 00:00:00.25 [CRC] SQL> ANALYZE TABLE works_on COMPUTE STATISTICS; Table analyzed. Elapsed: 00:00:00.23 [CRC] SQL> ANALYZE TABLE dependent COMPUTE STATISTICS; Table analyzed. Elapsed: 00:00:00.08 AFTER ANALYZING tables : 4(d) [CRC] SQL> select table_name, TABLESPACE_NAME, num_rows, avg_row_len, blocks, avg_space,num_rows/blocks block_size from TABS where table_name in ('DEPENDENT','EMPLOYEE','WORKS_ON','PROJECT','DEPARTMENT' TABLE_NAME TABLESPACE_NAME NUM_ROWS AVG_ROW_LEN BLOCKS ------------------------------ ------------------------------ ---------- ----------- ---------- AVG_SPACE BLOCK_SIZE ---------- ---------- DEPARTMENT USERS 5 36 1 1766 5 DEPENDENT USERS 7 37 1
  • Sathwik Rai Page 26 of 32 INFO 606 1687 7 EMPLOYEE USERS 9 85 1 1178 9 PROJECT USERS 7 36 1 1690 7 WORKS_ON USERS 16 19 1 1623 16 7 rows selected. Elapsed: 00:00:00.01 4(e) [CRC] SQL> select segment_name, bytes, blocks, bytes/blocks segment_size,extents , (bytes/(blocks*extents) )Extent_Size from user_segments where segment_name in ('EMPLOYEE','DEPARTMENT','PROJECT','WORKS_ON','DEPENDENT'); SEGMENT_NAME BYTES --------------------------------------------------------------------------------- ---------- BLOCKS SEGMENT_SIZE EXTENTS EXTENT_SIZE ---------- ------------ ---------- ----------- DEPARTMENT 51200 25 2048 1 2048 EMPLOYEE 51200 25 2048 1 2048 WORKS_ON 51200 25 2048 1 2048 PROJECT 51200 25 2048 1 2048 DEPENDENT 51200 25 2048 1 2048 4 (f) Current data instances of employee
  • Sathwik Rai Page 27 of 32 INFO 606 [CRC] SQL> [CRC] SQL> select * from employee; FNAME M LNAME SSN BDATE ADDRESS S SALARY --------------- - --------------- --------- --------- ------------------------------ - ---------- SUPERSSN DNO RETIREMEN --------- ---------- --------- Franklin T Wong 333445555 08-DEC-55 638 Voss, Houston, TX M 40000 888665555 5 31-DEC-03 John B Smith 123456789 09-JAN-65 731 Fondren, Houston, TX M 30000 333445555 5 31-DEC-04 Alicia J Zelaya 999887777 19-JUL-68 3321 Castle, Spring, TX F 25000 987654321 4 31-DEC-05 Jennifer S Wallace 987654321 20-JUN-41 291 Berry, Bellare, TX F 43000 888665555 4 31-DEC-06 Ramesh K Narayan 666884444 15-SEP-62 975 Fire Oak, Humble,TX M 38000 333445555 5 31-DEC-07 Joyce A English 453453453 31-JUL-72 5631 Rice, Houston, TX F 25000 333445555 5 31-DEC-08 Ahmad V Jabbar 987987987 29-MAR-69 980 Dallas, Houston, TX M 25000 987654321 4 31-DEC-09 James E Borg 888665555 10-NOV-37 450Stone, Houston, TX M 55000 1 31-DEC-10 Sathwik P Rai 123123123 08-JUN-78 3221,Powelton Ave,Phila M 58000 99 9 rows selected. 4(g) Increasing the number of rows of EMPLOYEE table by using the supplied stored procedure called inc_employee_data (N). Here use N as follows: N = 500 + the last two digits of your student ID. Hence for me N=500+98= 598 [CRC] SQL> EXEC inc_employee_data(598);
  • Sathwik Rai Page 28 of 32 INFO 606 PL/SQL procedure successfully completed. Elapsed: 00:00:00.46 4 (h) Checking the no.of rows in employee now [CRC] SQL> select count(*) from employee; COUNT(*) ---------- 607 [CRC] SQL> analyze table employee compute statistics; Table analyzed. Elapsed: 00:00:00.55 [CRC] SQL> select table_name, TABLESPACE_NAME, num_rows, avg_row_len, blocks, avg_space,TRUNC(num_rows/blocks) block_size from TABS where table_name ='EMPLOYEE'; TABLE_NAME TABLESPACE_NAME NUM_ROWS AVG_ROW_LEN BLOCKS -------------------- ---------- ------------------- ----------- --------------------- ---------- AVG_SPACE BLOCK_SIZE ---------- ---------- EMPLOYEE USERS 607 80 34 493 17 Elapsed: 00:00:00.00 4(i) Storage Estimations Method A :Manual Estimation for Tables without Data 1) The size of the block header is about 90 bytes Block size = 2048 bytes After overhead: 2048 -90= 1958 bytes 2) Subtracting PCTFREE factor for row updates Default value of PCTFREE =10. 1958* .01= 196 bytes Available space for data: 1958-196=1762 bytes
  • Sathwik Rai Page 29 of 32 INFO 606 3) Calculating space used per row Logical Record Length(LRL) = FNAME VARCHAR2(15) 15 bytes MINIT CHAR(1) 1 LNAME VARCHAR2(15) 15 SSN CHAR(9) 9 BDATE DATE 7 ADDRESS VARCHAR2(30) 30 SEX CHAR(1) 1 SALARY NUMBER(10,2) 8 - (10/2+1)+2 SUPERSSN CHAR(9) 9 DNO NUMBER(2) 4 RETIREMENT_DATE DATE 7 ======= Logical Record Length 106 bytes Avg row length=LRL+3(for row overhead)+ 11 bytes for column overhead ARL = 106+3+11 = 120 bytes 4) Blocking Factor BF=TRUNC(Available space /ARL) = TRUNC(1762/120) = 14 rows/block Method B: Estimation with ORACLE for Tables without data 1) The size of the block header is about 90 bytes Block size = 2048 bytes After overhead: 2048 -90= 1958 bytes 2) Subtracting PCTFREE factor for row updates Default value of PCTFREE =10. 1958* .01= 196 bytes Available space for data: 1958-196=1762 bytes 3) Calculating space used per row Avg record length obtained as follows [CRC] SQL> SELECT SUM(DATA_LENGTH) ARL FROM USER_TAB_COLUMNS WHERE TABLE_NAME ='EMPLOYEE'; ARL ---------- 138
  • Sathwik Rai Page 30 of 32 INFO 606 4) Blocking Factor = TRUNC(1762/ARL+3 for rowoverhead) = 12 rows/block Method C: Estimation with ORACLE for TABLES with Data 1) The size of the block header is about 90 bytes Block size = 2048 bytes After overhead: 2048 -90= 1958 bytes 2) Subtracting PCTFREE factor for row updates Default value of PCTFREE =10. 1958* .01= 196 bytes Available space for data: 1958-196=1762 bytes 3) Calculating the space used per row Calculate Avg record length as follows ANALYZE TABLE EMPLOYEE COMPUTE STATISTICS; Table analyzed. [CRC] SQL> SELECT AVG_ROW_LEN AVG_RL FROM USER_TABLES WHERE TABLE_NAME = 'EMPLOYEE'; AVG_RL ---------- 80 Elapsed: 00:00:00.08; 4) Blocking Factor = TRUNC(1762/Avg_RL)  TRUNC(1762/80) = 22 rows/block Method D: Actual Storage Display SQL> SELECT NUM_ROWS,BLOCKS,TRUNC(NUM_ROWS/BLOCKS) BF FROM USER_TABLES WHERE TABLE_NAME= 'EMPLOYEE'; NUM_ROWS BLOCKS BF ---------- ---------- - -------------- 607 34 17
  • Sathwik Rai Page 31 of 32 INFO 606 Elapsed: 00:00:00.01 BF = 17 rows/block 4 (j) Sizing of PK Index of Employee Method A:Estimation 1) Calculate the amount of space used by the index block header The size of the block header is about 161 bytes Block size = 2048 bytes After overhead : 2048 -161 = 1887 bytes 2) Subtract by PCTFREE factor for row updates - The default value of PCTFREE =10. - 1887 *(PCTFREE/100)=1887*0.1 =189 - Available space : 1887 -189 = 1698 bytes 3) Calculating space used per index row - Using the same method as in table size estimation above ?? - Space used per row = LRL+#columns+8 header bytes = 9 +2+8 = 19 bytes (Note: 2 columns –1 for the PK field value and another for the ROWID) Since PK index is unique index add 1 byte Hence Space used per row= 20 bytes 4) Index Blocking Factor BF=TRUNC(1698/20) = 84 rows/block Method B : Actual Storage using DD [CRC] SQL> select index_name,table_name from user_indexes where table_name like 'EMPLOYEE%'; INDEX_NAME TABLE_NAME ------------------------------ ------------------------------ EMPPK EMPLOYEE
  • Sathwik Rai Page 32 of 32 INFO 606 [CRC] SQL> select num_rows,leaf_blocks,TRUNC(num_rows/leaf_blocks)BF from user_indexes where INDEX_NAME ='EMPPK'; NUM_ROWS LEAF_BLOCKS BF ---------- ----------- -------------------- 607 8 75 Index Blocking Factor = 75 rows/block