Database Management Systems Syllabus
Database Management Systems Syllabus
II
SYLLABUS
Structure of Relational Databases - Integrity Constraints, Synthesizing
ER diagram to relational schema
► Domain Constraints
► Entity integrity constraints
► Key Constraints
► Referential Integrity Constraints
Domain Constraints
► Each attribute in a tuple is declared to be of a particular domain (for
example, integer, character, Boolean, String, etc.) which specifies a
constraint on the values that an attribute can take.
Entity integrity constraints
► The entity integrity constraint states that primary key value can't be
null.
► This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
► A table can contain a null value other than the primary key field.
Key constraints
► Keys are used to uniquely identify an entity within its entity set.
► An entity set can have multiple keys, but out of which one key will be
the primary key.
► A primary key can contain a unique value in the relational table.
Referential Integrity Constraints
► A referential integrity constraint is specified between two tables.
► In the Referential integrity constraints, if a foreign key in Table 1 refers to the
Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be
null or be available in Table 2.
Synthesizing ER diagram to relational schema
Entities and Simple Attributes
► An entity type within ER diagram is turned into a table.
► Each attribute turns into a column in the table.
► The key attribute of the entity is the primary key of the table which is
usually underlined.
► It is highly recommended that every table should start with its
primary key attribute conventionally named as TablenameID.
Entities and Simple Attributes
Primary key
Foreign key
► If you have a multi-valued attribute, take the attribute and turn it into a new entity
or table of its own.
► Then make a 1:N relationship between the new entity and the existing one.
► Create a table for the attribute.
► Add the primary (id) column of the parent entity as a foreign key within the new table
► Relational Algebra is a procedural query language where the user tells the
system to carry out a set of operations to obtain the desired results.
► The user tells what data should be retrieved from the database and how to retrieve it.
► In relational algebra,
► input is a relation (table from which data has to be accessed) and
► output is also a relation (a temporary table holding the data asked for by the user).
Relational Algebra
► All we have to do is specify the table name from which we need the data,
and in a single line of command, relational algebra will traverse the entire
table to fetch data for you.
Relational Algebra
Project(∏)
► Project Operation is used to project or display or get the required column
data from the relation.
ς 𝐶1 𝐶2 𝐶𝑛 (𝑅)
, ,….
𝝅𝑵𝒂𝒎𝒆,𝑨𝒈𝒆 (𝑬𝒎𝒑𝒍𝒐𝒚𝒆𝒆)
Select(σ)
► Select operation selects the required tuples from the relation according
to the given conditions.
► It is denoted by symbol sigma(σ).
► The select operator only selects the tuples from the relation but to
display those tuples we have to use the project operator.
𝜎𝑝 (𝑅)
► where p denotes the logic according to which the selection is done and R
is the relation.
Select(σ) - Example
► We have an employee table with attributes as name, emlpoyee_id,
salary, age.
► We want the details of all those employee whose age is greater than 26.
The query for the above operation would be
𝜎𝑎𝑔𝑒>26 (𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒)
The above operation would select the tuple from the employee relation.
But to display the selected tuple we will have to perform the project operation.
As we need all the details of the employee so we don't provide any condition in the
project operation.
If we don't provide any column name in project operation then it would project all the
columns. So, the new query would be:
Select(σ)- Example
𝜋(𝜎𝑎𝑔𝑒>26 (𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒))
► Notation: R1 X R2
► where R1 and R2 are two relations such that the cross product will result in a table
which has all the attributes of the table R1 followed by the attributes of the R2.
Cross Product(X)- Example
► The set difference operation performs the same function as the set
difference operation in the set theory.
► It is represented by -(minus) symbol.
► If we perform an operation A-B on table A and B, then the result would
include only those tuples that are in table A but not in B.
► This operation removes all those tuples which are common in both table A and B,
from A and gives output.
► For performing a set difference operation both the tables should have
the same attributes.
► Notation: R1 - R2 where R1 and R2 are two tables.
Set Difference - Example
► Course_1 - Course_2
Rename(ρ)
► The results of the operations that we are performing are saved without
any name.
► If we want to give some name to the result of any operation then we
can rename the result of the operations using the rename operation.
► It can also be used to rename an old relation. It is denoted by symbol
rho (ρ).
► Notation 1: Naming the result of an operation ρh(E)
► where the result of expression E is stored as h.
► Notation 2: Renaming an already existing column ρNew_Name/Old_Name(E)
Rename
► Inner join is a type of join in which only those tuples are selected which
full fill the required conditions.
► All those tuples which do not satisfy the required conditions are
excluded. Let us now study in detail each type of inner join with
example.
Natural Join(⋈)
► Natural Join is a join which is
performed if there is a
common attribute between
the relations.
► Notation: R1 ⋈ R2 where R1
and R2 are two relations.
► Example: We have two tables of
Student(S_id,Name,Class,Age
,C_id) and Courses(C_id,
C_name). Now, we will perform
natutral join on both the tables
i.e
► Student ⋈ Course
Theta Join (θ)
► Theta join is a join which combines the tuples from different relations
according to the given theta condition.
► The join condition in theta join is denoted by theta (θ) symbol. This
join uses all kind of comparison operator.
Notation: R1⋈ θ R2
► where R1 and R2 are relations such that they don't have any common
attribute.
Theta Join (θ) - Example
► We have two tables of
Student(S_id,Name,Std,Age)
and Courses(Class,C_name).
► Student ⋈ [Link]=[Link]
Course
► Equi Join is a type of theta join where we use only the equality
operator, unlike theta join where we can use any operator.
► The previous example which we gave in the theta join is also an
example of equi-join.
Outer Join
► In Inner Join, the matched rows are returned and unmatched rows are
not returned.
► But, in outer join, we include those tuples which meet the given
condition along with that, we also add those tuples which do not meet
the required condition.
► The result also includes the tuples from the left and right tables which
do not satisfy the conditions.
► Based on the tuples that are added from left, right or both the tables,
the outer join is further divided into three types. We will now study
about its types with the help of examples.
► Note: The operators here define the existence of null value which we will use to fill
the table if the required conditions do not match.
Left Outer Join(⟕)
► Left Outer Join is a type of join in which all the tuples from left relation
are included and only those tuples from right relation are included
which have a common value in the common attribute on which the join
is being performed.
► Student ⟖ Course
Full Outer Join(⟗)
► Full Outer Join is a type of join in which all the tuples from the left and
right relation which are having the same value on the common
attribute. Also, they will have all the remaining tuples which are not
common on in both the relations.
► Notation: R1 ⟗ R2 where R1 and R2 are relations.
Full Outer Join(⟗) - Example
► We have two tables of and Courses (C_type,C_name). Now, we
Student(S_id,Name,Class,Age,C_type) will perform full outer join
on both the tables i.e
► Student ⟗ Course
JOIN
Intersect (∩)
► The intersect operation performs the same function as the intersect
operation in the set theory.
► It is represented by ∩ symbol.
► This operation selects all those tuples from the relation which are
common in both the relation.
► Notation: R1 ∩ R2 where R1 and R2 are two relations.
Intersect (∩)
► Example: We have two courses table and we want to perform the intersect operation
on table Course_1(C_id, C_name) and Course_2(C_id, C_name). Remember,
we can perform intesect operation only because both the tables have same attributes.
► Course_1 ∩ Course_2
Example Queries -1
► Assume the following relations:
BOOKS(DocId, Title, Publisher, Year)
STUDENTS(StId, StName, Major, Age)
AUTHORS(AName, Address)
borrows(DocId, StId, Date)
has-written(DocId, AName)
describes(DocId, Keyword)
1. List the year and title of each book.
2. List all information about students whose major is CS
3. List all students with the books they can borrow
4. List all books published by McGraw-Hill before 1990
5. List the name of those authors who are living in Davis
6. List the name of students who are older than 30 and who are not studying CS
7. Rename AName in the relation AUTHORS to Name.
Example Queries -1
► List the year and title of each book.
► πYear, Title(BOOKS)
Example Queries -1
► List the year and title of each book.
► πYear, Title(BOOKS)
► List all information about students whose major is CS
► π (σMajor = ’CS’(STUDENTS))
Example Queries -1
► List the year and title of each book.
► πYear, Title(BOOKS)
► List all information about students whose major is CS
► π (σMajor = ’CS’(STUDENTS))
► List all students with the books they can borrow
► π (STUDENTS × BOOKS)
Example Queries -1
► List the year and title of each book.
► πYear, Title(BOOKS)
► List all information about students whose major is CS
► π (σMajor = ’CS’(STUDENTS))
► List all students with the books they can borrow
► π (STUDENTS × BOOKS)
► List all books published by McGraw-Hill before 1990
► π (σPublisher = ’McGraw-Hill’ ∧ Year<1990(BOOKS))
Example Queries -1
► List the year and title of each book.
► πYear, Title(BOOKS)
► List all information about students whose major is CS
► π (σMajor = ’CS’(STUDENTS))
► List all students with the books they can borrow
► π (STUDENTS × BOOKS)
► List all books published by McGraw-Hill before 1990
► π (σPublisher = ’McGraw-Hill’ ∧ Year<1990(BOOKS))
► List the name of those authors who are living in Davis
► πAName(σAddress like ’%Davis%’(AUTHORS))
Example Queries - 1
► List the name of students who are older than 30 and who are not
studying CS
► πStName(σAge>30(STUDENTS)) − πStName(σMajor=’CS’(STUDENTS))
Example Queries - 1
► List the name of students who are older than 30 and who are not
studying CS
► πStName(σAge>30(STUDENTS)) − πStName(σMajor=’CS’(STUDENTS))
► Rename AName in the relation AUTHORS to Name
► ρAName/Name(AUTHORS)
Example Queries - 2
► Assume the following relations:
BOOKS(DocId, Title, Publisher, Year)
STUDENTS(StId, StName, Major, Age)
AUTHORS(AName, Address)
borrows(DocId, StId, Date)
has-written(DocId, AName)
describes(DocId, Keyword)
1. List each book with its keywords.
2. List each student with the books she/he has borrowed.
3. List the title of books written by the author ’Ullman’
4. List the authors of the books the student ’Smith’ has borrowed
5. Which books have both keywords ’database’ and ’programming’?
Example Queries -2
Q: List each book with its keywords.
BOOKS ⋈ describes
Note that books having no keyword will not be there in the result.
Q: List each student with the books she/he has borrowed.
BOOKS ⋈ (borrows ⋈ STUDENTS)
Q: List the title of books written by the author ’Ullman’
πTitle(σAName=’Ullman’(BOOKS ⋈ has-written))
Or
πTitle(BOOKS ⋈ σAName=’Ullman’(has-written))
Q: List the authors of the books the student ’Smith’ has borrowed
πAName(σStName=’Smith’(has-written ⋈ (borrows ⋈ STUDENTS))
Example Queries -2
Q: Which books have both keywords ’database’ and ’programming’?
BOOKS ⋈ (πDocId(σKeyword=’database’(describes)) ∩
πDocId(σKeyword=’programming’(describes)))
Example Queries - 3
► Assume the following relations:
SALESPERSON(Ssn, Name, StartYear, DeptNo)
TRIP(Ssn, FromCity, ToCity, DepartureDate, ReturnDate, TripId)
EXPENSE(TripId, AccountNo, Amount)
► Write relation algebra expression to get the details of salespersons who hav
travelled between Mumbai and Delhi and the travel expense is greater that
Rs. 50000.
► πSsn,Name,StartYear,DeptNo(σ(FromCity=‘Mumbai’ and ToCity=‘Delhi’) or (FromCity=‘Delhi’ and
ToCity=‘Mumbai’) and Amount>5000(SALESPERSON ⋈ (TRIP ⋈ EXPENSE))))
Example Queries - 4
► Assume the following relations:
SALESPERSON(Ssn, Name, StartYear, DeptNo)
TRIP(Ssn, FromCity, ToCity, DepartureDate, ReturnDate, TripId)
EXPENSE(TripId, AccountNo, Amount)
Specify the subsequent queries in relational algebra:
1. Give the details (all attributes of TRIP) for trips which exceeded $2000 in expenses.
2. Print the SSN of salesman who took trips to ‘Honolulu'
3. Print the trip expenses incurred through the salesman with SSN= ‘234-56-7890'.
Example Queries - 4
► Assume the following relations:
SALESPERSON(Ssn, Name, StartYear, DeptNo)
TRIP(Ssn, FromCity, ToCity, DepartureDate, ReturnDate, TripId)
EXPENSE(TripId, AccountNo, Amount)
Specify the subsequent queries in relational algebra:
1. Give the details (all attributes of TRIP) for trips which exceeded $2000 in expenses.
A: πSsn, FromCity, ToCity, DepartureDate, ReturnDate, TripId (σAmount>2000(TRIP ⋈ EXPENSE))
2. Print the SSN of salesman who took trips to ‘Honolulu‘
A: π Ssn(σToCity=‘Honolulu‘ (TRIP))
3. Print the trip expenses incurred through the salesman with SSN= ‘234-56-7890'.
A: πAmount(σSsn=‘234-56-7890'(TRIP ⋈ EXPENSE))
References
► [Link]
► [Link]
algebra-operators-in-dbms-1ff2ddecaad7
► [Link]
algebra/
► [Link]
► [Link]
Introduction to Structured Query Language (SQL)
History
► Structured Query Language(SQL) was developed at IBM
by Donald D. Chamberlin and Raymond F. Boyce in the
early 1970s.
► This was initially called SEQUEL(Structured English
QUEry Language).
► The main objective of SQL is to update, store,
manipulate and retrieve data stored in a relational
database.
TRUNCATE user
SQL | ALTER (ADD, DROP, MODIFY)
► ALTER TABLE is used to add, delete/drop or modify columns in the
existing table.
► It is also used to add and drop various constraints on the existing
table.
► ALTER TABLE – ADD
► ADD is used to add columns into the existing table.
► Sometimes we may require to add additional information, in that case we do not
require to create the whole database again, ADD comes to our rescue.
► Syntax
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype);
SQL | ALTER (ADD, DROP, MODIFY)
ROLL_NO NAME
1 Ram
4 Tanu
1 Ram
2 Abhi
3 Rahul
4 Tanu
SQL | ALTER (ADD, DROP, MODIFY)
► ALTER TABLE – DROP
1 Ram
2 Abhi
3 Rahul
4 Tanu
1 Ram
2 Abhi
3 Rahul
4 Tanu
SQL | ALTER (ADD, DROP, MODIFY)
► ALTER TABLE-MODIFY
► It is used to modify the existing columns in a table. Multiple columns can also be
modified at once.
► Syntax may vary slightly in different databases.
► Syntax(Oracle,MySQL,MariaDB):
ALTER TABLE table_name
MODIFY column_name column_type;
► Syntax(SQL Server):
ALTER TABLE table_name
ALTER COLUMN column_name column_type;
► Example
ALTER TABLE Student MODIFY COURSE varchar(20);
SQL | ALTER (RENAME)
► Sometimes we may want to rename our table to give it a more
relevant name.
► For this purpose we can use ALTER TABLE to rename the name of
table.
► There are three basic constructs which allow database program and
user to enter data and information are:
► INSERT
► UPDATE
► DELETE
Data Manipulation Language
► DML stands for Data Manipulation Language.
► It is a language used for selecting, inserting, deleting and updating
data in a database.
► It is used to retrieve and manipulate data in a relational database.
► There are three basic constructs which allow database program and
user to enter data and information are:
► INSERT
► UPDATE
► DELETE
SQL | INSERT
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
SQL | UPDATE | Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
CustomerID CustomerName ContactName Address City PostalCode Country
Alfreds
1 Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
Futterkiste
Ana Trujillo
Avda. de la
2 Emparedados y Ana Trujillo México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno
3 Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds
5 Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp
SQL | UPDATE
► UPDATE Multiple Records
► It is the WHERE clause that determines how many records will be updated.
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
CustomerID CustomerName ContactName Address City PostalCode Country
Alfreds
1 Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany
Futterkiste
Ana Trujillo
Avda. de la
2 Emparedados y Juan México D.F. 05021 Mexico
Constitución 2222
helados
Antonio Moreno
3 Juan Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
Berglunds
5 Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
snabbköp
SQL | UPDATE
► If you omit the WHERE clause, ALL records will be updated!
UPDATE Customers
SET ContactName='Juan';