Dbms Lab 1,2,3,4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

DBMS LAB EXCERCISES.

Ex.No: 1
Name: M. Nithin Reddy Date: 13-9-21
Roll No: CH.EN.U4CSE20142

1. Table Name: Product_Info


Column Name Purpose
Maker Name of the maker/manufacturer of computers/peripherals
Model_No The unique identifier for each product manufactured
Type Indicates the type of the product – PC for personal computers, LP for
Laptop and PR for
Printer
Create
SQL>Table created
Sql>desc tablename

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

4. Table Name: Printer


Column Name Purpose
Model_No The unique identifier for each Laptop
Color Flag indicating whether it is a color printer or a black and white printer
Type Line, InkJet or Laser
Price Price of the printer

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.

1. Create the 4 tables mentioned above


2. Insert the following tuples in the table ProductInfo
MAKER MODEL 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
Select and display the table after inserting by using Select statement.
3.Insert the following tuples in the table pc
MODEL 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
Select and display the table after inserting by using Select statement
INSERT INTO PCC(MODEL_NO,SPEED,RAM,HD,CD,PRICE) VALUES
("PC112",2,256,60,52,40000),("PC122",2,256,60,48,42000),
("PC132",1,128,100,68,50000),("PC134",1,512,60,68,80000);

4. Insert the following tuples in the table LAPTOP


MODEL SPEED RAM HD SCREEN PRICE
------------------------------------------------------------------------
lp113 1 64 40 14 59000
lp123 2 128 60 16 72000
lp133 2 256 80 17 100000
lp114 2 128 40 17 45000
Select and display the table after inserting by using Select statement
5. Insert the following tuples in the table PRINTER
MODEL COLO TYPE PRICE
---------------------------------------------------
Pr114 true ink 17000
pr124 false dot 12000
pr134 true laser 17000
Select and display the table after inserting by using Select statement

6. Insert a tuple such that the model value is PC100, it is


manufactured by HCL and has a speed of 3 GHz, RAM of 256 MB, HD of
40 GB and CD of speed 52x. The price is 50000.
Show the updated table
7. Delete all PC’s with less than 50 cd speed.
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
PC100 3 256 40 52 50000

8. Manufacturer IBM buys manufacture HCL. Change all products made


by HCL so they are now made by IBM.
9. For each PC double the amount of RAM and add 10 gb to the HDD.

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.

2. Alter the table pc to have a default speed of 2.


3. Create suitable primary keys for all the tables of the above
database schema.
a. Check if the primary keys are created successfully
(User_Constraints table)
4. Create suitable foreign keys for all the tables of the above
database schema
a. Check if the foreign keys are created successfully
(User_Constraints table)
b. Demonstrate the usage of On delete cascade

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

7. Create a table named ‘Type_info’ with the columns model , type


from ProductInfo table ( Use the “create table yyy as select” construct )
a. Initially no rows should be present in the dummy table
b. Insert all the rows from product_info into ‘Type_Info’ (only
selected columns)
a. Initially no rows should be present in the dummy table

b. Insert all the rows from product_info into ‘Type_Info’ (only


selected columns)
8. Modify the table printer by adding a column printercode of type
varchar2(10) to identify the printer uniquely in the world.

9. Add a Unique key constraint in the printercode column.


a. Check if the unique key constraint is created successfully
(User_Constraints table)
10. Increase the width of the column printercode by 2.

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

1. Create a table ‘WORKERSKILL’ with the following details


Code:
create table WorkerSkill(Name varchar(20), Skill char(30), City char(30) ,
Phone bigint);

2. Insert into workerskill table


Code:
Insert into WorkerSkill(Name,Skill,City,Phone) values
('chandu’,’racer’,’Hyderabad’, 8945678945),
('nithin’,’driver’,’Badrachalam’, 4567897456),
('srinidhi’, carpenter’,’Chennai’, 8887974568),
('bannu’,’Engineer’,’nalgonda’, 7995846788),
('swetha’,’hacker’,’hyderabad’, 7894568795),
('sanjana’,’doctor’,’hyderabad’,9854789789);

Output:

3) Create a table named ‘WORKER’ with the following details


Code:
create table Worker(Name varchar(20), Age int(10));

Output:

4) Insert into WORKER table


Code:
Insert into Worker(Name , Age) values (‘chandu’,20),(‘nithin’,19),
('srinidhi',30),('bannu',21),('swetha',25),('sanjana',21);

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;

13.Display the printer details with the following columnname as heading


PRINTER MODEL COLORINFO PRINTERTYPE PRICE -------------
-------- ---------------- --------------------- -------
Code:
select printcode 'printer', model_no 'model', type 'printertype', color
'colorinfo',price from printer;
14. Apply the following operations on WORKER and WORKERSKILL tables
UNION, UNION ALL , INTERSECTION & MINUS.
Union code:
select age from worker union select city from workerskill;

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;

Ex.no:4 Name:M. Nithin Reddy


Date:13-sep-2021 reg.no:20142

BUILT- IN FUNCTIONS

Code:
select max(price)-min(price) as difference from laptop;
Code:
select stddev(price) as standarddeviation from pc;

select variance(price) as variance from pc;


Code:
select name workername,lpad(age,4,'--') age from worker;

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("2009-07-27","%d-%m-%y") date,current_time time ;

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

You might also like