Uploaded by MOHAMMED AL YEMENI

Data base

advertisement
DATABASE
[DataBase Project
DECEMBER 6, 2020
KING KHALID UNIVERSITY
[Company address]
Problem Statement:
A company specializing in the archiving and printing of digital photos wishes to implement a
new database for its management. The database must respect the following constraints: - A
customer is identified by a unique number, and is characterized by his name, his postal address
(considered as a single field) and his e-mail address. - Each digital photo is identified by a
unique number. A photo is also characterized by a file name, the date of its download, and the
photo's metadata containing the type of the image: jpeg, gif, .. The photos are of two types.
The identity image type is characterized by a specific size. The type of landscape image is
characterized by its color and framing. - An order is characterized by its number and its
creation date. - A photo print is characterized by the customer who has placed a given order
and including the list of photos forming part of the order. - Some photos can be presented in
one or more album(s). An album is identified by a unique number, a label and a creation date.
It is possible to save the number of photos per album
Part1 (TBA- blackboard) ER Model (CLO 2.3)
ER model
Figure 1 ER Model
Part2 (TBA- blackboard) Mapping (CLO 2.3)
Figure 2 Mapping of ER model











Customer(customer_number,name,postal_address,email)
Order(order_number,customer_number,creation_date)
FK: customer_number references Customer(customer_number)
Album(album_number,creation_date,label,order_number)
FK: order_number references Order(order_number)
Photo(photo_number,download_date,metadata,file_name,type,color,framing,size)
Photo_print(customer_number, order_number, album_number,photo_number)
FK: customer_number references Customer(customer_number)
FK: order_number references Order(order_number)
FK: album_number references Album(album_number)
FK: photo_number references Photo(photo_number)
Part3 (TBA- blackboard) (CLO 1.1)
3. Write the SQL DDL code for the creation of different tables:
Creating tables:
drop table CLIENT cascade constraint;
create table CLIENT (
NUM_CLI number,
NAME_CLI varchar2 (25) not null,
ADR_CLI varchar2 (150) not null,
EMAIL_CLI varchar2 (30) not null unique,
constraint CLIENT_PK primary key (NUM_CLI));
drop table ORDERS cascade constraint;
create table ORDERS (
NUM_ORD number,
DATE_ORD date not null,
constraint ORDERS_PK primary key (NUM_ORD));
drop table PHOTO cascade constraint;
create table PHOTO (
NUM_PHO number,
FILE_PHO varchar2 (25) not null,
DOW_DAT_PHO date,
MET_D_PHO varchar2 (10) not null,
constraint PHOTO_PK primary key (NUM_PHO));
drop table P_PRINT cascade constraint;
create table P_PRINT (
NUM_CLI number,
NUM_ORD number,
NUM_PHO number,
N_EXEMP varchar2 (10) not null,
constraint P_PRINT_PK primary key (NUM_CLI,NUM_ORD,NUM_PHO),
constraint P_PRINT_FK1 foreign key (NUM_CLI) references CLIENT
(NUM_CLI),
constraint P_PRINT_FK2 foreign key (NUM_ORD) references ORDERS
(NUM_ORD),
constraint P_PRINT_FK3 foreign key (NUM_PHO) references PHOTO
(NUM_PHO)
);
4. Fill the tables with different data to different fields.
INSERTING TO THE TABLES
insert into CLIENT values(1, 'Ahmed Abdullah', 'Riyadh, Saudi
Arabia', 'ahmed.abdullah@mail.com');
insert into CLIENT values(2, 'Nada Karim', 'Mecca, Saudi Arabia',
'Nada.Karim@mail.com');
insert into CLIENT values(3, 'Eman Mohammed', 'Abha, Saudi Arabia',
'Eman.Mohammed@mail.com');
insert into ORDERS values(1, '10-Oct-2020');
insert into ORDERS values(2, '03-Nov-2020');
insert into ORDERS values(3, '9-Nov-2020');
insert into PHOTO values(1, 'file1','14-Nov-2020','jpeg');
insert into PHOTO values(2, 'file2','11-Nov-2019','gif');
insert into PHOTO values(3, 'file3','12-Nov-2020','jpeg');
insert into P_PRINT values(1, 2,1,10);
insert into P_PRINT values(2, 3,2,6);
insert into P_PRINT values(1, 1,2,2);
Part4 (TBA- blackboard) RELATIONAL ALGEBRA & SQL-DML(CLO 2.1)
USING SQL:
5.1. List the emails of all clients. (Relation Algebra-SQL)
select c.EMAIL_CLI as email from CLIENT c;
5.2. List of all the orders placed on 03-11-2020. (Relation Algebra-SQL)
select * from ORDERS where DATE_ORD = '03-Nov-2020';
5.3. List of all files of the photos ordered on 03-11-2020.
select p.FILE_PHO from PHOTO p inner join P_PRINT pr on pr.NUM_PHO =
p.NUM_PHO inner join
ORDERS O on O.NUM_ORD = pr.NUM_ORD where DATE_ORD = '03-Nov-2020';
5.4. List of all files of the photos ordered on 03-11-2020 as well as the name of their respective
client. (Relation Algebra-SQL)
select p.FILE_PHO, c.NAME_CLI from PHOTO p inner join P_PRINT pr on
pr.NUM_PHO = p.NUM_PHO inner join ORDERS O on O.NUM_ORD = pr.NUM_ORD
inner join CLIENT C on c.NUM_CLI = pr.NUM_CLI where DATE_ORD = '03Nov-2020';
5.5. Find the list of photos that have not been printed. (Relation Algebra-SQL)
select p.* from PHOTO p where p.NUM_PHO not in (select pr.NUM_PHO
from P_PRINT pr);
5.6 Give the number of photos (NUM_PHO) that are printed or that are downloaded before 0101-2020. (use the union operator) (Relation Algebra-SQL)
select count(NUM_PHO) as count_of_photos from ( select p.NUM_PHO
from PHOTO p where DOW_DAT_PHO < '01-Jan-2020' UNION select
pr.NUM_PHO from P_PRINT pr);
5.7 Give the number of photos (NUM_PHO) that are printed with exemplary number more than 5
and that are downloaded before 13-05-2020. (use the intersect operator)
select count(NUM_PHO) as count_of_photos from ( select pr.NUM_PHO
from P_PRINT pr where N_EXEMP > 5 intersect select p.NUM_PHO from
PHOTO p where DOW_DAT_PHO < '13-May-2020' );
5.8 Find all photos in “jpeg” type.
select * from PHOTO where MET_D_PHO like '%jpeg%';
5.9. For each client, give the total of the exemplary printed
select count(N_EXEMP) count_exemplary_printed, NUM_CLI from P_PRINT
group by NUM_CLI;
5.10. For each client give the number of orders during the year 2020; the result must be given in ascending
order of client number. (SQL
select c.NUM_CLI, count(pr.NUM_ORD) as count_clients from CLIENT c
inner join P_PRINT pr on c.NUM_CLI = pr.NUM_CLI
5.11 Give the biggest exemplary number
select max(N_EXEMP) biggest_exemplary_number from P_PRINT;
Using Relational Algebra
1-List the emails of all clients
∏EMAIL_CLI (CLIENT)
2- List of all the orders placed on 03-11-2020
σDATE_ORD = ’03-NOV-2020’ (ORDERS)
3- Give the number of photos (NUM_PHO) that are printed or that are
downloaded before 01-01-2020. (use the union operator)
P1 =∏ NUM_PHO (σ DOW_DAT_PHO < '01-Jan-2020' (Photo))
P2 =∏ NUM_PHO
(P_PRINT)
P = Gcount(NUM_PHO)( P1 ⋃ P2)
4Give the number of photos (NUM_PHO) that are printed with exemplary
number more than 5 and that are downloaded before 13-05-2020
P1 =∏NUM_PHO (σ DOW_DAT_PHO < '13-May-2020' (Photo))
P2 =∏NUM_PHO
(σ N_EXEMP > 5 )P_PRINT))
P = Gcount(NUM_PHO)( P1 ⋂ P2)
5- List of all files of the photos ordered on 03-11-2020
∏ FILE_PHO (σDATE_ORD = ’03-NOV-2020’ (ORDERS ⋈ (P_PRINT ⋈ PHOTO)))
6. Translate 10 queries from the questions below (from 5.1 to 5.13) into SQL DML queries
The code:
7- Include all parts from 1 to 4 in the report .Add screenshots of the different results obtained for each SQL
query.
This part is is done in above parts(1-4)
Download