Question 1

The suppliers-and-parts database consists of following relations:

S (SID, SNAME, STATUS, CITY)
- PRIMARY KEY (SID)
P (PID, PNAME, COLOR, WEIGHT, CITY)
- PRIMARY KEY (PID)
SP (SID, PID, QTY)
- PRIMARY KEY (SID, PID), FOREIGN KEY (SID) REFERENCES S,
- FOREIGN KEY (PID) REFERENCES P

The tables S and P represent suppliers and parts respectively.
The relation SP (shipments) indicates which supplier has shipped which part in which quantity.
image.png

A) Create the above tables and insert the values according to the below values.
I. Create the tables

  1. create schema if not exists `suppliers-and-parts` collate utf8mb4_0900_ai_ci;
  2. create table if not exists P
  3. (
  4. PID varchar(5) not null
  5. primary key,
  6. PNAME varchar(15) null,
  7. COLOR varchar(10) null,
  8. WEIGHT decimal(3) null,
  9. CITY varchar(15) null
  10. );
  11. create table if not exists S
  12. (
  13. SID varchar(5) not null
  14. primary key,
  15. SNAME varchar(15) null,
  16. STATUS decimal(4) null,
  17. CITY varchar(15) null
  18. );
  19. create table if not exists SP
  20. (
  21. SID varchar(4) not null,
  22. PID varchar(4) not null
  23. primary key,
  24. QTY decimal(5) null,
  25. constraint S_P_1
  26. foreign key (SID) references S (SID),
  27. constraint S_P_2
  28. foreign key (PID) references P (PID)
  29. );

II. Insert the values
P table

  1. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P1', 'Nut', 'Red', 12, 'London');
  2. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P2', 'Bolt', 'Green', 17, 'Paris');
  3. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P3', 'Screw', 'Blue', 17, 'Rome');
  4. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P4', 'Screw', 'Red', 14, 'London');
  5. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P5', 'Cam', 'Blue', 12, 'Paris');
  6. INSERT INTO `suppliers-and-parts`.P (PID, PNAME, COLOR, WEIGHT, CITY) VALUES ('P6', 'Cog', 'Red', 19, 'London');

S Table

  1. INSERT INTO `suppliers-and-parts`.S (SID, SNAME, STATUS, CITY) VALUES ('S1', 'Smith', 20, 'London');
  2. INSERT INTO `suppliers-and-parts`.S (SID, SNAME, STATUS, CITY) VALUES ('S2', 'Jones', 10, 'Paris');
  3. INSERT INTO `suppliers-and-parts`.S (SID, SNAME, STATUS, CITY) VALUES ('S3', 'Black', 30, 'Paris');
  4. INSERT INTO `suppliers-and-parts`.S (SID, SNAME, STATUS, CITY) VALUES ('S4', 'Clark', 20, 'London');
  5. INSERT INTO `suppliers-and-parts`.S (SID, SNAME, STATUS, CITY) VALUES ('S5', 'Adams', 30, 'Athens');

SP Table

  1. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P1', 300);
  2. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P2', 200);
  3. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P3', 400);
  4. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P4', 200);
  5. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P5', 100);
  6. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S1', 'P6', 100);
  7. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S2', 'P1', 300);
  8. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S2', 'P2', 400);
  9. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S3', 'P2', 200);
  10. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S4', 'P2', 200);
  11. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S4', 'P4', 300);
  12. INSERT INTO `suppliers-and-parts`.SP (SID, PID, QTY) VALUES ('S4', 'P5', 400);

B) Execute the queries from your midterm test at any database.

Give SQL formulations for the following queries:

  1. Find full details of parts heavier than 3 kgs.

    1. select *
    2. from p
    3. where WEIGHT > 3;
  2. Find supplier id, supplier name and supplier city for suppliers who supply part P3.

    1. select S.SID, SNAME, CITY
    2. from S,SP
    3. where S.SID = SP.SID and PID = 'P3';
  3. Find the maximum and the minimum quantity for part P2.

    1. select max(QTY), min(QTY)
    2. from SP
    3. where PID = 'p2';
  4. Find the part number and the total shipment quantity for each part supplied less than 700.

    1. select PID, sum(QTY)
    2. from SP
    3. group by PID
    4. having sum(QTY) < 700;
  5. Find all shipments where quantity is in the range 400 and 800 inclusive.

    1. select *
    2. from SP
    3. where qty >= 400 and QTY <= 800;
  6. Find colors of parts supplied by supplier S3.

    1. select distinct COLOR
    2. from p,SP
    3. where P.PID = SP.PID and SID = 's3';

    Copy your SQL formulation and paste at the space below.

image.png