Dbms Lab 1,2,3,4
Dbms Lab 1,2,3,4
Dbms Lab 1,2,3,4
Ex.No: 1
Name: M. Nithin Reddy Date: 13-9-21
Roll No: CH.EN.U4CSE20142
2. 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
3.Table Name: Laptop
Column Name Purpose
Model_No The unique identifier for each Laptop
Speed Clock Speed of the PC
RAM RAM in MB
HD The hard-disk capacity of the PC in GB
Screensize Screensize of the laptop
Price Price of the PC
DML QUERIES
The product_info table serves as a master table containing the list of all
models of pc, laptop and printers manufactured by all makers. The PC,
Laptop and the printer table give specific details about each product in
that class.
10. For the printer whose modelno=pr124 subtract Rs. 4000 from the
price.
KEY CONSTRAINTS
1. Alter the table product_info to make the type column NOT NULL.
DELETE CASCADE: When we create a foreign key using this option, it deletes
the referencing rows in the child table when the referenced row is deleted in the
parent table which has a primary key.
UPDATE CASCADE: When we create a foreign key using UPDATE
CASCADE the referencing rows are updated in the child table when the
referenced row is updated in the parent table which has a primary key.
5. Check Constraints:
a. Apply a check constraint on the product_info table such that the
only permitted 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.
c. Check if the check constraints are created successfully
(User_Constraints table)
6. What are the different values for constraint_type in the
user_constraints table and what is the meaning of each of those
values?
P
R
C
U
11. Without removing the table definition from the database remove all
the rows from the table ‘Type_info’.
12. Drop the table ‘type_info’ from the database
Ex.No-3
Name: M. Nithin Reddy Date: 04-10-21
Roll.No: CH.EN.U4CSE20142
ARITHMETIC , LOGICAL , SET OPERATIONS & SORTING
Output:
Output:
Output:
5.List those rows of the table PC where RAM sizes are either ‘128’ or ‘256’ and
the capacity of HD is greater than or equal to 50.
Code:
select*from PC where Ram=256 or Ram=128 and HD>=50;
Output:
6. List all the rows from PRINTER except the printers ‘pr112’ or ‘pr124’
Code:
select*from Printer where not Model!="Pr112" or Model!="Pr124";
Output:
7. List all the rows from LAPTOP in ascending and descending order of the
screen size.
Code:
select*from Laptop order by Screen asc;
select*from Laptop order by Screen desc;
Output:
8. List the makers of PC’s. Use ‘like’ operator in the model field (don’t use
type).
Code:
Select*from Product_info where Type like ‘PC%’;
Output:
9. List the laptop details where the screen size is not 17.
Code:
select*from Laptop where Not Screen=17;
Output:
10. List the printers whose price is between 5000 and 10000. (Use between)
Code:
select*from Printer where Price between 5000 and 10000;
Output:
11. Give examples for all the arithmetic operators – >, =, <=, <>, =, between, in
Code:
select*from PC where HD>50;
Output:
Code:
Select*from PC where Price<50000;
Output:
Code:
Select*from PC where CD=52;
Output:
Code:
select*from PC where Ram<=512;
Output:
Code:
Select*from PC where CD<>52;
Output:
Code:
select*from PC where Speed<3;
Output:
Code:
Select*from PC where Ram>=512;
Output:
Code:
Select*from PC where Speed=3;
Output:
12. Display the PC details if we double the ram capacity
Code:
update pc set ram=ram*2;
Union all
Code:
select name from worker union all select name from workerskill;
Intersection
Code:
select distinct name from worker
-> where name IN(select name from workerskill);
Minus:
Code:
select distinct name from worker left join workerskill using(name) where
workerskill.name is null;
15. Display the HD size available in PC without duplicate values(use
DISTINCT).
Code:
select distinct hd from pc ;
16. For each value of RAM, list the number of PCs (use group by function)
Code:
select count(ram),ram from pc group by ram;
17. For each value of HD, list the number of PCs (use group by function).
Code:
select count(hd),model_no,hd from pc group by hd;
18. List the HD values for which the number of PCs is more than 2 (use group
by and having clause).
Code:
select count(hd),hd,model_no from pc group by hd having count(hd)>2;
BUILT- IN FUNCTIONS
Code:
select max(price)-min(price) as difference from laptop;
Code:
select stddev(price) as standarddeviation from pc;
Code:
update pc set price=price*1.1233;
select lower(model_no) model,substr(price,1,3) from pcc;
Code:
select name,length(name) length from worker order by length(name);
Code:
select substr(phone,9,5) phone from workerskill;
Code:
select instr(name,'A') nameA from workerskill;
Code:
select city from workerskill where soundex(city)=soundex('chennai');
Code:
select substr(name,instr(name,' ')+1) name from workerskill;
Code:
select concat(substr(name,instr(name,' ')+1) ,substr(name,1,instr(name,' ')))
name from workerskill;
Code:
select concat(substr(name,instr(name,' ')+1) ," ", substr(name,1,instr(name,'
')))firstname from workerskill;
Code:
selectlpad(substr(name,1,instr(name,'')),length(name),substr(name,instr(name,' ')
+1,length(name))) name from workerskill;
code:
alter table worker add(date_of_joining date);
Code:
update worker set date_of_joining='1976-07-25' where name='chandhu';
update worker set date_of_joining='1986-09-08' where name='nithin';
update worker set date_of_joining='1999-01-29' where name='samhitha';
update worker set date_of_joining='1989-10-26' where name='balaji';
update worker set date_of_joining='1979-11-11' where name='lohith';
Code:
select name,concat(round(datediff(curdate(),date_of_joining)/365),"years ",
round((datediff(curdate(),date_of_joining)-
(round(datediff(curdate(),date_of_joining)/365))*365)/30), "months")
experience from worker;
12)
Code:
select date_format(curdate(),'%d-%m-%y') DATE
,CONVERT_TZ(curdate(),'+12:00','+06:30') time ;
Code:
select replace(name,'sanjana','ravi') from worker;
Decode:
Code:
select name,skill,city,decode(city,'hyderabad','hyd') city from workerskill;
Translate:
Code;
There is no translate in mysql ut we can use nested replace in mysql to get same
operation as translate