[CC4057] Tutorial – week 4
November 30, 2023•379 words
1. You have the following tables in a database:

i. Make an ER diagram for the relational model.

ii. Write SQL statements to create the tables present in the above relational model. (Use foreign key relations and other constraints where necessary)
suppliers
create
create table suppliers (
name varchar(100) primary key,
phone varchar(15)
);insert
insert into suppliers values
("Google", "865-898-644"),
("Microsoft", "661-182-856"),
("Sony", "500-122-856");
customers
create
create table customers (
id int primary key auto_increment,
name varchar(255) not null,
address varchar(255)
);insert
insert into customers
(name, address)
values
("John Smith", "Kathmandu"),
("Allan Turing", "Pokhara"),
("Sarah Jones", "Banepa"),
("Jeff Hinton", "Lalitpur"),
("Ashley Stone", "Chitwan"),
("Donald Duck", "Chitwan");
items
create
create table items (
name varchar(100) primary key,
supplier varchar(100),
price int,
foreign key (supplier) references suppliers(name)
);insert
insert into items values
("Nexus-5X", "Google", 200),
("Nexus-6P", "Google", 300),
("PS2", "Sony", 400),
("Xbox-360", "Microsoft", 250);
orders
create
create table orders(
order_id int primary key auto_increment,
customer_id int,
item varchar(100),
OrderDate date,
foreign key (customer_id) references customers(id),
foreign key (item) references items(name)
);insert
insert into orders
(customer_id, item, OrderDate)
values
(1, "Xbox-360", "2015-09-03"),
(1, "Nexus-5X", "2015-09-03"),
(2, "Nexus-6P", "2015-10-05"),
(3, "PS2", "2016-11-22"),
(4, "PS2", "2016-12-01"),
(4, "Nexus-5X", "2016-12-02");
iii. Write SQL queries for the following:
a. Show the name and address of the customers who are from Kathmandu or Lalitpur.
select name, address from customers where address in ("Kathmandu","Lalitpur");
b. Show the details of the items whose price range is from 200 to 300.
select * from items where price between 200 and 300;
c. Show the details of the orders made by customers having client IDs 1,2 or 4.
select * from orders where customer_id in (1,2,4);
d. Show the details of the orders from 2015.
select * from orders where year(OrderDate)=2015;
Screenshot
