Assignment# 3: Name: Mah Noor (f2021105007) Sehar Maqsood (f2021105137) Section: Y3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Assignment# 3

Name: Mah Noor (f2021105007)

Sehar Maqsood (f2021105137)


Section: Y3

1|Page
Question # 1: Convert the ER/EER to Relational schema (in SQL) using
the conversion steps/rules discussed in the theory Week10 lecture
slides. You may choose any entities of

your choice.
a) Strong Entity set with Simple attributes (05 points)

create database db_2

2|Page
go

use db_2

go

create table tbl_bookshop

book_id int not null unique,

book_name varchar(25),

book_author varchar(15),

book_price money,

constraint pk_primary_key primary key (book_id)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values (1, 'John Green', 'Looking for Alaska', 1800)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(2, 'Paulo Coelho', 'The Alchemist', 2000)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(3, 'Elif Shafak', 'The Forty Rules Of Love', 2500)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(4, 'Colleen Hoover', 'Ugly Love', 1500)

select* from tbl_bookshop

3|Page
b) Strong Entity Set with Composite Attributes (05 points)

4|Page
create table tbl_student

student_id int not null,

student_fname varchar(15),

student_lname varchar(15),

student_age int,

student_department varchar(15),

constraint pk_sprimary_key primary key (student_id)

insert into tbl_student(student_id, student_fname,


student_lname,student_age,student_department)

values(1,'Jazib', 'Bajwa',23,'Marketing')

insert into tbl_student(student_id, student_fname,


student_lname,student_age,student_department)

values(2, 'Kainat', 'Nasir' , 22, 'HR')

5|Pa ge
insert into tbl_student(student_id, student_fname,
student_lname,student_age,student_department)

values(3, 'Ali', 'Haider' , 21, 'IT')

insert into tbl_student(student_id, student_fname,


student_lname,student_age,student_department)

values(4, 'Labina', 'Imran', 20, 'Cardiology')

select * from tbl_student

c) Strong Entity Set with Multi Valued Attributes (10 points)

6|Pa ge
create table tbl_bookshop

book_id int not null,

book_name varchar(25),

book_author varchar(15),

book_price money,

constraint pk_primary_key primary key (book_id)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values (1, 'John Green', 'Looking for Alaska', 1800)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(2, 'Paulo Coelho', 'The Alchemist', 2000)

insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(3, 'Elif Shafak', 'The Forty Rules Of Love', 2500)

7|Page
insert into tbl_bookshop(book_id, book_author, book_name, book_price)

values(4, 'Colleen Hoover', 'Ugly Love', 1500)

select* from tbl_bookshop

create table tbl_bookshop1

book_idd char(10) not null,

book_language varchar(15),

book_id int,

constraint fk_foreign_key foreign key (book_id)

references tbl_bookshop (book_id)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B1', 'English', 1)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B1','Urdu', 1)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B2', 'English',2)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B2', 'Urdu', 2)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B3', 'English', 3)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B3', 'Turkish', 3)

insert into tbl_bookshop1(book_idd, book_language, book_id)

values('B4', 'English', 4)

8|Pa ge
select *from tbl_bookshop1

d) Unary Relationship

• one-to-one (05 points)

9|Pa ge
create table tbl_student1

student_id int not null unique,

student_name varchar(15),

student_age int,

student_semester varchar(15),

roomate_name varchar(15),

constraint pk_s1primary_key primary key (student_id)

insert into
tbl_student1(student_id,student_name,student_age,student_semester,roomate_n
ame)

values(1, 'Haider', 22, '4th' , 'Muneeb')

insert into
tbl_student1(student_id,student_name,student_age,student_semester,roomate_n
ame)

values(2, 'Mahnoor', 20, '4th', 'Rabia')

10 | P a g e
insert into
tbl_student1(student_id,student_name,student_age,student_semester,roomate_n
ame)

values(3, 'Aneeb' , 23,'6th', 'Ateeb' )

insert into
tbl_student1(student_id,student_name,student_age,student_semester,roomate_n
ame)

values(4, 'Kainat', 22, '8th', 'Sadaf')

Select* from tbl_student1

• one to many relations (05 points)

11 | P a g e
create table tbl_teacher

teacher_id int not null,

teacher_name varchar(15),

teacher_phone int,

teacher_course varchar(15),

coworker_name varchar(15)

insert into tbl_teacher(teacher_id, teacher_name, teacher_phone, teacher_course,


coworker_name)

values(1, 'Amna', 5676453, 'Chemistry', 'Maryam')

insert into tbl_teacher(teacher_id, teacher_name, teacher_phone, teacher_course,


coworker_name)

values(2, 'Amna', 063726, 'Chemistry', 'Alveena')

insert into tbl_teacher(teacher_id, teacher_name, teacher_phone, teacher_course,


coworker_name)

values(3, 'Nida', 562883, 'Biology' , 'Ataf')

12 | P a g e
insert into tbl_teacher(teacher_id, teacher_name, teacher_phone, teacher_course,
coworker_name)

values(4, 'Nida', 56383, 'Biology' , 'Aleesha')

insert into tbl_teacher(teacher_id, teacher_name, teacher_phone, teacher_course,


coworker_name)

values(5, 'Salma' ,02615, 'Physics','Sadaf' )

select * from tbl_teacher

• many-to-many relations (05 points)

13 | P a g e
create table tbl_course

course_id int not null,

course_name varchar(15),

course_units int

constraint pk_cprimary_key primary key (course_id)

insert into tbl_course(course_id,course_name,course_units)

values(1, 'SRE' ,3)

insert into tbl_course(course_id,course_name,course_units)

values(2, 'Database', 3)

insert into tbl_course(course_id,course_name,course_units)

values(3, 'Spanish II' ,4)

create table tbl_prerequisite

prerequisite_name varchar(15),

course_id int not null

14 | P a g e
constraint pk_1foreign_key foreign key (course_id)

references tbl_course (course_id)

insert into tbl_prerequisite(prerequisite_name, course_id)

values('SE' , 1)

insert into tbl_prerequisite(prerequisite_name, course_id)

values('DSA' , 2)

insert into tbl_prerequisite(prerequisite_name, course_id)

values('Spanish I' , 3)

select* from tbl_course

select* from tbl_prerequisite

15 | P a g e
e) Binary Relationships
• Binary Relationship with Cardinality Ratio m: n (10 points)

16 | P a g e
create table tbl_order

order_id int not null,

order_amount money

constraint pk_mprimary_key primary key (order_id)

insert into tbl_order(order_id, order_amount)

values(1, 1500)

insert into tbl_order(order_id, order_amount)

values(2, 2000)

insert into tbl_order(order_id, order_amount)

values(3, 2500)

insert into tbl_order(order_id, order_amount)

values(4,3000)

17 | P a g e
create table tbl_make

order_id int not null,

product_id int not null

insert into tbl_make(order_id, product_id)

values(1,101)

insert into tbl_make(order_id, product_id)

values(2, 102)

insert into tbl_make(order_id, product_id)

values(3, 103)

insert into tbl_make(order_id, product_id)

values(4, 104)

create table tbl_product

product_id int not null,

product_number int

constraint pk_pprimary_key primary key (product_id)

insert into tbl_product(product_id, product_number)

values(101, 100)

insert into tbl_product(product_id, product_number)

values(102, 200)

insert into tbl_product(product_id, product_number)

values(103, 150)

18 | P a g e
insert into tbl_product(product_id, product_number)

values(104, 180)

select * from tbl_order

select* from tbl_product

select* from tbl_make

• Binary Relationship with Cardinality Ratio 1: n (10 points)

19 | P a g e
create table tbl_course

course_id int not null,

course_name varchar(15),

course_units int

constraint pk_cprimary_key primary key (course_id)

insert into tbl_course(course_id,course_name,course_units)

values(1, 'SRE' ,3)

insert into tbl_course(course_id,course_name,course_units)

values(2, 'Database', 3)

insert into tbl_course(course_id,course_name,course_units)

values(3, 'Spanish II' ,4)

select* from tbl_course

create table tbl_section

section_number int not null,

semester_id varchar(10) ,

20 | P a g e
course_idd int

constraint pk_seprimary_key primary key (section_number)

constraint fk_sforeign_key foreign key (course_idd)

references tbl_course(course_id)

insert into tbl_section(section_number, semester_id, course_idd)

values(101, '4th', 1)

insert into tbl_section(section_number, semester_id, course_idd)

values(102, '2nd', 2)

insert into tbl_section(section_number, semester_id, course_idd)

values(103, '7th', 3)

select *from tbl_section

select* from tbl_course

21 | P a g e
• Binary Relationship with Cardinality Ratio m:1 (10 points)

22 | P a g e
create table tbl_player

player_id int not null,

player_name varchar(15),

t_id int

constraint pk_piprimary_key primary key (player_id)

constraint fk_tforeign_key foreign key (t_id)

references tbl_team (team_id)

insert into tbl_player(player_id, player_name, t_id)

values(1, 'Ali', 101)

insert into tbl_player(player_id, player_name, t_id)

values(2, 'Haider', 102)

insert into tbl_player(player_id, player_name, t_id)

values(3, 'Ahmad',101)

23 | P a g e
insert into tbl_player(player_id, player_name, t_id)

values(4,'Mubashir', 103)

insert into tbl_player(player_id, player_name, t_id)

values(5, 'Junaid', 103)

create table tbl_team

team_id int not null,

team_name varchar(10)

constraint pk_ttprimary_key primary key (team_id)

insert into tbl_team(team_id, team_name)

values(101, 'Thunder')

insert into tbl_team(team_id, team_name)

values(102, 'Gladiators')

insert into tbl_team(team_id, team_name)

values(103, 'Strikers')

select* from tbl_player

select* from tbl_team

24 | P a g e
• Binary Relationship with Cardinality Ratio 1:1
o Both sides have partial participation. (10 points)

25 | P a g e
create table tbl_manager

manager_id int not null unique,

manager_name varchar(10),

d_id int

constraint pk_mgprimary_key primary key (manager_id)

constraint fk_dforeign_key foreign key (d_id)

references tbl_department (department_id)

insert into tbl_manager(manager_id,manager_name, d_id)

values(101, 'Ahmad' , 1)

insert into tbl_manager(manager_id,manager_name, d_id)

values(102, 'Zayan' , 2)

insert into tbl_manager(manager_id,manager_name, d_id)

values(103, 'Altamash', 3)

26 | P a g e
create table tbl_department

department_id int not null unique,

department_name varchar(15)

constraint pk_dprimary_key primary key (department_id)

insert into tbl_department(department_id,department_name)

values (1, 'HR')

insert into tbl_department(department_id,department_name)

values (2, 'IT')

insert into tbl_department(department_id,department_name)

values (3,'Cybersecurity')

select* from tbl_department

select * from tbl_manager

27 | P a g e
o One side has partial participation. (10 points)

28 | P a g e
create table tbl_manager

manager_id int not null unique,

manager_name varchar(10),

constraint pk_mgprimary_key primary key (manager_id)

insert into tbl_manager(manager_id,manager_name)

values(101, 'Ahmad' )

insert into tbl_manager(manager_id,manager_name)

values(102, 'Zayan' )

insert into tbl_manager(manager_id,manager_name)

values(103, 'Altamash')

create table tbl_department

department_id int not null unique,

29 | P a g e
department_name varchar(15),

m_id int

constraint pk_dprimary_key primary key (department_id)

constraint fk_dhforeign_key foreign key (m_id)

references tbl_manager (manager_id)

insert into tbl_department(department_id,department_name, m_id)

values (1, 'HR', 101)

insert into tbl_department(department_id,department_name, m_id)

values (2, 'IT', 103)

insert into tbl_department(department_id,department_name, m_id)

values (3,'Cybersecurity', 102)

select* from tbl_department

select * from tbl_manager

30 | P a g e
o Both sides have total participation. (10 points)

31 | P a g e
create table tbl_manages

(manager_id int not null unique,

manager_name varchar(10),

department_id int not null unique,

department_name varchar(15)

insert into
tbl_manages(manager_id,manager_name,department_id,department_name)

values(101, 'Ahmad', 1, 'HR')

insert into
tbl_manages(manager_id,manager_name,department_id,department_name)

values(102, 'Zayan', 2, 'IT')

insert into
tbl_manages(manager_id,manager_name,department_id,department_name)

values(103, 'Altamash', 3,'Cybersecurity')

select * from tbl_manages

32 | P a g e
f) Binary Relationship with Weak Entity Set (10 points)

33 | P a g e
create table tbl_customer

customer_id int not null,

customer_name varchar(15),

customer_address varchar(15),

customer_phone int

constraint pk_cjprimary_key primary key (customer_id)

insert into tbl_customer(customer_id, customer_name,


customer_address,customer_phone)

values(1, 'Ahmad', 'DHA LAHORE', 12345699)

insert into tbl_customer(customer_id, customer_name,


customer_address,customer_phone)

values(2, 'Jazib', 'Almere Holland' ,2347596)

insert into tbl_customer(customer_id, customer_name,


customer_address,customer_phone)

values(3, 'Kainat', 'Ontario Canada' ,7832346)

34 | P a g e
create table tbl_loan

loan_id int not null unique,

loan_date date ,

c_id int

constraint pk_lprimary_key primary key (loan_id)

constraint fk_lforeign_key foreign key (c_id)

references tbl_customer (customer_id)

insert into tbl_loan(loan_id, loan_date, c_id)

values(101, '2002/02/12', 1)

insert into tbl_loan(loan_id, loan_date, c_id)

values(202, '1999/12/04', 2)

insert into tbl_loan(loan_id, loan_date, c_id)

values(304, '2000/08/18', 3)

select* from tbl_customer

select* from tbl_loan

35 | P a g e
g) Ternary Relationship (10 points)

36 | P a g e
create table tbl_chemist

chemist_id int not null unique ,

chemist_name varchar(15),

chemist_phone int,

constraint pk_hfprimary_key primary key (chemist_id)

insert into tbl_chemist(chemist_id, chemist_name, chemist_phone)

values(1, 'Jude', 123466 )

insert into tbl_chemist(chemist_id, chemist_name, chemist_phone)

values(2, 'Alex' ,2457785)

insert into tbl_chemist(chemist_id, chemist_name, chemist_phone)

values(3, 'Zade' , 3786587)

create table tbl_equipment

37 | P a g e
serial_no varchar(10) not null unique,

equipment_cost money

constraint pk_ghyprimary_key primary key (serial_no)

insert into tbl_equipment(serial_no, equipment_cost)

values('GH-13456', 2000)

insert into tbl_equipment(serial_no, equipment_cost)

values('XY-6795', 1000)

insert into tbl_equipment(serial_no, equipment_cost)

values('AB-4557', 1500)

create table tbl_project

project_id int not null unique ,

project_date date

constraint pk_dddprimary_key primary key (project_id)

insert into tbl_project(project_id,project_date)

values(101, '2000/12/01')

insert into tbl_project(project_id,project_date)

values(401, '2005/08/19')

insert into tbl_project(project_id,project_date)

values(901, '2023/02/22')

38 | P a g e
create table tbl_assigned

p_id int not null,

c_id int not null,

s_no varchar(15),

assigned_date date

insert into tbl_assigned(p_id, c_id, s_no, assigned_date)

values(1, 101, 'GH-13456', '2000/01/13')

insert into tbl_assigned(p_id, c_id, s_no, assigned_date)

values(2, 401, 'XY-6795' , '2001/12/24')

insert into tbl_assigned(p_id, c_id, s_no, assigned_date)

values(3, 901, 'AB-4557' , '2002/3/29')

select * from tbl_chemist

select * from tbl_equipment

select* from tbl_project

select* from tbl_assigned

39 | P a g e
h) Mapping EER Diagrams to Relational Schema
• Create a separate relation for each superclass and subclass. (10
points)

40 | P a g e
create table tbl_person

person_id int not null unique,

person_name varchar(10),

person_age int

constraint pk_asprimary_key primary key (person_id)

insert into tbl_person(person_id,person_name,person_age)

values(1, 'Jazib', 23)

insert into tbl_person(person_id,person_name,person_age)

values(2, 'Haider', 21)

insert into tbl_person(person_id,person_name,person_age)

41 | P a g e
values(3, 'Zayan', 18)

create table tbl_student1

student_gpa varchar(10),

p_id int

constraint fkpjforeign_key foreign key (p_id)

references tbl_person (person_id)

insert into tbl_student1(student_gpa,p_id)

values('3,19' , 1)

insert into tbl_student1(student_gpa,p_id)

values('3,89',2)

insert into tbl_student1(student_gpa,p_id)

values('4.0' ,3)

create table tbl_employee

employee_salary money,

ps_id int

constraint fk_rrforeign_key foreign key (ps_id)

references tbl_person (person_id)

insert into tbl_employee(employee_salary, ps_id)

values(50000, 1)

42 | P a g e
insert into tbl_employee(employee_salary, ps_id)

values(60000, 2)

insert into tbl_employee(employee_salary, ps_id)

values(80000, 3)

select* from tbl_person

select* from tbl_student1

select* from tbl_employee

43 | P a g e
• Create relations for subclass only. (10 points)
create table tbl_studentt

person_id int not null unique,

person_name varchar (10),

person_age int ,

student_gpa varchar(10),

constraint pk_askprimary_key primary key (person_id)

insert into tbl_studentt(person_id, person_name, person_age,student_gpa)

values(1, 'Jazib', 23, '3.19')

insert into tbl_studentt(person_id, person_name, person_age,student_gpa)

values(2, 'Haider', 21, '3.89')

insert into tbl_studentt(person_id, person_name, person_age,student_gpa)

values(3, 'Zayan', 18, '4.0' )

create table tbl_employeee

person_id int not null unique,

person_name varchar (10),

person_age int ,

employee_salary money,

constraint pk_aslprimary_key primary key (person_id)

44 | P a g e
)

insert into tbl_employeee(person_id, person_name, person_age,employee_salary)

values(1, 'Jazib', 23, 50000)

insert into tbl_employeee(person_id, person_name, person_age,employee_salary)

values(2, 'Haider', 21, 60000)

insert into tbl_employeee(person_id, person_name, person_age,employee_salary)

values(3, 'Zayan', 18, 80000)

select* from tbl_employeee

select* from tbl_studentt

45 | P a g e
• Create a single relation with one type attribute. (10 points)
create table tbl_personn

( person_id int not null unique,

person_name varchar (10),

person_age int ,

employee_salary money,

46 | P a g e
student_gpa varchar(10),

person_type varchar(10)

constraint pk_qweprimary_key primary key (person_id)

insert into tbl_personn(person_id,person_name,person_age,student_gpa,


person_type)

values(1, 'Kainat', 18, '4.0', 'Student')

insert into tbl_personn(person_id,person_name,person_age,employee_salary,


person_type)

values(2, 'Haider', 21, 60000, 'Employee')

insert into tbl_personn(person_id,person_name,person_age,employee_salary,


person_type)

values(3, 'Zayan', 28, 80000, 'Employee')

insert into tbl_personn(person_id,person_name,person_age,student_gpa,


person_type)

values(4, 'Sadaf', 20, '3,54', 'Student')

insert into tbl_personn(person_id,person_name,person_age,student_gpa,


person_type)

values(5, 'Labina', 23, '3.92', 'Student')

select* from tbl_personn

47 | P a g e
• Create a single relation with multiple type attributes. (10 points)

48 | P a g e
create table tbl_personn

( person_id int not null unique,

person_name varchar (10),

person_age int ,

employee_salary money,

student_gpa varchar(10),

person_type varchar(10)

constraint pk_qweprimary_key primary key (person_id)

insert into
tbl_personn(person_id,person_name,person_age,employee_salary,student_gpa,
person_type)

values(1, 'Kainat', 18,23000, '4.0', 'Student')

49 | P a g e
insert into
tbl_personn(person_id,person_name,person_age,employee_salary,student_gpa,
person_type)

values(2, 'Haider', 21, 60000,'2.98', 'Employee')

insert into
tbl_personn(person_id,person_name,person_age,employee_salary,student_gpa,
person_type)

values(3, 'Zayan', 28, 80000,'3,12', 'Employee')

insert into
tbl_personn(person_id,person_name,person_age,employee_salary,student_gpa,
person_type)

values(4, 'Sadaf', 20,30000, '3,54', 'Student')

insert into
tbl_personn(person_id,person_name,person_age,employee_salary,student_gpa,
person_type)

values(5, 'Labina', 23, 50000, '3.92', 'Student')

select * from tbl_personn

50 | P a g e
51 | P a g e
52 | P a g e

You might also like