Professional Documents
Culture Documents
DBMS
Submitted To:
Sir Faizan Ahmad
Submitted By:
Bushra Munawar (2019-CS-622)
Rabia Parveen (2019-CS-602)
Soha Noor (2019-CS-614)
CLASS:
DBMS LAB
SEMESTER:
4th
SECTION:
A
Department of Computer Science, University of Engineering
and Technology, New Main Campus Lahore
Movie Database
ABSTRACT:
SQL stands for Structured Query Language and it is an ANSI standard computer
language for accessing and manipulating database systems. It is used for managing data
in relational database management system which stores data in the form of tables and
relationship between data is also stored in the form of tables. SQL statements are used to
retrieve and update data in a database. Keeping this in mind, we created a Movie
Database and implemented some of the commands which include DML Commands,
DDL Commands, SQL Commands, SQL Joins, Boolean, Relational & Arithmetic
Operators etc. The description of each Query along with their proper syntax and results
is provided.
Here we made database of movie having 4 tables named as movie table, actor
table,rating table and reviwer table. Movie_id is primery key.and use as forign key in
rating id.and similarly reviewer id is also use as foreign key in rating table while
reviwer id iself a unique key.
Actor_Table
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom
Rating_Table
Movie_ID Rev_ID Rev_Stars
A12 12 4
B13 13 3.5
C56 14 2.5
Z11 15 3.7
D7 16 4.8
F9 17 5
S7 18 3.2
M8 19 4.2
N57 20 4
X11 30 3.9
Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Pictorial Representation of Movie Database:
DML Commands:
1) Update:
We will use Update key word to change any value in the table. In the following example we will
change the Rev_Stars of movie_id B13 FROM 3.5 TO 4.5.
Lets see the original table.
Syntax:
UPDATE Rating Table
SET Rev_Stars = 4.5
WHERE movie_id = B13;
After applying above query resulted table is
2) DELETE:
We will use DELETE query to remove some data from table. We can also remove whole
table.
Lets see the original table named as Actor _Table.
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom
Syntax:
DELETE FROM Actor_Table
WHERE Actor_FName = 'Daniel';
After applying query resulted table is:
3) INSERT INTO:
The INSERT INTO statement is used to insert new records in a table.
Syntax:
2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table.
DDL Commands:
4)CREATE:
This command creates table. But here we have to give attributes and data types of attributes too.
Syntax:
Create Table TableName
(attributes);
Example:
Create Table Movie_table
(Movie_ID text not null primary key,
Movie_Title varchar(50),
Release_Date date,
Movie_Language varchar(20),
Country char(20));
Movie_ID Movie_Title Release_Date Movie_Language Country
A table like this will be created.
5) ALTER:
Use Alter command to change the data type of attributes. As well as it is use to add, delete ,drop
specific column from table.in the following example we’ll see how we’ll drop column from
table.
Lets see the original table named as Actor _Table.
Syntax:
ALTER TABLE Actor_Table
DROP COLUMN Contact_link;
After applying query resulted table is following.
6) Drop:
DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view. A DROP statement in SQL removes a
component from a relational database management system (RDBMS). If we want to drop a
column than we have to use drop command with Alter table Command.
Syntax:
DROP TABLE table_name;
Example:
Drop table Movie_Table;
This command will delete the whole Movie_Table from database.
7) COMMENTS:
Comments are used to explain SQL statements. It’ll not effect the execution of statements. For
single line comment use - - and for multiple line comment use /* */ .
Lets see the original table named as reviewed_table.
Syntax:
/*
It will sort the
table in Ascending
order w-r-t rev_id.
By default
*/
SELECT * FROM Reviewer_Table
ORDER BY Rev_ID ASC;
After applying commnets we’ll see no changes will occure.
DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view. A DROP statement in SQL removes a
component from a relational database management system (RDBMS). If we want to drop a
column than we have to use drop command with Alter table Command.
Syntax:
DROP TABLE table_name;
Example:
Drop table Movie_Table;
This command will delete the whole Movie_Table from database.
8). TRUNCATE TABLE
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.
DQL Commands:
9)SELECT *:
If you want to select all the fields available in the table.
SELECT * Syntax:
Here, column1, column2, ... are the field names of the table you want to select data from.
Example:
SELECT Rev_ID, Rev_Name
FROM Reviewer_Table
Rev_ID Rev_Name
12 Chris
13 Amir
14 Raj
15 Soha
16 Rabia
17 Bushra
18 Usman
19 Robert
20 Caroline
21 Bonnie
Constraints in SQL:
The following constraints are commonly used in SQL:
11)Not Null:
By default, a column can hold NULL values. The NOT NULL constraint enforces a column
to not accept NULL values. This enforces a field to always contain a value, which means that
you cannot insert a new record, or update a record without adding a value to this field.
Example:
12)Unique:
The UNIQUE constraint ensures that all values in a column are different. Both
the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has
a UNIQUE constraint.
Example:
13)Primary key:
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must
contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary
key; and in the table, this primary key can consist of single or multiple columns.
Example:
Create Table Reviewer_Table(
Rev_ID int not null unique Primary Key,
Rev_Name Varchar(30),
Rev_Age int);
The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY
KEY in another table. The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.
Example:
Here, Rating_Table uses primary Key (Rev_ID) of Reviewer table as Foreign Key for Relstion
between two tables.
15) Check:
The CHECK constraint is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a column it will allow only certain values for this column. If you
define a CHECK constraint on a table it can limit the values in certain columns based on values
in other columns in the row.
Example:
The DEFAULT constraint is used to set a default value for a column. The default value will be
added to all new records, if no other value is specified.
Example:
Create Table Reviewer_Table(
Rev_ID int not null unique Primary Key,
Rev_Name Varchar(30),
Rev_Age int Default 20,
);
Here, if we don’t insert age then by default it will be 20.
SQL JOINS:
16). INNER JOIN:
A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.
Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Syntax:
SELECT Rating_Table.Rev_ID, Reviewer_Table.Mov_ID, Rating.Rev_Stars
FROM Rating_Table INNER JOIN
ON Reviewer_Table Rating_Table.Rev_ID = Reviewer_Table.Rev_ID;
Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Rating _Table
Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID
FROM Reviewer_Table Full Outer JOIN
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;
Rev_Name Movie_ID
Chris A12
Amir B13
Raj C56
Soha Z11
Rabia D7
Bushra F9
Usman S7
Robert M8
Caroline N57
Bonnie X11
18). RIGHT JOIN:
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no match.
Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Rating _Table
Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID, Rating_Table.Rev_Stars
FROM Reviewer_Table
RIGHT JOIN Rating_Table
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;
Reviewer_Table
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
14 Raj 25
15 Soha 26
16 Rabia 22
17 Bushra 25
18 Usman 35
19 Robert 32
20 Caroline 18
30 Bonnie 16
Rating _Table
Syntax:
SELECT Reviewer_Table.Rev_Name, Rating_Table.Mov_ID
FROM Reviewer_Table
LEFT JOIN Rating_Table
ON Reviewer_Table.Rev_ID = Rating_Table.Rev_ID;
ORDER BY Reviewer_Table.Rev_Name;
Rev_Name Movie_ID
Chris A12
Amir B13
Raj C56
Soha Z11
Rabia D7
Bushra F9
Usman S7
Robert M8
Caroline N57
Bonnie X11
Other Queries:
20) ORDER BY:
By default, The ORDER BY keyword sorts the records in ascending order. The ORDER
BY keyword is used to sort the result in ascending or descending order. To sort the records in
descending order, We’ll use the DESC keyword.
SYNTAX:
21) Distinict :
We use this Distinct key word to remove the duplication in table.
Lets see the original table named as Movie_Table
Syntax:
Country
USA
USA
Pakistan
UK
UK
India
UK
South Korea
USA
Japan
Germany
Germany
Syntax:
SELECT TOP 6 * FROM movie_table;
23) Concatenation :
We use Concatenation to combine 2 strings from different columns and make sing string having
1 column hading.
Lets have an example Actor_Table.
SYNTAX:
select Actor_FName + ‘ ,’ + Actor_LName as Actor_Name from Actor_Table
After applying concatenation see the final column.
Actor_Name
Tom , Cruise
Raj , Kumar
Jahnvi , kapoor
Hanery , Cavil
Millie , Brown
Emma , Watson
Scarlet , Johanson
Robert , Pattinson
Elizbeth , Olsen
Chris , Evans
Loius , Hoffman
Park , Shinhye
24) MIN() :
We use Min() function to find the smallest value from selected column
Lets see a example .we have table names as rating_table
25) MAX():
We use Max() function to find the largest value from selected column.
Lets see a example .we have table names as rating_table
SYNTAX:
SELECT MAX(Rev_Stars) AS Highest_Rev
FROM RATING_TABLE;
26) LAST():
We will get Last Value from movie title of mavie_table.
Lets see the example. We have movie_table.
SYNTEX:
SELECT LAST(Movie_Title) AS Last_Movie
FROM Movie_Table;
After applying function lets see the result.
Last_Movie
The Rain
27) FIRST():
We will get First Value from movie title of movie_table.
Let’s see the example. We have movie_table.
SYNTEX:
SELECT First(Movie_Title) AS First_Movie
FROM Movie_Table;
After applying function lets see the result.
First_Movie
The Croods
28). SUM:
It is function used to return sum of values from a particular column
Syntax:
SELECT SUM(Rev_Age)
FROM Reviewer_Table;
The result of above Query is:
29). AVG:
It is used to aggregate a numeric column and return its average.
Syntax:
SELECT AVG(column_name)
FROM table_name;
Actor Table
Actor_ID Actor_FName Actor_LName Actor_Gender Contact_Link
AB1 Tom Cruise Male tom@gmail.com
AB2 Raj Kumar Male Raj1@gmail.com
AB3 Jahnvi kapoor Female Kapoor@gmail.com
AB4 Hanery Cavil Male Cavil@gmail.com
AB5 Millie Brown Female Bron@gmail.com
AB6 Emma Watson Female Wat23@gmail.com
AB7 Scarlet Johanson Female John@gmail.com
AB8 Daniel Radcliff Male Rad@gmail.com
AB9 Robert Pattinson Male Robert@gmail.com
AB10 Elizbeth Olsen Female Olsen@gmail.com
AB11 Chris Evans Male Chris1@gmail.com
AB12 Loius Hoffman Male Lois@gmail.com
AB13 Park Shinhye Female shin@gmailcom
31).UNION:
With UNION, two SELECT statements can be combined into one result set (allowing
only distinct values).
Let’s see the example. We have movie_table & Rating_Table respectively.
Movie_ID Movie_Title Release_Date Movie_Language Country
A12 The Croods 2016-10-05 English USA
B13 Mission Impossible 2010-06-28 English USA
C56 The Avengers 2013-02-15 Urdu Pakistan
Z11 Witcher 2020-08-20 English UK
D7 Harry Potter 2001-02-30 English UK
F9 Roohi 2021-04-20 Hindi India
S7 Enola Holmes 2021-01-13 English UK
M8 Alive 2020-06-20 Korean South Korea
N57 Aquaman 2018-07-25 English USA
X11 A silent voice 2016-05-30 Japanese Japan
O9 Dark 2017-10-25 German Germany
A51 The Rain 2019-08-18 German Germany
Syntax:
SELECT Movie_ID FROM Movie_Table
UNION
SELECT Movie_ID FROM Rating_Table
ORDER BY Movie_ID;
The resulting table will look like:
Movie_ID
A12
B13
C56
Z11
D7
F9
S7
M8
N57
X11
O9
A51
Syntax:
SELECT Movie_ID FROM Movie_Table
UNION ALL
SELECT Movie_ID FROM Rating_Table
ORDER BY Movie_ID;
The resulting table will look like:
Movie_ID
A12
A12
B13
B13
C56
C56
Z11
Z11
D7
D7
F9
F9
S7
S7
M8
M8
N57
N57
X11
X11
O9
A51
Syntax:
33). HAVING:
It is used in SQL because the WHERE keyword cannot be used in aggregating functions
Let’s see the example. We have Rating_table.
Syntax:
SELECT COUNT(Rev_ID), Rev_Stars
FROM Rating_Table
GROUP BY Rev_Stars;
HAVING COUNT(Rev_ID) > 15
After implementing the query, we obtain the following result.
Rev_ID Rev_Stars
16 4.8
17 5
18 3.2
19 4.2
20 4
30 3.9
Syntax:
SELECT COUNT(Rev_ID), Rev_Stars
FROM Rating_Table
GROUP BY Rev_Stars;
The result set will look like:
Rev_ID Rev_Stars
12 4
13 3.5
14 2.5
15 3.7
16 4.8
17 5
18 3.2
19 4.2
30 3.9
The above table does not contain the row with Rev_ID = 20
35).WHERE :
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified
condition. The Where Cluase can be used with multiple operators such as: ‘AND’, ‘OR’ , ‘NOT’
,’IS NULL’, ‘IS NOT NULL’,’LIKE’, ’=’, ‘>’, ’<’, ’>=’, ‘<=’, ’+’,’-‘ etc. and many more.
Movie_Title
Alive
Aquaman
A silent voice
It showed the result of those Movie_Title whose name begin with letter ‘A’.
36). AS:
It is an keyword in SQL that is used to rename a column or table using an alias name.
Syntax:
SELECT Movie_Title AS Mov_Name
FROM Customers;
Syntax:
SELECT m.Movie_ID, m.Movie_Title, r.Rev_Stars
FROM Movie_Table AS m, Rating_Table AS r
WHERE m.Movie_Title = "Witcher" AND m.Movie_ID = r.Movie_ID;
37).ROUND:
It is a function that takes the column name and a integer as an argument, and rounds the values in
a column to the number of decimal places specified by an integer.
Syntax:
SELECT ROUND(3.9, 1) AS Round_Value;
Round_Value
1 4.0
Syntax:
Example:
WHERE Rev_Name LIKE 'a%' Finds any values that start with "a"
WHERE Rev_Name LIKE '%a' Finds any values that end with "a"
WHERE Rev_Name LIKE Finds any values that have "or" in any position
'%or%'
WHERE Rev_Name LIKE '_r%' Finds any values that have "r" in the second position
WHERE Rev_Name LIKE Finds any values that start with "a" and are at least 2 characters in
'a_%' length
WHERE Rev_Name LIKE Finds any values that start with "a" and are at least 3 characters in
'a__%' length
WHERE Rev_Name LIKE Finds any values that start with "a" and ends with "o"
'a%o'
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern
Example:
Select Rev_ID
FROM Reviewer_Table
WHERE Rev_Name Like %a;
Reviewer_Table
Rev_ID Rev_Name
15 Soha
16 Rabia
17 Bushra
40). In Operator:
The IN operator allows you to specify multiple values in a WHERE clause. We can also use
NOT IN statement.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Example:
SELECT * FROM Reviewer_Table
WHERE Rev_Age IN (35, 25, 16);
Reviewer_Table
Rev_ID Rev_Name Rev_Age
14 Raj 25
17 Bushra 25
18 Usman 35
21 Bonnie 16
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example:
SELECT * FROM Rating_Table
WHERE Rev_Stars BETWEEN 4 AND 5;
Rating_Table
Movie_ID Rev_ID Rev_Stars
A12 12 4
D7 16 4.8
F9 17 5
M8 19 4.2
N57 20 4
In EXCLUSIVE mode, the data of the table cannot be read or modified by another transaction.
In SHARE mode, the data of the table can be read by concurrent transactions but modifications
are still prohibited.
Example
This example locks the Movie_Table so that it can be read but not modified by other
transactions:
Syntax:
LOCK TABLE Movies_Table IN SHARE MODE;
43). UNLOCK TABLE:
The UNLOCK TABLE command is used to unlock a table that has previously been locked via
the LOCK TABLE command. It will not work if it is passed within a transaction or if it is used
on a table that is locked by another process.
Example
Syntax:
IS NULL Syntax:
The IS NULL operator is used to test for empty values (NULL values).
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Example:
SELECT Rev_ID, Rev_Name, Rev_Age
FROM Reviewer_Table
WHERE Rev_Name IS NOT NULL;
Example:
“=”
SELECT Rev_Name, Rev_Age
FROM Reviewer_Table
WHERE Rev_ID = 20;
“<”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_ID<15;
Rev_ID Rev_Name
12 Chris
13 Amir
14 Raj
“>”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_ID>19;
Rev_ID Rev_Name
20 Caroline
21 Bonnie
“<=”
Select Rev_Name,Rev_ID
FROM Reviewer_Table
Where Rev_Age<=20;
“>=”
Select Rev_Name,Rev_Age
FROM Reviewer_Table
Where Rev_ID>=15;
“<>”
Select *
FROM Reviewer_Table
Where Rev_age<25;
Rev_ID Rev_Name Rev_Age
12 Chris 20
13 Amir 30
15 Soha 26
16 Rabia 22
18 Usman 35
19 Robert 32
20 Caroline 18
21 Bonnie 16