You are on page 1of 9

Exp-1

1. Create the table customer, with primary key constraints

SQL> create table customer_19 (customer_id number NOT NULL,customer_name


varchar(10),customer_address varchar(14),city varchar(4), state
varchar(5),pstal_code number(3),PRIMARY KEY(CUSTOMER_ID));

Table created.

SQL> DESC CUSTOMER_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMER_ID NOT NULL NUMBER
CUSTOMER_NAME VARCHAR2(10)
CUSTOMER_ADDRESS VARCHAR2(14)
CITY VARCHAR2(4)
STATE VARCHAR2(5)
PSTAL_CODE NUMBER(3)

2. Create all other tables


SQL> CREATE TABLE order_19(order_id number not null, order_date date, customer_id
number,primary key(order_id));

Table created.

SQL> CREATE TABLE orderline_19(order_id number not null, product_id number,


ordered_quantity number ,primary key(order_id));

Table created.

SQL> CREATE TABLE product_19(product_id number not null, product_description


varchar(5), product_finish date,product_line_id number(3),primary key(product_id));

Table created.

SQL> create table supplier_19(s_id number not null,gender varchar(1),s_date


date,product_id number,ordered_quanity number,rating varchar(3),primary key
(s_id));

Table created.

3. Add foreign key reference to the customer_id attribute.


SQL> alter table order_19 add foreign key (customer_id) references
customer_19(customer_id);

Table altered.

4. Modify the attribute product_finish in the product table to product_material


SQL> alter table product_19 rename column product_finish to product_material;

Table altered.

5. Add an attribute called price in the Orderline table


SQL> alter table orderline_19 add price number;

Table altered.
SQL> desc orderline_19;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NOT NULL NUMBER
PRODUCT_ID NUMBER
ORDERED_QUANTITY NUMBER
PRICE NUMBER

6. Add a check constraint to ordered_quantity column and check that quantity is


above 5

SQL> alter table orderquantity_19 add check (ordered_quantity>5);

Table altered.

7. Rename orderline to orderquantity


SQL> alter table orderline_19 rename to orderquantity_19;

Table altered.

SQL> desc orderquantity_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NOT NULL NUMBER
PRODUCT_ID NUMBER
ORDERED_QUANTITY NUMBER
PRICE NUMBER

8. Add supplier address column to supplier table

SQL> alter table supplier_19 add s_addr varchar(5);

Table altered.

9. Describe all the tables

SQL> desc customer_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
CUSTOMER_ID NOT NULL NUMBER
CUSTOMER_NAME VARCHAR2(10)
CUSTOMER_ADDRESS VARCHAR2(14)
CITY VARCHAR2(4)
STATE VARCHAR2(5)
PSTAL_CODE NUMBER(3)

SQL> desc order_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NOT NULL NUMBER
ORDER_DATE DATE
CUSTOMER_ID NOT NULL NUMBER

SQL> desc orderquantity_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NOT NULL NUMBER
PRODUCT_ID NUMBER
ORDERED_QUANTITY NUMBER
PRICE NUMBER

SQL> desc product_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
PRODUCT_ID NOT NULL NUMBER
PRODUCT_DESCRIPTION VARCHAR2(5)
PRODUCT_MATERIAL DATE
PRODUCT_LINE_ID NUMBER(3)

SQL> desc supplier_19;


Name Null? Type
----------------------------------------- -------- ----------------------------
S_ID NOT NULL NUMBER
GENDER VARCHAR2(1)
S_DATE DATE
PRODUCT_ID NUMBER
ORDERED_QUANITY NUMBER
RATING VARCHAR2(3)
S_ADDR VARCHAR2(5)

10. Add primary key constraint to all tables

alter table customer_19 add primary key(customer_id);


Table altered.
alter table order_19 add primary key(customer_id);
Table altered.
alter table orderline_19 add primary key(customer_id);
Table altered.
alter table product_19 add primary key(customer_id);
Table altered.
alter table supplier_19 add primary key(customer_id);
Table altered.
11. Modify the datatype of rating to decimal
SQL> alter table supplier_19 modify rating decimal;

Table altered.

12. Add a not null constraint to customer name and address


SQL> alter table customer_19 modify customer_name varchar(10) not null;

Table altered.
SQL> alter table customer_19 modify customer_address varchar(14) not null;

Table altered.

13. Add unique key constraint to order_id in order table

SQL> alter table order_19 add unique(order_id);

Table altered.

______________________________________________________________________
EXP-2

1. Insert the above values in to the corresponding table.


SQL> select* from customer_19;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS
----------- ------------------------------ -------------------------
CITY STATE PSTAL_CODE
-------------------- ----- ----------
1 John Doe 392 Sunset Blvd
New York NT 10059

2 Henry Smith 6900 Main St


San Francisco CA 94032

3 Richard Newman 2040 Riverside Rd


San Diego CA 92010

CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS


----------- ------------------------------ -------------------------
CITY STATE PSTAL_CODE
-------------------- ----- ----------
4 Joe Joe 4010 Speedway
Tucson AZ 85719

SQL> select* from order_19;

ORDER_ID ORDER_DAT CUSTOMER_ID


---------- --------- -----------
100 01-OCT-14 1
101 01-OCT-14 2
102 02-OCT-14 3
103 03-OCT-14 2
104 10-OCT-14 1
105 10-OCT-14 4
106 10-OCT-14 2
107 10-OCT-14 1

SQL> select* from product_19;

PRODUCT_ID PRODUCT_DESCRIPTION PRODUCT_MATERIAL PRODUCT_LINE_ID


---------- -------------------- -------------------- ---------------
1000 Office Desk Cherry 95
1001 Managers Desk Red Oak 199
2000 Office Chair Cherry 75
2001 Managers Desk Natural Oak 129
3000 Book Shelf Natural Ash 35
3001 DuplexBookShelf White Ash 80
4000 Table Lamp Natural Ash 15
4001 Duplex Table Lamp White Ash 40
1000 Keyboard Plastic 20
SQL> SELECT* FROM SUPPLIER_O19;

S_ S_NAM G S_DATE PRODUCT_ID ORDERED_QUANTITY RATING


-- ----- - --------- ---------- ---------------- ----------
S1 aaa M 11-NOV-14 1000 5 1
S2 bbb F 01-NOV-14 2000 7 2
S2 bbb F 01-NOV-14 2001 10 2
S3 ccc M 20-OCT-14 9999 10 3
S4 ddd F 05-NOV-14 4001 5 4
S4 ddd F 05-NOV-14 4000 3 4
S5 eee M 13-NOV-14 1001 10 5
S5 eee M 13-NOV-14 1000 10 4
8 rows selected.

SQL> select* from orderquantity_19;

ORDER_ID PRODUCT_ID ORDERED_QUANTITY PRICE


---------- ---------- ---------------- ----------
100 4000 10 10000
101 1000 20 15000
101 2000 20 20000
102 3000 15 23500
102 2000 12 34560
103 4001 14 1000
104 2000 10 2000
105 3001 20 30005
106 3000 12 20500
106 4000 11 30000
107 4001 5 40100

2. Update the pstal_code of 'Mary Smith', to ‘10032'

SQL> UPDATE CUSTOMER_19 SET pstal_code=10032 WHERE customer_name='Henry Smith ';

1 row updated.
SQL> select* from customer_19;

CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS


----------- ------------------------------ -------------------------
CITY STATE PSTAL_CODE
-------------------- ----- ----------
1 John Doe 392 Sunset Blvd
New York NT 10059

2 Henry Smith 6900 Main St


San Francisco CA 10032

3 Richard Newman 2040 Riverside Rd


San Diego CA 92010

CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS


----------- ------------------------------ -------------------------
CITY STATE PSTAL_CODE
-------------------- ----- ----------
4 Joe Joe 4010 Speedway
Tucson AZ 85719

3. Insert the record (108, '10-NOV-14', 1) into Order table


SQL> insert into order_19 values(108,'10-nov-14',1);

1 row created.

SQL> select* from order_19;

ORDER_ID ORDER_DAT CUSTOMER_ID


---------- --------- -----------
100 01-OCT-14 1
101 01-OCT-14 2
102 02-OCT-14 3
103 03-OCT-14 2
104 10-OCT-14 1
105 10-OCT-14 4
106 10-OCT-14 2
107 10-OCT-14 1
108 10-NOV-14 1
4. Delete the record from Order where order_id =’108’

SQL> delete from order_19 where order_id=108;

1 row deleted.

SQL> select* from order_19;

ORDER_ID ORDER_DAT CUSTOMER_ID


---------- --------- -----------
100 01-OCT-14 1
101 01-OCT-14 2
102 02-OCT-14 3
103 03-OCT-14 2
104 10-OCT-14 1
105 10-OCT-14 4
106 10-OCT-14 2
107 10-OCT-14 1

8 rows selected.

5. Display the details of Product whose price is >100.


SQL> select distinct product_description from product_19 join orderquantity_19 on
orderquantity_19.product_id=product_19.product_id where orderquantity_19.price>100;

PRODUCT_DESCRIPTION
--------------------
Duplex Table Lamp
Book Shelf
Table Lamp
Office Desk
Office Chair
Keyboard
DuplexBookShelf

6. Sort and display the details of supplier whose rating is > = 4.


SQL> select* from supplier_O19 where rating>=4 order by rating;

S_ S_NAM G S_DATE PRODUCT_ID ORDERED_QUANTITY RATING


-- ----- - --------- ---------- ---------------- ----------
S4 ddd F 05-NOV-14 4001 5 4
S5 eee M 13-NOV-14 1000 10 4
S4 ddd F 05-NOV-14 4000 3 4
S5 eee M 13-NOV-14 1001 10 5

7. Sort the table orderline in descending, according to the ordered price.

SQL> select * from orderquantity_19 order by price desc;

ORDER_ID PRODUCT_ID ORDERED_QUANTITY PRICE


---------- ---------- ---------------- ----------
107 4001 5 40100
102 2000 12 34560
105 3001 20 30005
106 4000 11 30000
102 3000 15 23500
106 3000 12 20500
101 2000 20 20000
101 1000 20 15000
100 4000 10 10000
104 2000 10 2000
103 4001 14 1000

11 rows selected.

8. Show the customers details who have ordered the table lamp.
SQL> select customer_name,customer_address from( ((customer_19 inner join order_
19 on customer_19.customer_id=order_19.customer_id )inner join orderquantity_19
on order_19.order_id=orderquantity_19.order_id ) inner join product_19 on produc
t_19.product_id=orderquantity_19.product_id)where product_19.product_description
='Table Lamp' ;

CUSTOMER_NAME CUSTOMER_ADDRESS
------------------------------ -------------------------
John Doe 392 Sunset Blvd
Henry Smith 6900 Main St

9. How many customers have a name that ends with the letter 'a'?

SQL> select customer_name from customer_19 where customer_name like '%a';

no rows selected

10. List the customers who have the letter ‘y’ in their name.
SQL> select customer_name from customer_19 where customer_name like '%y%';

CUSTOMER_NAME
------------------------------
Henry Smith

11. Show the customers whose name starts with the letter ‘j’.

SQL> select customer_name from customer_19 where customer_name like 'J%';

CUSTOMER_NAME
------------------------------
John Doe
Joe Joe

12. List the names of the customers whose ordered quantity is between 10 to
15.
SQL> select customer_name from( (customer_19 inner join order_19 on
customer_19.customer_id=order_19.customer_id ) inner join orderquantity_19 on
order_19.order_id=orderquantity_19.order_id)where
orderquantity_19.ordered_quantity>10 and orderquantity_19.ordered_quantity<15;

CUSTOMER_NAME
------------------------------
Richard Newman
Henry Smith
Henry Smith
Henry Smith

13. Increase the quantity of suppliers by 2% for whoever rating is greater


than 4.

SQL> update supplier_O19 set ordered_quantity=ordered_quantity+


(ordered_quantity*2.0/100.0) where rating>4;

1 row updated.

14. Display the names of the customers in alphabetical order.


SQL> select customer_name from customer_19 order by customer_name;

CUSTOMER_NAME
------------------------------
Henry Smith
Joe Joe
John Doe
Richard Newman
15. Find the number of orders placed on 10-OCT-2014.
SQL> select count(*) from order_19 where order_date='10-OCT-14';

COUNT(*)
----------
4

16. Show the order details, customer details for the product_id ‘1000’.
SQL> select customer_19.customer_name,customer_19.customer_address,
order_19.order_id, order_19.order_date,orderquantity_19.ordered_quantity
from customer_19
inner join order_19 on customer_19.customer_id=order_19.customer_id
inner join orderquantity_19 on order_19.order_id=orderquantity_19.order_id
where orderquantity_19.product_id=1000

CUSTOMER_NAME CUSTOMER_ADDRESS ORDER_ID ORDER_DAT


------------------------------ ------------------------- ---------- ---------
ORDERED_QUANTITY
----------------
Henry Smith 6900 Main St 101 01-OCT-14
20

17. View the customer table of your Friend who is next to you using
appropriate DCL Commands
SQL> select* from urk17bi010.customer_010;

CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS


----------- ------------------------- -------------------------
CITY STATE PSTAL_CODE
------------------------- ------------------------- ----------
1 john doe 392 sunset blvd
new york NT 10059

2 HENRY SMITH 6900 MAIN ST


SAN FRNCISCO CA 10032

3 RICHARD NEWMAN 2040 RIVERSIDE RD


SAN FDIEGO CA 92010
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_ADDRESS
----------- ------------------------- -------------------------
CITY STATE PSTAL_CODE
------------------------- ------------------------- ----------
4 JOE JOE 4010 SPEEDWAY
TUCSON AZ 85719

You might also like