0% found this document useful (0 votes)
7 views

lab1.SQL

Uploaded by

sidimed.jeilang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lab1.SQL

Uploaded by

sidimed.jeilang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MSC Data Science for Business Jannuary 2023

Database Management

Lab session 1 : SQL

We will rely on either sqliteonline.com

or Postgres in order to create and query relational databases.

Indications for Postgres installation are in the Moodle. With Postgres, you can use the psql
client in order to interact with the DBMS. To run the client nd the related icon in the
Postgres directory and launch it by double clicking on the icon.

The client will allow for interactions via a prompt (e.g., postgres=# ) allowing you for writing
and executing SQL commands. Observe that there is a ‘by-default’ database called
postgres to which you are initially connected.

Here is a list of useful psql commands, including \c to change the current database, after
you have created it with CREATE DATABASE dbname; .

• \q to quit pgsql;
• \l to obtain the list of available databases;
• \c to connect to a particular database;
• \d to have the list of tables in the current database;
• \d to describe a table (the table name must provided as the argument of the command,
e.g. \d egg for describing the egg table);
• \i le-adresss, to execute SQL commands stored in a separate le.
fi
fi
fi
needed to make the expression meaningful:

(1) R1 ∪R2,
MSC Data Science (2) R1 ∩R2, (3) R1 − R2, (4) R1 × R2, (5) σ
for Business a=5 (R1),2023
Jannuary (6) πa (R1),
and (7) R1/R2
Answer 4.2 See Figure 4.1.
Exercice 1. SQL

ConsiderExercise Consider
4.3relational
the following the following schema:
schema.

Suppliers(sid: integer, sname: string, address: string)


Parts(pid: integer, pname: string, color: string)
Catalog(sid: integer, pid: integer, cost: real)

The keyThe eldskey


are fields are underlined,
underlined, and the domain andofthe
eachdomain
eld is of each
listed field
after the iseld
listed after the fi
name.
name.
Therefore sid isTherefore
the key forsid is the key
Suppliers, pid for Suppliers,
is the pid isand
key for Parts, the sid
keyand forpid
Parts, and sid and p
together
form thetogether
key for form
Catalog.the The
key Catalog
for Catalog.
relationThe
listsCatalog relation
the prices chargedlistsforthe prices
parts by charged
Suppliers. Write
parts bythe following queries
Suppliers. in SQL.
Write the For thequeries
following sake of in
verifying the correctness
relational of
algebra, tuple relatio
your answers, the expected result of each query is reported in the Appendix.
calculus, and domain relational calculus:

1. 42part.
Find the names of suppliers who supply some red

2. Find the names of suppliers who supply some red or green part.

3. Find the sids of suppliers who supply some red part or are at Illinois.

4. Find the sids of suppliers who supply some red part and some green part.

5. Find the sids of suppliers who do not supply red parts.

6. Find the sids of suppliers who supply every part.

7. Find the sids of suppliers who supply every red part.

8. Find the sids of suppliers who supply every red or green part.

9. Find the sids of suppliers who supply every red part or supply every green part.

10. Find pairs of sids such that the supplier with the rst sid charges more for some part
than the supplier with the second sid.

11. Find the pids of parts supplied by at least two different suppliers.

12. Find the pids of the most expensive parts supplied by suppliers named Alien Aircaft
Inc.

13. Find for each supplier the most expensive part he/she supplies
fi
fi
fi
fi
MSC Data Science for Business Jannuary 2023
To create and ll the database please use the following commands in the next page.

In Postgres rst create a database named ‘tp’ for the exercise:

create database tp;


Then connect to that database:

\c tp;

With sqliteonline.com you run/execute directly commands in the SQL form.

And then execute the following commands one after the other :

create table suppliers(sid int primary key, sname text, address text);
create table parts(pid int primary key, pname text, color text);
create table catalog(sid int, pid int, cost real,
constraint pk_cat primary key(sid, pid),
constraint fk_sup foreign key(sid) references suppliers,
constraint fk_par foreign key(pid) references parts
);
fi
fi
MSC Data Science for Business Jannuary 2023
INSERT INTO parts (pid, pname, color) VALUES
(1,'Left Handed Bacon Stretcher Cover','Red'),
(2,'Smoke Shifter End','Black'),
(3,'Acme Widget Washer','Red'),
(4,'Acme Widget Washer','Silver'),
(5,'I Brake for Crop Circles Sticker','Translucent'),
(6,'Anti-Gravity Turbine Generator', 'Cyan'),
(7,'Anti-Gravity Turbine Generator',' Magenta'),
(8,'Fire Hydrant Cap','Red'),
(9,'7 Segment Display','Green');

INSERT INTO suppliers (sid, sname, address) VALUES


(1,'Acme Widget Suppliers','Illinois'),
(2,'Big Red Tool and Die','Oregon'),
(3,'Perfunctory Parts','Texas'),
(4,'Alien Aircaft Inc.','Nevada');

INSERT INTO catalog (sid, pid, cost) VALUES


(1,3,0.50),
(1,4,0.50),
(1,8,11.70),
(2,3,0.55),
(2,8,7.95),
(2,1,16.50),
(3,8,12.50),
(3,9,1.00),
(4,5,2.20),
(4,6,1247548.23),
(4,7,1247548.23);
MSC Data Science for Business Jannuary 2023
Appendix: Expected results for each query.

1- Find the names of suppliers who supply some red part.

sname

-----------------------

Acme Widget Suppliers

Big Red Tool and Die

Perfunctory Parts

(3 rows)

2. Find the sids of suppliers who supply some red or green part.

sname

-----------------------

Acme Widget Suppliers

Big Red Tool and Die

Perfunctory Parts

(3 rows)

3.Find the sids of suppliers who supply some red part or are at Illinois.

sid

-----

(3 rows)
MSC Data Science for Business Jannuary 2023
4.Find the sids of suppliers who supply some red part and some green part.

sid

-----

(1 row)

5. Find the sids of suppliers who do not supply red parts.

sid

-----

(1 row)

6.Find the sids of suppliers who supply every part.

sid

-----

(0 rows)

7.Find the sids of suppliers who supply every red part.

sid

-----

(1 row)
MSC Data Science for Business Jannuary 2023
8. Find the sids of suppliers who supply every red or green part.

sid

-----

(0 rows)

9.Find the sids of suppliers who supply every red part or supply every green part.

sid

-----

(2 rows)

10. Find pairs of sids such that the supplier with the rst sid charges more for some part
than the supplier with the second sid.

sid_exp | sid_cheap
---------+-----------
2| 1
1| 2
3| 1
3| 2
(4 rows)
fi
MSC Data Science for Business Jannuary 2023
11. Find the pids of parts supplied by at least two different suppliers.

pid

-----

(2 rows)

12. Find the pids of the most expensive parts supplied by suppliers named Alien Aircaft
Inc.

pid

-----

(2 rows)

13. Find for each supplier the most expensive part he/she supplies.

sname | pid | cost

-----------------------+-----+-------------

Acme Widget Suppliers | 8 | 11.7

Big Red Tool and Die | 1 | 16.5

Perfunctory Parts | 8| 12.5

Alien Aircaft Inc. | 6 | 1.24755e+06

Alien Aircaft Inc. | 7 | 1.24755e+06

(5 rows)

You might also like