Database Management
Systems and SQL
DATABASE
Collection of Logically related tables
Why Multiple tables ?
Company
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14 Pro 50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Orders
Order_id ODate Qty Prname
101 2019/11/12 15 Hp Laptop
102 2019/12/13 10
Iphone 14
Pro
103 2019/1211 20 Sony TV
CompName RegDate
Stock
Price
Country
Hp
Company
2019/10/21 25 USA
Iphone 2019/10/3 65 Japan
Sony 2019/10/10 15 India
COMPDTLS
Foreign
key
4
Foreign Keys : Value of FK
either equals to the value
of PK to which it is
referred or NULL
Create table Orders
Orders
Order_id ODate Qty Prname
101 2019/11/12 15 Hp Laptop
102 2019/12/13 10 Iphone 14 Pro
103 2019/1211 20 Sony TV
create table orders(order_id varchar(5) primary key, odate date
NOT NULL, qty int , prname varchar(20) references
product(pname));
Insert into Orders
Orders
Order_id ODate Qty Prname
101 2019/11/12 15 Hp Laptop
102 2019/12/13 10 Iphone 14 Pro
103 2019/12/11 20 Sony TV
104 2019/12/12 5 Hp Laptop
105 2019/12/13 7 Sony TV
PName Price Category Manufacturer
Hp
Computer
30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Orders
Joins
Show the pname, price, orderid, qty
Select pname, price, order_id from
product, orders where pname=prname
Join
between Product
and Order
Order_id ODate Qty Prname
101 2019/11/12 15 Hp Laptop
102 2019/12/13 10
Iphone 14
Pro
103 2019/12/11 20 Sony TV
104 2019/12/12 5 Hp Laptop
105 2019/12/13 7 Sony TV
Joins
Product (pname, price, category, manufacturer)
orders(order_id, odate,qty, prname)
Find all products names and odate of category Computer;
Select pname, odate from product, orders where product.pname= orders.prname and
category=“Computer”;
List the products names whose ordered qty is greater than 10;
Select pname from Product, orders where qty>10;
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14 Pro 50000 Phone Iphone
Sony TV 20000 Tv Sony
SELECT sum(price*3)
FROM Product;
Product
Simple Aggregation
9
SELECT (price*3) as increased_price
FROM Product;
PName Price Category Manufacturer
Hp
Computer
30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product Orders
Simple Aggregation
Show the amount of all orders i.e. price* qty
Select (price*qty) as amount from
product, orders where
pname=prname ;
Order_id ODate Qty Prname
101 2019/11/12 15 Hp Laptop
102 2019/12/13 10
Iphone 14
Pro
103 2019/12/11 20 Sony TV
104 2019/12/12 5 Hp Laptop
105 2019/12/13 7 Sony TV
Grouping and Aggregation
SELECT Sum(price)
FROM Product
GROUP BY category
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14 Pro 50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Count the no of products manufactured by each manufacturer
Find total price of each category
SELECT count(*)
FROM Product
GROUP BY manufacturer
Deletions
DELETE FROM product
WHERE price<40000
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Delete products where price less than 40000
Deletions
DELETE FROM product
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Delete All products
Updation
Update product set price = 35000
WHERE category = ‘Computer’
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Set the price 35000 of all products of Computer category
Updation
Update product set price = price+200
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Increase the price of all products by 200
Updation
Update product set price = price+(price*0.1)
Where manufacturer=“Hp Company”
PName Price Category Manufacturer
Hp Computer 30000 Computer Hp Company
Hp Laptop 40000 Computer Hp Company
Iphone 14
Pro
50000 Phone Iphone
Sony TV 20000 Tv Sony
Product
Increase the price of all products of Hp Company works by 10%
17
Revision
VIPS: Oct - Dec 2019 18
19
20
VIPS: Oct - Dec 2019 21
VIPS: Oct - Dec 2019 22
23
24
25
Exercise
Create Database Myorg with following two tables
Employee
Department
Empid Empname designation DOJ Sal Comm Dept
Number Varchar Varchar Date Number Number Number
Deptid Deptname Mgrid Location
Number Varchar Number Varchar
VIPS: Oct - Dec 2019 27
Populate Data : Employee
Department
28
29
Do the Following Queries
VIPS: Oct - Dec 2019 30
Thanks.

Database Management Systems and SQL SERVER Multiple tablees.pptx

  • 1.
  • 2.
    DATABASE Collection of Logicallyrelated tables Why Multiple tables ?
  • 3.
    Company PName Price CategoryManufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Orders Order_id ODate Qty Prname 101 2019/11/12 15 Hp Laptop 102 2019/12/13 10 Iphone 14 Pro 103 2019/1211 20 Sony TV CompName RegDate Stock Price Country Hp Company 2019/10/21 25 USA Iphone 2019/10/3 65 Japan Sony 2019/10/10 15 India COMPDTLS Foreign key
  • 4.
    4 Foreign Keys :Value of FK either equals to the value of PK to which it is referred or NULL
  • 5.
    Create table Orders Orders Order_idODate Qty Prname 101 2019/11/12 15 Hp Laptop 102 2019/12/13 10 Iphone 14 Pro 103 2019/1211 20 Sony TV create table orders(order_id varchar(5) primary key, odate date NOT NULL, qty int , prname varchar(20) references product(pname));
  • 6.
    Insert into Orders Orders Order_idODate Qty Prname 101 2019/11/12 15 Hp Laptop 102 2019/12/13 10 Iphone 14 Pro 103 2019/12/11 20 Sony TV 104 2019/12/12 5 Hp Laptop 105 2019/12/13 7 Sony TV
  • 7.
    PName Price CategoryManufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Orders Joins Show the pname, price, orderid, qty Select pname, price, order_id from product, orders where pname=prname Join between Product and Order Order_id ODate Qty Prname 101 2019/11/12 15 Hp Laptop 102 2019/12/13 10 Iphone 14 Pro 103 2019/12/11 20 Sony TV 104 2019/12/12 5 Hp Laptop 105 2019/12/13 7 Sony TV
  • 8.
    Joins Product (pname, price,category, manufacturer) orders(order_id, odate,qty, prname) Find all products names and odate of category Computer; Select pname, odate from product, orders where product.pname= orders.prname and category=“Computer”; List the products names whose ordered qty is greater than 10; Select pname from Product, orders where qty>10;
  • 9.
    PName Price CategoryManufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony SELECT sum(price*3) FROM Product; Product Simple Aggregation 9 SELECT (price*3) as increased_price FROM Product;
  • 10.
    PName Price CategoryManufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Orders Simple Aggregation Show the amount of all orders i.e. price* qty Select (price*qty) as amount from product, orders where pname=prname ; Order_id ODate Qty Prname 101 2019/11/12 15 Hp Laptop 102 2019/12/13 10 Iphone 14 Pro 103 2019/12/11 20 Sony TV 104 2019/12/12 5 Hp Laptop 105 2019/12/13 7 Sony TV
  • 11.
    Grouping and Aggregation SELECTSum(price) FROM Product GROUP BY category PName Price Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Count the no of products manufactured by each manufacturer Find total price of each category SELECT count(*) FROM Product GROUP BY manufacturer
  • 12.
    Deletions DELETE FROM product WHEREprice<40000 PName Price Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Delete products where price less than 40000
  • 13.
    Deletions DELETE FROM product PNamePrice Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Delete All products
  • 14.
    Updation Update product setprice = 35000 WHERE category = ‘Computer’ PName Price Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Set the price 35000 of all products of Computer category
  • 15.
    Updation Update product setprice = price+200 PName Price Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Increase the price of all products by 200
  • 16.
    Updation Update product setprice = price+(price*0.1) Where manufacturer=“Hp Company” PName Price Category Manufacturer Hp Computer 30000 Computer Hp Company Hp Laptop 40000 Computer Hp Company Iphone 14 Pro 50000 Phone Iphone Sony TV 20000 Tv Sony Product Increase the price of all products of Hp Company works by 10%
  • 17.
  • 18.
    VIPS: Oct -Dec 2019 18
  • 19.
  • 20.
  • 21.
    VIPS: Oct -Dec 2019 21
  • 22.
    VIPS: Oct -Dec 2019 22
  • 23.
  • 24.
  • 25.
  • 26.
    Create Database Myorgwith following two tables Employee Department Empid Empname designation DOJ Sal Comm Dept Number Varchar Varchar Date Number Number Number Deptid Deptname Mgrid Location Number Varchar Number Varchar
  • 27.
    VIPS: Oct -Dec 2019 27 Populate Data : Employee Department
  • 28.
  • 29.
  • 30.
    VIPS: Oct -Dec 2019 30
  • 31.