JJ
JJ
: 1 Name:
Date : Reg. No. :
--------------------------------------------------------------------------------
----------------------------
CREATION OF TABLES & SIMPLE QUERIES
Table Name: PC
Column Name Purpose
Model_No The unique identifier for each PC
Speed Clock Speed of the PC
RAM RAM in MB
HD The hard-disk capacity of the PC in GB
CD The speed of the CD Drive
Price Price of the PC
SQL> create table PC(
2 Model_No varchar2(10),
3 Speed number(5),
4 RAM number(5),
5 HD number(5),
6 CD number(5),
7 Price number(7));
Table created.
SQL> desc PC;
Name Null? Type
----------------------------------------- -------- ------------------
MODEL_NO VARCHAR2(10)
SPEED NUMBER(5)
RAM NUMBER(5)
HD NUMBER(5)
CD NUMBER(5)
PRICE NUMBER(7)
************************************************************
************************************************************
8. Manufacturer IBM buys manufacture HCL. Change all products made by HCL
so they are now made by IBM.
SQL> select * from product_info;
MAKER MODEL_NO TYPE
---------- ---------- --------------------
HCL PC112 PC
HCL LP113 LP
ZENITH PR114 PR
WIPRO PC122 PC
WIPRO LP123 LP
WIPRO PR124 PR
IBM PC134 PC
HCL LP114 LP
IBM PC132 PC
IBM LP133 LP
IBM PR134 PR
MAKER MODEL_NO TYPE
---------- ---------- --------------------
HCL PC100 PC
12 rows selected.
SQL> update product_info set maker='IBM' where maker='HCL';
4 rows updated.
SQL> select * from product_info;
MAKER MODEL_NO TYPE
---------- ---------- ----------
IBM PC112 PC
IBM LP113 LP
ZENITH PR114 PR
WIPRO PC122 PC
WIPRO LP123 LP
WIPRO PR124 PR
IBM PC134 PC
IBM LP114 LP
IBM PC132 PC
IBM LP133 LP
IBM PR134 PR
MAKER MODEL_NO TYPE
---------- ---------- ----------
IBM PC100 PC
12 rows selected.
************************************************************
9. For each PC double the amount of RAM and add 10 gb to the HDD.
3. Create suitable primary keys for all the tables of the above database sc
hema
a. Check if the primary keys are created successfully (User_Constraints tab
le)
4. Create suitable foreign keys for all the tables of the above database sc
hema
a. Check if the foreign keys are created successfully (User_Constraints tab
le)
SQL> alter table pc add constraint pcfk foreign key(model_no) references product
_info(model_no);
Table altered.
SQL> alter table printer add constraint prfk foreign key(model_no) references pr
oduct_info(model_no);
Table altered.
SQL> alter table laptop add constraint ltfk foreign key(model_no) references pro
duct_info(model_no);
Table altered.
SQL> select table_name,constraint_name,constraint_type from user_constraints;
TABLE_NAME CONSTRAINT_NAME C
------------------------------ ------------------------------ -
LAPTOP LTPK P
LAPTOP LTFK R
PC PCPK P
PC PCFK R
PRINTER PRPK P
PRINTER PRFK R
PRODUCT_INFO SYS_C0034022 C
PRODUCT_INFO PIPK P
SQL> alter table laptop add constraint ltfk foreign key(model_no) references pro
duct_info(model_no) on delete cascade;
Table altered.
SQL> delete from product_info where model_no='LP133';
1 row deleted.
SQL> select * from product_info;
MAKER MODEL_NO TYPE
---------- ---------- ----------
HCL PC112 PC
HCL LP113 LP
ZENITH PR114 PR
WIPRO PC122 PC
WIPRO LP123 LP
WIPRO PR124 PR
IBM PC134 PC
HCL LP114 LP
IBM PC132 PC
IBM PR134 PR
10 rows selected.
SQL> select * from laptop;
MODEL_NO SPEED RAM HD SCREENSIZE PRICE
---------- ---------- ---------- ---------- ---------- ----------
LP113 1 64 40 14 59000
LP123 2 128 60 16 72000
LP114 2 128 40 17 45000
****************************************************************
5. Check Constraints:
a. Apply a check constraint on the product_info table such that the only pe
rmitted values for the type column are pc , lp and pr .
b. Apply a check constraint such that the prices of pc, laptop and printer
are all positive.
SQL> alter table pc add constraint pcck check(price>0);
Table altered.
SQL> alter table laptop add constraint ltck check(price>0);
Table altered.
SQL> alter table printer add constraint prck check(price>0);
Table altered.
SQL> insert into printer values('PR134','TRUE','DOT',-50000);
insert into printer values('PR134','TRUE','DOT',-50000)
*
ERROR at line 1:
ORA-02290: check constraint (CSE3E63.PRCK) violated
7. Create a table named Type_info with the columns model , type from Product
Info table ( Use the create table yyy as select construct )
a. Initially no rows should be present in the dummy table
SQL> create table type_info as select model_no, type from product_info where 1>2
;
Table created.
SQL> desc type_info;
Name Null? Type
----------------------------------------- -------- ----------------------------
MODEL_NO VARCHAR2(10)
TYPE NOT NULL VARCHAR2(10)
SQL> select * from type_info;
no rows selected
b. Insert all the rows from product_info into Type_Info (only selected column
s)
Ex.No. :3 Name:
Date : Reg. No. :
--------------------------------------------------------------------------------
----------------------------
Name Age
DICK JONESWILLIAM SWING BART SARJEANT SUMA RANGANATH RICHARD WILLIA
MSBHARTH KUMAR JOHN PEARSON JACKSON MEENA KUMARI ADAH TALBOT
33 30 32 21 29 36 28 34 29
27
SQL> create table worker (
2 name varchar2(20),
3 age number(3));
Table created.
SQL> desc worker;
Name Null? Type
----------------------------------------- -------- ---------------
NAME VARCHAR2(20)
AGE NUMBER(3)
SQL> insert into worker values('&name',&age);
Enter value for name: DICK JONES
Enter value for age: 33
old 1: insert into worker values('&name',&age)
new 1: insert into worker values('DICK JONES',33)
1 row created.
SQL> /
Enter value for name: WILLIAM SWING
Enter value for age: 30
old 1: insert into worker values('&name',&age)
new 1: insert into worker values('WILLIAM SWING',30)
1 row created.
SQL> select * from worker;
NAME AGE
-------------------- ----------
DICK JONES 33
WILLIAM SWING 30
BART SARJEANT 32
SUMA RANGANATH 21
RICHARD WILLIAMS 29
BHARATH KUMAR 36
JOHN PEARSON 28
JACKSON 34
MEENA KUMARI 29
ADAH TALBOT 27
10 rows selected.
****************************************************************
1. List those rows of the table PC where RAM sizes are either 128 or 256 and th
e capacity of HD is greater than or equal to 50.
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
2. List all the rows from PRINTER except the printers pr112 or pr124 .
3. List all the rows from LAPTOP in ascending and descending order of the s
creen size.
****************************************************************
4. List the makers of PC s. Use like operator in the model field (don t use type)
.
5. List the laptop details where the screen size is not 17.
7. Give examples for all the arithmetic operators >, <, >=, <=, <>, =, betw
een, in.
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
> OPERATOR
SQL> select * from pc where hd>50;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
< OPERATOR
SQL> select * from pc where hd<100;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC134 1 512 60 68 80000
>= OPERATOR
SQL> select * from pc where hd>=100;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC132 1 128 100 68 50000
PC136 2 512 100 68 90000
<= OPERATOR
SQL> select * from pc where hd<=100;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
< OPERATOR
SQL> select * from pc where hd<>100;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC134 1 512 60 68 80000
= OPERATOR
SQL> select * from pc where hd=100;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC132 1 128 100 68 50000
PC136 2 512 100 68 90000
IN
SQL> select name from workerskill minus select name from worker;
NAME
--------------------
ANAND KUMAR
ELBERT LOWELL
HELEN BRANDT
VICTORIA LYNN
WILFRED LOWELL
****************************************************************
13. For each value of HD, list the number of PCs (use group by function).
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
SQL> select hd, count(*) as qty from pc group by hd;
HD QTY
---------- ----------
60 3
100 2
****************************************************************
14.List the HD values for which the number of PCs is more than 2 (use group by a
nd having clause).
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
Ex.No. :4 Name:
Date : Reg. No. :
--------------------------------------------------------------------------------
----------------------------
BUILT- IN FUNCTIONS
1. Find the difference between the highest and lowest price from laptop.
SQL> select * from laptop;
MODEL_NO SPEED RAM HD SCREENSIZE PRICE
---------- ---------- ---------- ---------- ---------- ----------
LP113 1 64 40 14 59000
LP123 2 128 60 16 72000
LP114 2 128 40 17 45000
SQL> select max(price), min(price), max(price)-min(price) as diff from laptop;
MAX(PRICE) MIN(PRICE) DIFF
---------- ---------- ----------
72000 45000 27000
2. Find the Standard deviation and Variance of price in PC table.
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
SQL> select STDDEV(price) , VARIANCE(price) from pc;
STDDEV(PRICE) VARIANCE(PRICE)
------------- ---------------
23039.0972 530800000
3. Use a specific command to get the data in the following manner:
Workername age
JACK ADAMS --21
MEENA KUMARI --31
SQL> select * from worker;
NAME AGE
-------------------- ----------
DICK JONES 33
WILLIAM SWING 30
BART SARJEANT 32
SUMA RANGANATH 21
RICHARD WILLIAMS 29
BHARATH KUMAR 36
JOHN PEARSON 28
JACKSON 34
MEENA KUMARI 29
ADAH TALBOT 27
10 rows selected.
SQL> select name as workername, lpad(age,4,'--') as age from worker;
WORKERNAME AGE
-------------------- ----
DICK JONES --33
WILLIAM SWING --30
BART SARJEANT --32
SUMA RANGANATH --21
RICHARD WILLIAMS --29
BHARATH KUMAR --36
JOHN PEARSON --28
JACKSON --34
MEENA KUMARI --29
ADAH TALBOT --27
10 rows selected.
4. a. Update the PC table such that, the price for all PC models is increas
ed by 12.33%.
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 40000
PC122 2 256 60 48 42000
PC132 1 128 100 68 50000
PC134 1 512 60 68 80000
PC136 2 512 100 68 90000
SQL> update pc set price=price*1.1233;
5 rows updated.
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 44932
PC122 2 256 60 48 47179
PC132 1 128 100 68 56165
PC134 1 512 60 68 89864
PC136 2 512 100 68 101097
SQL> select * from pc;
MODEL_NO SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ----------
PC112 2 256 60 52 44932
PC122 2 256 60 48 47179
PC132 1 128 100 68 56165
PC134 1 512 60 68 89864
PC136 2 512 100 68 101097
b. Display the PC model in lowercase and price rounded to the hu
ndredths for all the PCs.
SQL> select lower(model_no) as model,substr(price,1,3) as price from pc;
MODEL PRI
---------- ---
pc112 449
pc122 471
pc132 561
pc134 898
pc136 101
SQL> select lower(model_no) as model,trunc(price/100) as price from pc;
MODEL PRICE
---------- ----------
pc112 449
pc122 471
pc132 561
pc134 898
pc136 1010
5. List the names in the ascending order of the length of names from WORKER
table.
SQL> select * from worker;
NAME AGE
-------------------- ----------
DICK JONES 33
WILLIAM SWING 30
BART SARJEANT 32
SUMA RANGANATH 21
RICHARD WILLIAMS 29
BHARATH KUMAR 36
JOHN PEARSON 28
JACKSON 34
MEENA KUMARI 29
ADAH TALBOT 27
10 rows selected.
SQL> select name, length(name) as length from worker order by length(name) ;
NAME LENGTH
-------------------- ----------
JACKSON 7
DICK JONES 10
ADAH TALBOT 11
JOHN PEARSON 12
MEENA KUMARI 12
WILLIAM SWING 13
BART SARJEANT 13
BHARATH KUMAR 13
SUMA RANGANATH 14
RICHARD WILLIAMS 16
10 rows selected.
6. List the phone number (eliminate area code like 91-0437) of all employee
s from WORKERSKILL table.
SQL> select * from workerskill;
NAME SKILL CITY PHONE
-------------------- --------------- ---------- ---------------
DICK JONES SMITHY TRICHY 91-0437-77651
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0426-98721
HELEN BRANDT COMBINE DIRVER MADURAI 91-0435-33333
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0453-98765
JOHN PEARSON SMITHY MADURAI 91-0345-34565
VICTORIA LYNN SMITHY SYDNEY 91-0234-98723
ADAH TALBOT WORK THANJAVUR 91-0652-66544
ELBERT LOWELL DISCUS DELHI 91-0433-90875
WILFRED LOWELL WORK SALEM 91-0213-98723
ANAND KUMAR PROGRAM ERODE 91-0441-98123
JACKSON PROGRAM HOSUR 91-0543-90873
11 rows selected.
SQL> select substr(phone,9,5) from workerskill;
SUBST
-----
77651
98721
33333
98765
34565
98723
66544
90875
98723
98123
90873
11 rows selected.
7. Find the location of letter A in the employee name of WORKERSKILL table.
(Hint: use INSTR ( )).
SQL> select * from workerskill;
NAME SKILL CITY PHONE
-------------------- --------------- ---------- ---------------
DICK JONES SMITHY TRICHY 91-0437-77651
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0426-98721
HELEN BRANDT COMBINE DIRVER MADURAI 91-0435-33333
JOHN PEARSON COMBINE DRIVER CHENNAI 91-0453-98765
JOHN PEARSON SMITHY MADURAI 91-0345-34565
VICTORIA LYNN SMITHY SYDNEY 91-0234-98723
ADAH TALBOT WORK THANJAVUR 91-0652-66544
ELBERT LOWELL DISCUS DELHI 91-0433-90875
WILFRED LOWELL WORK SALEM 91-0213-98723
ANAND KUMAR PROGRAM ERODE 91-0441-98123
JACKSON PROGRAM HOSUR 91-0543-90873
11 rows selected.
SQL> select instr(name,'A') from workerskill;
INSTR(NAME,'A')
---------------
0
8
9
8
8
8
1
0
0
1
2
11 rows selected.
8. Use SOUNDEX ( ) function on WORKERSKILL table to find a record of column
city sound like Sidney .
c. List the employee name from WORKERSKILL table with lastname first and fir
stname next in single column. (without using CONCAT () function and || operator)
.
SQL> select lpad(substr(name,1,instr(name,' ')),
2 length(name),substr(name,instr(name,' ')+1,length(name))) as name
3 from workerskill;
NAME
-------------------------------------------------------------------------
JONESDICK
PEARSONJOHN
BRANDTHELEN
PEARSONJOHN
PEARSONJOHN
LYNNVICTORIA
TALBOTADAH
LOWELLELBERT
LOWELLWILFRED
KUMARANAND
11 rows selected.
11a. Alter the table worker skill to add a new column called date-of-joining (date
data type).
SQL> desc worker;
Name Null? Type
----------------------------------------- -------- ---------------------
NAME VARCHAR2(20)
AGE NUMBER(3)
SQL> alter table worker add(doj date);
Table altered.
SQL> desc worker;
Name Null? Type
----------------------------------------- -------- ---------------------
NAME VARCHAR2(20)
AGE NUMBER(3)
DOJ DATE
b. Update the table to include the values for date-of-joining for each
worker.
SQL> select * from worker;
NAME AGE DOJ
-------------------- ---------- ---------
DICK JONES 33
WILLIAM SWING 30
BART SARJEANT 32
SUMA RANGANATH 21
RICHARD WILLIAMS 29
BHARATH KUMAR 36
JOHN PEARSON 28
JACKSON 34
MEENA KUMARI 29
ADAH TALBOT 27
10 rows selected.
NAME
--------------------
Experience
-------------------------------------------------------------------------
SUMA RANGANATH
8Years1Months
RICHARD WILLIAMS
7Years6Months
BHARATH KUMAR
6Years4Months
NAME
--------------------
Experience
-------------------------------------------------------------------------
JOHN PEARSON
6Years6Months
JACKSON
8Years5Months
MEENA KUMARI
8Years6Months
NAME
--------------------
Experience
-------------------------------------------------------------------------
ADAH TALBOT
3Years10Months
10 rows selected.
13. Display the current date/time in the following formats:
SQL> select to_char(sysdate,'dd-mon-yyyy') as "DATE" from dual;
DATE
-----------
27-jul-2009
a. Sep-2006
SQL> select to_char(sysdate,'mon-yyyy') as "DATE" from dual;
DATE
--------
jul-2009
b. September 12 2006
SQL> select to_char(sysdate,' month dd yyyy') as "DATE" from dual;
DATE
------------------
july 27 2009
c. 12-09-06 12:11:10
SQL> select to_char(sysdate,'dd-mm-yy hh:mi:ss') as "DATE" from dual;
DATE
-----------------
27-07-09 02:51:50
13. Display the equivalent GMT and EST, for the given date and time.
SQL> select to_char(sysdate,'dd-mon-yyyy') as "DATE",
2 to_char(new_time(sysdate,'EST','GMT'), 'hh:mi:ss') as "TIME" from dual;
DATE TIME
----------- --------
27-jul-2009 07:53:52
DATE TIME
----------- --------
27-jul-2009 07:53:52
SQL> select to_char(sysdate,'dd-mon-yyyy') as "DATE",
2 to_char(new_time(sysdate,'GMT','EST'),'hh:mi:ss') as "TIME" from dual;
DATE TIME
----------- --------
27-jul-2009 09:54:57