0% found this document useful (0 votes)
39 views13 pages

Experiment 4

Data Base Management System 4

Uploaded by

lakshyasantani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views13 pages

Experiment 4

Data Base Management System 4

Uploaded by

lakshyasantani14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭Experiment No.: 04‬

‭Title:‬‭To use DML operations and SQL queries to‬


‭Populate the database‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭Batch:‬‭C-2‬ ‭Roll No.:‬‭16014223049‬ ‭Experiment No: 04‬

‭Aim:‬‭To use DML operations and SQL queries to populate‬‭the database .‬

‭___________________________________________________________________________‬

‭Resources needed:‬‭PostgreSQL PgAdmin4‬

_‭ __________________________________________________________________________‬
‭Theory:‬

‭ he‬ ‭Data‬ ‭Manipulation‬ ‭Language‬ ‭(DML)‬ ‭is‬ ‭used‬ ‭to‬ ‭populate‬ ‭the‬ ‭table‬ ‭with‬ ‭values,‬
T
‭modify the table values and remove the rows of the table.‬

‭ he DML statements‬
T
‭are: SELECT‬

I‭ NSERT‬
‭UPDATE‬
‭DELETE‬

_‭ _________________________________________________________________________‬
‭Procedure:‬

‭ REATE TABLE products (‬


C
‭product_no integer,‬

n‭ ame text,‬
‭price‬
‭numeric );‬

‭Let us consider the above products table‬

I‭ nserting rows:‬
‭The INSERT command requires the table name and column values‬

‭INSERT INTO products VALUES (1, ’Cheese’, 9.99);‬

I‭ f we don’t have values for all the columns, you can omit some of them. In that case, the‬
‭columns will be filled with their default values. For example:‬

‭INSERT INTO products (product_no, name) VALUES (1, ’Cheese’)‬

‭ pdating the values:‬


U
‭The UPDATE command requires three pieces of information:‬
‭(A Constituent College of Somaiya Vidyavihar University)‬
‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

1‭ .‬‭The name of the table and column to update‬


‭2.‬‭The new value of the column‬
‭3.‬‭Which row(s) to update‬
‭UPDATE products SET price = 10 WHERE price = 5;‬
‭UPDATE products SET price = price * 1.10;‬

‭Deleting rows:‬

‭ he syntax of the DELETE command is similar to the UPDATE command.‬


T
‭DELETE FROM products WHERE price = 10;‬

‭Retrieving values:‬

‭ he general syntax of the SELECT command‬


T
‭is SELECT select_list FROM table_expression‬
‭SELECT * FROM table1;‬
‭SELECT * FROM products WHERE price=10;‬
‭SELECT product_no, name FROM products WHERE price=10;‬

‭Example:‬

i‭nsert into department values('IT', 101, 'mumbai');‬


‭insert into department values('COMP', 102, 'mumbai');‬
‭insert into department values('ETRX', 103, 'delhi');‬
‭insert into department values('EXTC', 104, 'chennai');‬
‭insert into department values('account', 105, 'mumbai');‬

i‭nsert into employee values('anita','m','sharma','emp0001',20000,'mumbai',101);‬


‭insert into employee values('nita','g','patil','emp0004',10000,'mumbai',101);‬
‭insert into employee values('krupita','v','jetali','emp0003',20000,'delhi',103);‬
‭insert into employee values('juhi','r','verma','emp0002',15000,'delhi',104);‬
‭insert into employee values('anita','m','sharma', 'emp0005',20000,'mumbai',104);‬

i‭nsert into project values( 1, 'mumbai','website',101);‬


‭insert into project values( 2, 'chennai','coding',101);‬
‭insert into project values( 3, 'mumbai','testing',102);‬
‭insert into project values( 4, 'delhi','documentaion',103);‬

i‭nsert into works_on values(1,'emp0001', 12);‬


‭insert into works_on values(1,'emp0002', 10);‬
‭insert into works_on values(2,'emp0001', 6);‬
‭insert into works_on values(3,'emp0004', 2);‬

i‭nsert into dependent values(‘emp0001’, ‘sunita’,’sister’);‬


‭insert into dependent values(‘emp0001’, ‘nita’,’mother’);‬
‭insert into dependent values(‘emp0002’, ‘kamal’,’brother’);‬
‭insert into dependent values(‘emp0004’, ‘krishna’,’father’);‬
‭(A Constituent College of Somaiya Vidyavihar University)‬
‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

s‭ elect * from employee;‬


‭select * from department;‬
‭select * from project;‬
‭select * from dependent;‬
‭select * from works_on;‬

‭1) employee‬

f‭ namemnamelnamessn salary ecitydno‬


‭---------------------------------------- ------------------------------ ----------------------------------‬
‭anita m sharma emp0001 20000 mumbai101‬
‭juhi r verma emp0002 15000 delhi 104‬
‭krupita v jetali emp0003 20000 delhi 103‬
‭nita g patil emp0004 10000 mumbai 101‬
‭anita m sharma emp0005 20000 mumbai104‬

‭2) department‬

d‭ namednodlocation‬
‭------------------------------ ----------- -----------------------------------‬
‭IT 101 mumbai‬
‭COMP 102 mumbai‬
‭ETRX 103 delhi‬
‭EXTC 104 chennai‬
‭account 105 mumbai‬

‭4) project‬

p‭ noplocationpnamedno‬
‭----------- ---------------------------------------- -------------------- -----------‬
‭1 mumbai website 101‬
‭2 chennai coding 101‬
‭3 mumbai testing 102‬
‭4 delhidocumentaion 103‬

‭5) dependents‬

s‭ sndepname relation‬
‭-------------------- ------------------------------ ------------------------------‬
‭emp0001nita mother‬
‭emp0001sunita sister‬
‭emp0002kamal brother‬
‭emp0004krishna father‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬
‭6) woks_on‬

p‭ nossnno_of_hrs‬
‭----------- -------------------- -----------‬
‭1 emp0001 12‬
‭1 emp0002 10‬
‭2 emp0001 6‬
‭3 emp0004 2‬

‭__________________________________________________________________________‬

‭Results: (Queries printout with output as per the format)‬

I‭ NSERT INTO Users (UserID, UserName, EMail, PhoneNo, Password)‬


‭VALUES‬
‭('16014223049','Lakshya Santani','[email protected]',8400601296,'Lakshya@1411'),‬
‭('16014223039','Harshit Modi','[email protected]',836951507,'Harshit.Modi'),‬
‭('16014223035','Eashan Salian','[email protected]',8828224975,'Muscles@Eashan'),‬
‭('16014223057','Navya Bhootra','[email protected]',9819865073,'Navya.Bhootra'),‬
‭('16014223054','Zammy Shaikh','[email protected]',8104098974,'Kashmir@Azaadi'),‬
‭('16014223048','Kush Jain','[email protected]',8268334411,'Kush@Aish'),‬
‭('16014223038','Harshika Masand','[email protected]',7666779160,'Harshu@2309'),‬
‭('16014223028','Ayushi Ranjan','[email protected]',9819771902,'Aashu@05');‬

I‭ NSERT INTO Restaurant (RestroID, RestroName, Address, Cuisine, Ratings)‬


‭VALUES‬
‭('1','Cafe Monza','Kharghar','Indian',5.0),‬
‭('2','Pizza by the Bay','Marine Lines','Italian',4.2),‬
‭('3','Pleo','BKC','European',4.8),‬
‭('4','Bastain','Bandra','Indian',3.9),‬
‭('5','Balgami','Andheri East','Korean',4.9),‬
‭('6','Amazonia','Vile Parle','Indian',4.5);‬

I‭ NSERT INTO Reviews (ReviewID, UserID, RestroID, Ratings, Comments)‬


‭VALUES‬
‭('1', '16014223049', '1', 4.5, 'Great food and ambience!'),‬
‭('2', '16014223039', '2', 4.0, 'Good pizza, but service was slow.'),‬
‭('3', '16014223035', '3', 4.8, 'Amazing experience, highly recommend!'),‬
‭('4', '16014223057', '4', 3.5, 'Food was okay, but too pricey.'),‬
‭('5', '16014223054', '5', 4.9, 'Authentic Korean food, loved it!'),‬
‭('6', '16014223048', '6', 4.2, 'Lovely decor and good food.'),‬
‭('7', '16014223038', '1', 5.0, 'Absolutely wonderful place, will visit again.'),‬
‭('8', '16014223028', '2', 3.8, 'Decent, but not worth the hype.');‬

I‭ NSERT INTO Locations (LocationID, RestroID, City, CState, Zipcode)‬


‭VALUES‬
‭('LOC001', '1', 'Navi Mumbai', 'Maharashtra', 410210),‬
‭('LOC002', '2', 'Mumbai', 'Maharashtra', 400020),‬
‭('LOC003', '3', 'Mumbai', 'Maharashtra', 400051),‬
‭('LOC004', '4', 'Mumbai', 'Maharashtra', 400050),‬
‭('LOC005', '5', 'Mumbai', 'Maharashtra', 400059),‬
‭('LOC006', '6', 'Mumbai', 'Maharashtra', 400056),‬
‭('LOC007', '1', 'Thane', 'Maharashtra', 400601),‬
‭('LOC008', '2', 'Pune', 'Maharashtra', 411001),‬
‭(A Constituent College of Somaiya Vidyavihar University)‬
‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬
(‭ 'LOC009', '3', 'Bangalore', 'Karnataka', 560001),‬
‭('LOC010', '4', 'Delhi', 'Delhi', 110001);‬

I‭ NSERT INTO Specials (SpecialID, RestroID, Price, FoodType, DishName)‬


‭VALUES‬
‭('SP001', '1', 299.99, TRUE, 'Paneer Tikka Wrap'),‬
‭('SP002', '2', 499.50, FALSE, 'Pepperoni Pizza'),‬
‭('SP003', '3', 750.00, TRUE, 'Vegetarian Risotto'),‬
‭('SP004', '4', 999.00, FALSE, 'Butter Chicken'),‬
‭('SP005', '5', 850.25, TRUE, 'Bibimbap'),‬
‭('SP006', '6', 645.99, FALSE, 'Chicken Biryani'),‬
‭('SP007', '1', 320.00, TRUE, 'Veggie Club Sandwich'),‬
‭('SP008', '2', 450.75, TRUE, 'Margherita Pizza'),‬
‭('SP009', '3', 780.00, FALSE, 'Grilled Salmon'),‬
‭('SP010', '4', 650.49, TRUE, 'Chana Masala');‬

I‭ NSERT INTO Reservations (ReservationID, RestroID, UserID, ReservationTime)‬


‭VALUES‬
‭('RES001', '1', '16014223049', '18:30:00'),‬
‭('RES002', '2', '16014223039', '19:00:00'),‬
‭('RES003', '3', '16014223035', '20:00:00'),‬
‭('RES004', '4', '16014223057', '21:30:00'),‬
‭('RES005', '5', '16014223054', '19:30:00'),‬
‭('RES006', '6', '16014223048', '18:00:00'),‬
‭('RES007', '1', '16014223038', '17:45:00'),‬
‭('RES008', '2', '16014223028', '20:15:00'),‬
‭('RES009', '3', '16014223039', '19:15:00'),‬
‭('RES010', '4', '16014223057', '22:00:00');‬

‭ ELECT *‬
S
‭FROM Restaurant‬
‭WHERE Ratings > 4;‬

‭ ELECT *‬
S
‭FROM Reviews‬
‭WHERE RestroID = '2';‬

‭ ELECT *‬
S
‭FROM Locations‬
‭WHERE City = 'Mumbai';‬

‭ ELECT *‬
S
‭FROM Specials‬
‭WHERE FoodType = TRUE;‬

‭ ELECT *‬
S
‭FROM Reservations‬
‭WHERE UserID = '16014223049';‬

‭ ELECT *‬
S
‭FROM Reviews‬
‭WHERE Ratings = 5.0;‬

‭ ELECT *‬
S
‭FROM Locations‬
‭WHERE CState = 'Maharashtra';‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬
‭ ELECT *‬
S
‭FROM Specials‬
‭WHERE RestroID = '3';‬

‭ ELECT *‬
S
‭FROM Reservations‬
‭WHERE RestroID = '1';‬

‭ ELECT *‬
S
‭FROM Reviews‬
‭WHERE Ratings <4.0;‬

‭Output :‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭(A Constituent College of Somaiya Vidyavihar University)‬


‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬
‭Example:‬

1‭ )‬ ‭To extract the name and ssn of all the employees:‬


‭Select fname, mname, lname, ssn from employee;‬

f‭ namemnamelnamessn‬
‭---------------------------------------- ---------------------------------------- -----------------------------‬
‭anitasharmam emp0001‬
‭juhiverma r emp0002‬
‭krupitajetali v emp0003‬
‭nitapatil g emp0004‬
‭anitasharma m emp0005‬

2‭ )‬‭To select names and city of the employees earning salary more then 10000:‬
‭Select fname, mname, lname, ecity from the employee where salary>10000;‬

f‭ namemnamelname‬ ‭ecity‬
‭---------------------------------------- ------------------------- ----------------------------------------‬
‭anitasharmam mumbai‬
‭juhivermar delhi‬
‭krupitajetaliv delhi‬
‭anitasharma m mumbai‬

3‭ )‬ ‭TO get the details of the cities of the employees in our company:‬
‭select distinct ecity from employee;‬
‭ecity‬
‭------------‬
‭delhi‬
‭mumbai‬

‭4)‬ T ‭ o find the name of the department located in Mumbai and with department‬
‭number 101:‬
‭select dname from department where dlocation=’Mumbai’ and dno=101;‬
‭dname‬
‭--------------‬

5‭ )‬‭To delete all dependent whose relation is mother with employee:‬


‭delete form dependent where relation=’mother’;‬
‭ssndepname relation‬
‭-------------------- ------------------------------ ------------------------------‬
‭emp0001sunita sister‬
‭emp0002kamal brother‬
‭emp0004krishna father‬

‭6)‬‭Update relation employee to increment salary of all employees working in‬


‭Department 101 by Rs. 10000:‬
‭update employee set salary=salary+10000 where dno=101;‬
‭fnamemnamelnamessn salary ecitydno‬
‭(A Constituent College of Somaiya Vidyavihar University)‬
‭ JSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬
K
‭---------------------------------------- ------------------------------ ----------------------------------‬
‭anita m sharma emp0001 30000 mumbai101‬
‭juhi r verma emp0002 15000 delhi 104‬
‭krupita v jetali emp0003 20000 delhi 103‬
‭nita g patil emp0004 20000 mumbai 101‬
‭anita m sharma emp0005 20000 mumbai104‬

‭__________________________________________________________________________________‬

‭Questions:‬

‭Q1 Explain various data types used in SQL‬

‭Q2 What is outer JOIN and why is it used? Explain its type with example‬

‭Answers :‬

‭ ns‬ ‭1.‬ ‭SQL‬ ‭(Structured‬ ‭Query‬ ‭Language)‬ ‭uses‬‭a‬‭variety‬‭of‬‭data‬‭types‬‭to‬‭define‬‭the‬‭kind‬‭of‬


A
‭data‬ ‭that‬ ‭can‬ ‭be‬ ‭stored‬ ‭in‬ ‭a‬ ‭table's‬ ‭columns,‬ ‭helping‬ ‭to‬ ‭ensure‬ ‭data‬ ‭integrity‬ ‭and‬ ‭optimize‬
‭storage.‬ ‭Numeric‬ ‭data‬ ‭types‬ ‭like‬ ‭INT‬ ‭(integer),‬ ‭FLOAT,‬ ‭and‬ ‭DECIMAL‬ ‭are‬ ‭used‬ ‭for‬
‭numbers—INT‬ ‭for‬ ‭whole‬ ‭numbers‬ ‭and‬ ‭DECIMAL‬ ‭for‬ ‭precise‬ ‭decimal‬‭values‬‭(like‬‭prices).‬
‭Character‬ ‭data‬ ‭types‬ ‭like‬ ‭VARCHAR(n)‬ ‭and‬ ‭CHAR(n)‬ ‭store‬ ‭text;‬ ‭VARCHAR‬ ‭is‬ ‭used‬ ‭for‬
‭variable-length‬ ‭strings,‬ ‭while‬ ‭CHAR‬ ‭is‬ ‭for‬ ‭fixed-length‬ ‭strings.‬ ‭Date‬ ‭and‬ ‭Time‬ ‭data‬ ‭types‬
‭such‬‭as‬‭DATE,‬‭TIME,‬‭and‬‭DATETIME‬‭handle‬‭various‬‭formats‬‭of‬‭date‬‭and‬‭time‬‭information,‬
‭essential‬ ‭for‬ ‭recording‬ ‭events‬ ‭or‬ ‭scheduling.‬ ‭Boolean‬ ‭data‬ ‭types‬ ‭(e.g.,‬ ‭BOOLEAN‬ ‭or‬ ‭BIT)‬
‭represent‬‭true/false‬‭values,‬‭useful‬‭for‬‭flags‬‭or‬‭status‬‭indicators.‬‭Binary‬‭data‬‭types‬‭like‬‭BLOB‬
‭(Binary‬‭Large‬‭Object)‬‭store‬‭large‬‭binary‬‭data,‬‭such‬‭as‬‭images‬‭or‬‭files.‬‭There‬‭are‬‭also‬‭special‬
‭data‬ ‭types‬ ‭like‬ ‭JSON‬ ‭for‬ ‭storing‬ ‭JSON‬ ‭formatted‬ ‭data,‬ ‭and‬ ‭ENUM‬ ‭or‬ ‭SET‬ ‭for‬ ‭defining‬ ‭a‬
‭column‬ ‭with‬ ‭a‬ ‭predefined‬ ‭list‬ ‭of‬ ‭possible‬ ‭values.‬ ‭These‬ ‭data‬ ‭types‬ ‭collectively‬ ‭help‬ ‭in‬
‭managing and querying data efficiently, tailored to the needs of each application.‬

‭ ns‬ ‭2.‬‭An‬‭outer‬‭join‬‭in‬‭SQL‬‭is‬‭a‬‭type‬‭of‬‭join‬‭that‬‭returns‬‭all‬‭the‬‭records‬‭from‬‭one‬‭table‬‭and‬
A
‭the‬‭matching‬‭records‬‭from‬‭another,‬‭filling‬‭in‬‭NULL‬‭values‬‭where‬‭there‬‭is‬‭no‬‭match.‬‭It‬‭is‬‭used‬
‭when‬‭you‬‭want‬‭to‬‭see‬‭all‬‭the‬‭data‬‭from‬‭one‬‭table‬‭regardless‬‭of‬‭whether‬‭it‬‭has‬‭a‬‭corresponding‬
‭match‬‭in‬‭the‬‭other‬‭table,‬‭which‬‭is‬‭useful‬‭for‬‭identifying‬‭missing‬‭or‬‭incomplete‬‭data.‬‭There‬‭are‬
‭three‬ ‭types‬ ‭of‬ ‭outer‬ ‭joins:‬ ‭LEFT‬ ‭JOIN‬‭(or‬‭LEFT‬‭OUTER‬‭JOIN),‬‭RIGHT‬‭JOIN‬‭(or‬‭RIGHT‬
‭OUTER‬‭JOIN),‬‭and‬‭FULL‬‭JOIN‬‭(or‬‭FULL‬‭OUTER‬‭JOIN).‬‭A‬‭LEFT‬‭JOIN‬‭returns‬‭all‬‭records‬
‭from‬‭the‬‭left‬‭table‬‭and‬‭the‬‭matched‬‭records‬‭from‬‭the‬‭right‬‭table;‬‭if‬‭there‬‭is‬‭no‬‭match,‬‭the‬‭result‬
‭is‬ ‭NULL‬ ‭on‬ ‭the‬ ‭right‬ ‭side.‬ ‭For‬ ‭example,‬ ‭if‬ ‭you‬ ‭have‬ ‭a‬ ‭table‬ ‭of‬ ‭customers‬ ‭and‬ ‭a‬ ‭table‬ ‭of‬
‭orders,‬ ‭a‬ ‭LEFT‬ ‭JOIN‬ ‭from‬ ‭customers‬ ‭to‬ ‭orders‬ ‭will‬ ‭list‬‭all‬‭customers,‬‭including‬‭those‬‭who‬
‭have not placed any orders. A RIGHT JOIN works similarly but returns all records from the‬

r‭ ight‬‭table‬‭and‬‭matched‬‭records‬‭from‬‭the‬‭left,‬‭filling‬‭NULLs‬‭when‬‭there‬‭is‬‭no‬‭match.‬‭Finally,‬
‭a‬ ‭FULL‬ ‭JOIN‬ ‭combines‬ ‭the‬ ‭result‬ ‭of‬ ‭both‬ ‭LEFT‬ ‭and‬ ‭RIGHT‬ ‭JOINs,‬ ‭returning‬ ‭all‬ ‭records‬
‭when‬ ‭there‬ ‭is‬ ‭a‬ ‭match‬ ‭in‬ ‭either‬ ‭left‬ ‭or‬ ‭right‬ ‭table‬ ‭records,‬ ‭filling‬‭NULLs‬‭where‬‭there‬‭is‬‭no‬
‭match‬‭on‬‭either‬‭side.‬‭For‬‭example,‬‭using‬‭a‬‭FULL‬‭JOIN‬‭on‬‭customers‬‭and‬‭orders‬‭will‬‭show‬‭all‬
‭customers‬ ‭and‬ ‭all‬ ‭orders,‬ ‭including‬ ‭those‬ ‭without‬ ‭a‬ ‭match‬ ‭in‬ ‭the‬ ‭other‬ ‭table.‬ ‭2.‬
‭__________________________________________________________________________________‬
‭(A Constituent College of Somaiya Vidyavihar University)‬
‭KJSCE/IT/SYBTECH/SEMIII/DMS/2023-24‬

‭Conclusion: (Conclusion to be based on the objectives and outcomes achieved)‬

‭Grade: AA / AB / BB / BC / CC / CD /DD‬

‭Signature of faculty in-charge with date‬

‭__________________________________________________________________________________‬

‭References:‬

‭Books:‬

‭1.‬ E ‭ lmasri‬ ‭and‬ ‭Navathe,‬ ‭“Fundamentals‬ ‭of‬ ‭Database‬ ‭Systems”,‬ ‭6‭t‬h‬ ‭Edition,‬ ‭Pearson‬
‭Education‬
‭2.‬ ‭Korth, Slberchatz,Sudarshan, :”Database System Concepts”, 6th Edition, McGraw –‬
‭Hill.‬

‭WebSite:‬
‭1.‬ ‭http://www.tutorialspoint.com/postgresql/‬
‭2.‬ ‭http://sage.virtual-labs.ac.in/home/pub/21/‬

‭(A Constituent College of Somaiya Vidyavihar University)‬

You might also like