Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

[CC4057] Lab work – week 3

1. Create a database named “MyStore”.

create database MyStore;
use MyStore;

2. Create a table named “suppliers” in the database with the following fields:

supplierID   |  INT          | PRIMARY KEY
name         |  VARCHAR(255) |
address      |  VARCHAR(255) |
phone        |  VARCHAR(255) |

create table suppliers (supplierID int primary key, name varchar(225), address varchar(255),phone varchar(255));

3. Create a table named “products” in the database with the following fields:

productID         |  INT          | PRIMARY KEY
product_code      |  VARCHAR(255) |
name              |  VARCHAR(255) |
quantity          |  INT          |
price             |  INT          |
supplier          |  INT          | FOREIGN KEY

create table products (productID int primary key, product_code varchar(255), name varchar(255), quantity int, price int, supplier int, foreign key (supplier) references suppliers (supplierID));

4. Insert data into the “suppliers” table

supplierID |  name     |  address    |  phone
1          |  Adidas   |  Lalitpur   |  889-22-12
2          |  Nivea    |  Pokhara    |  686-48-34
3          |  CG       |  Kathmandu  |  950-43-11

insert into suppliers values (1,"Adidas","Lalitpur","889-22-12"), (2, "Nivea","Pokhara","686-48-34"), (3,"CG","Kathmandu","950-43-11");

5. Insert data into the “products” table

productID  |  product_code  |  name    |  quantity  |  price  | supplier
1          |  p001          |  waiwai  |  10000     |  20     | 3
2          |  p002          |  cream   |  2000      |  200    | 2
3          |  p003          |  shirt   |  2500      |  600    | 1
4          |  p004          |  pant    |  5000      |  1200   | 1
5          |  p005          |  tv      | 50000      |  50000  | 3

insert into products values (1, "p001","waiwai",10000,20,3), (2, "p002","cream",2000,200,2), (3,"p003","shirt",2500,600,1),(4,"p004","pant",5000,1200,1), (5,"p005","tv",50000,50000,3);

6. Write SQL statements to do the following:

a) show all records from both tables

select * from suppliers; select * from products;
OR,
SELECT suppliers.*, products.* FROM suppliers JOIN products ON products.supplier = suppliers.supplierID ORDER BY productID;

b) show the names and the prices of all products

select name, price from products;

c) show the productID and quantity of all the products

select productID, quantity from products;

d) show the names and phone numbers of all the suppliers

select name, phone from suppliers;

e) show the phone number of supplier named “Adidas”

select phone from suppliers where name="Adidas";

f) show all the products having supplier as 1

select * from products where supplier=1;

g) show the names and prices of products which are expensive than 500

select name, price from products where price > 500;

h) show the product_code and quantity of the products having quantity less than or equal to 5000

select product_code, quantity from products where quantity <= 5000;

i) show the price of “waiwai”

select price from products where name="waiwai";

j) show all the details of the products which are expensive than 500 and having quantity greater than 2000

select * from products where price > 500 AND quantity > 2000;

k) change the price of “waiwai” to 30

update products set price=30 where name="waiwai";

l) change the quantity of cream to 1500

update products set quantity=1500 where name="cream";

m) delete the data of “waiwai” from the products table

delete from products where name="waiwai";

n) add a column named “info” with type VARCHAR(255) in the products table

alter table products add column info varchar(255);

o) in the products table set the value of the column “info” to “from CG group” where supplier is equal to 3

update products set info = "from CG group" where supplier = 3;


screenshot


You'll only receive email when they publish something new.

More from Prabesh Sapkota
All posts