2. b)
Primary Key
1) Title in movie table is a primary key
2) Director name in director table is a primary key
3) Producer name in production company table is a primary key
4) Actor name in actor table is a primary key.
Foreign Key
There is no foreign key in the tables.
3. c)
SQL Code
CREATE TABLE Movie (
Id NOT NULL PRIMARY KEY,
Title varchar(255) NOT NULL,
Length varchar(255),
Genre varchar(255),
Quotes varchar (255),
Release Year Date,
);
CREATE TABLE Director (
ID int NOT NULL PRIMARY KEY,
Dir Name varchar(255) NOT NULL,
Dir_DOB varchar(255),
);
4. CREATE TABLE Actor (
ID int NOT NULL PRIMARY KEY,
Actor_Name varchar(255) NOT NULL,
Actor_DOB varchar(255),
);
5. Question 3
SQL Statements
a)
SQL Left Join
SQL left join returns all the data from the left table and left join returns only matched data from the right
table.
SQL Right join
SQL right join returns all the data from the right table and returns only matched data from the left table.
Example:
There is a three table in the library database. First table is the Members records, second table is the books
records and the third table is the borrowed records.
Now we apply both left and right join on the above table.
SQL query:
Left join
“ Select members.member_id, borrowed.member_id
from members
Left join borrowed
on
members.member_id = borrowed.member_id
Group By
borrowed.member_id”
“Above SQL statement return all the member id from the member table and returns only matched id from
the borrowed table, This is called left join”.
Right Join
“ Select members.member_id, borrowed.member_id
from members
Right Join borrowed
6. on
members.member_id = borrowed.member_id
Group By
borrowed.member_id”
“Above SQL statement return all the member id from the borrowed table and returns only matched id from
the member table, this is called right join”.
b)
SQL query
“SELECT count(*) AS ‘noOfBook’
FROM borrowed
WHERE
Member_id =
(SELECT member_id
FROM member WHERE
name = ‘Ali Smith’)”;
c)
SQL Query
“SELECT members.member_id ,
members.name,
borrowed.member_id
FROM members
Inner Join borrowed
ON
Members.member_id = borrowed.member_id
Group By borrowed.member_id
Having borrowed.member_id > 20 ”;
7. d)
SQL Query
“SELECT book.ISBN, book.authors, book.publisher,
borrowed.ISBN
FROM book
Group By borrowed.ISBN
Having borrowed.ISBN > 10
Where book.publisher = ‘UoL Press’”;