DBMS Unit-4 Notes
DBMS Unit-4 Notes
Relational Algebra
Relational Algebra is a procedural language. In Relational
Algebra, order is specified in which operations have to
be performed. Basic operation in relational algebra are:
1. Select (σ)
2. Project (Π)
3. Union (U)
4. Set Difference (-)
5. Cartesian product (X)
Relational Calculus
Relational Calculus is the formal query language. It is
also known as Declarative language. In Relational
Calculus, order is not specified in which operation has to
be performed. Relational Calculus means what result we
have to obtain. Relational Calculus has two variations:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus (DRC)
Relational Calculus is denoted as:
{ t | P(t) } Where,
t: the set of tuples
p: is condition which is true for the given set of tuples.
Difference between Relational Algebra and Relational Calculus:
Basis of
S.NO Comparison Relational Algebra Relational Calculus
Relational Calculus is a
Language Type It is a Procedural language. Declarative (non-procedural)
1. language.
Completeness of a language is
measured in the manner that it is
The expressiveness of any
least as powerful as calculus.
given language is judged
Expressiveness That implies relation defined
using relational algebra
using some expression of
operations as a standard.
calculus is also definable by
10. some other expression
Functional Dependency
In relational database management, functional
dependency is a concept that specifies the relationship
between two sets of attributes where one attribute
determines the value of another attribute. It is denoted
as X → Y, where attribute set on the left side of the
arrow, X is called Determinant, Y is called Dependent.
Example:
roll_no name dept_name dept_building
42 abc CO A4
43 pqr IT A3
44 xyz CO A4
From above table we can conclude some valid
functional dependencies:
roll_no → { name, dept_name, dept_building },→
42 abc 17
43 pqr 18
44 xyz 18
42 abc CO 4
43 pqr EC 2
44 xyz IT 1
45 abc EC 2
resources.
o The chances of errors and inconsistencies increases.
in the database.
o Normalization is a process of decomposing the
Advantages of Normalization
o Normalization helps to minimize data redundancy.
14 John 7272826385, UP
9064738238
14 John 7272826385 UP
14 John 9064738238 UP
25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38
In the given table, non-prime attribute TEACHER_AGE is
dependent on TEACHER_ID which is a proper subset of a
candidate key. That's why it violates the rule for 2NF.
To convert the given table into 2NF, we decompose it
into two tables:
TEACHER_DETAIL table:
TEACHER_ID TEACHER_AGE
25 30
47 35
83 38
TEACHER_SUBJECT table:
TEACHER_ID SUBJECT
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
EMPLOYEE_ZIP table
EMP_ZIP EMP_STATE EMP_CITY
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Boyce Codd normal form (BCNF)
o BCNF is the advance version of 3NF. It is stricter than
3NF.
o A table is in BCNF if every functional dependency X
264 India
264 India
EMP_DEPT table:
EMP_DEPT DEPT_TYPE EMP_DEPT_NO
EMP_DEPT_MAPPING table:
EMP_ID EMP_DEPT
D394 283
D394 300
D283 232
D283 549
Functional dependencies:
1. EMP_ID → EMP_COUNTRY
2. EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
Candidate keys:
For the first table: EMP_ID
For the second table: EMP_DEPT
For the third table: {EMP_ID, EMP_DEPT}
Now, this is in BCNF because left side part of both the
functional dependencies is a key.
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
(PJ/NF).
Example
SUBJECT LECTURER SEMESTER
Semester 1 Computer
Semester 1 Math
Semester 1 Chemistry
Semester 2 Math
P2
SUBJECT LECTURER
Computer Anshika
Computer John
Math John
Math Akash
Chemistry Praveen
P3
SEMSTER LECTURER
Semester 1 Anshika
Semester 1 John
Semester 1 John
Semester 2 Akash
Semester 1 Praveen
SQL
SQL stands for Structured Query Language. SQL is used to
manipulate underlying relational databases that are
queried by SQL like Oracle, MySQL, SQLite, etc.
Components of SQL
1. Keywords:
Keywords are reserved or non-reserved words. SQL-
reserved keywords are INTO, UPDATE, SELECT, DELETE,
DROP, DESC, and ASC.
2. Identifiers:
The database objects, like function name, schema name,
table name, etc., are named Identifiers.
3.Clauses:
The components of queries and SQL statements such as
WHERE, GROUP BY, HAVING, and ORDER BY are formed
by clauses.
4. Expression:
Either columns or scalar values and rows of data in SQL
produced by EXPRESSION.
5. Boolean Conditions:
The boolean value TRUE or FALSE is the result of the
Conditions, also called expressions. the effect of
statements or queries limited by this condition.
5. Queries:
The data based on specific criteria is retrieved by the SQL
statements. Queries are Statements that start with the
SELECT clause because they retrieve data from the
underlying database.
6. Statements:
SQL statements may persistently affect schema and data
or control transactions, program flow, connections,
sessions, or diagnostics. SQL statements are such as
INSERT, UPDATE, DROP, and DELETE statements since
they modify the database structure or data.
CHAR(Size) It is used to specify a fixed length string that can contain numbers, letters, and special
characters. Its size can be 0 to 255 characters. Default is 1.
VARCHAR(Size) It is used to specify a variable length string that can contain numbers, letters, and
special characters. Its size can be from 0 to 65535 characters.
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size parameter specifies the
column length in the bytes. Default is 1.
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size parameter specifies the
maximum column length in bytes.
TEXT(Size) It holds a string that can contain a maximum length of 255 characters.
ENUM(val1, val2, It is used when a string object having only one value, chosen from a list of possible
val3,...) values. It contains 65535 values in an ENUM list. If you insert a value that is not in the
list, a blank value will be inserted.
SET( It is used to specify a string that can have 0 or more values, chosen from a list of
val1,val2,val3,....) possible values. You can list up to 64 values at one time in a SET list.
MySQL Numeric Data Types
BIT(Size) It is used for a bit-value type. The number of bits per value is specified in size. Its size can be
1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -2147483648 to 2147483647 and
unsigned range varies from 0 to 4294967295. The size parameter specifies the max display
width that is 255.
FLOAT(size, d) It is used to specify a floating point number. Its size parameter specifies the total number of
digits. The number of digits after the decimal point is specified by d parameter.
FLOAT(p) It is used to specify a floating point number. MySQL used p parameter to determine whether
to use FLOAT or DOUBLE. If p is between 0 to24, the data type becomes FLOAT (). If p is from
25 to 53, the data type becomes DOUBLE().
DOUBLE(size, It is a normal size floating point number. Its size parameter specifies the total number of
d) digits. The number of digits after the decimal is specified by d parameter.
DECIMAL(size, It is used to specify a fixed point number. Its size parameter specifies the total number of
d) digits. The number of digits after the decimal parameter is specified by d parameter. The
maximum value for the size is 65, and the default value is 10. The maximum value for d is 30,
and the default value is 0.
BOOL It is used to specify Boolean values true and false. Zero is considered as false, and nonzero
values are considered as true.
DATETIME(fsp) It is used to specify date and time combination. Its format is YYYY-MM-DD hh:mm:ss. Its
supported range is from '1000-01-01 00:00:00' to 9999-12-31 23:59:59'.
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is stored as the number of seconds since the
Unix epoch('1970-01-01 00:00:00' UTC). Its format is YYYY-MM-DD hh:mm:ss. Its supported
range is from '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss. Its supported range is from '-
838:59:59' to '838:59:59'
YEAR It is used to specify a year in four-digit format. Values allowed in four digit format from
1901 to 2155, and 0000.
This example inserts 101 in the first column, Akhil in the second column, Sharma in the
third column, 40000 in the fourth column, and Bangalore in the last column of the
table Employee_details.
2.UPDATE Statement
This SQL statement changes or modifies the stored data in the SQL database.
1. UPDATE table_name
2. SET column_name1 = new_value_1, column_name2 = new_value_2, ...., column_nameN = new_va
lue_N
3. [ WHERE CONDITION ];
1. UPDATE Employee_details
2. SET Salary = 100000
3. WHERE Emp_ID = 10;
This example changes the Salary of those employees of the Employee_details table
whose Emp_ID is 10 in the table.
3. DELETE Statement
This SQL statement deletes the stored data from the SQL database.
Syntax of DELETE Statement:
This example deletes the record of those employees from the Employee_details table
whose First_Name is Sumit in the table.
This example inserts 101 in the first column, Akhil in the second column, Sharma in the
third column, 40000 in the fourth column, and Bangalore in the last column of the
table Employee_details.
4.View in SQL
A view is a SQL statement stored in the database with a name linked to it. It can store all
table rows or only a few selected rows from the table. The user can create a view in SQL
using single or multiple tables. The users create a view so that the data stored in a
specific table can be represented as virtual tables. It also enables the administrator to
restrict access to the data so that the user can only view or edit exactly the particular
element of the table they want to without changing the rest.
Creating Views
If the user wants to create a view in the database, then the user can do so by
implementing CREATE VIEW statement. The user can use a single or multiple tables to
create views. Mainly the views are created by the database administrator.
3.Evaluation
With addition to the relational algebra translation, it is
required to annotate the translated relational algebra
expression with the instructions used for specifying and
evaluating each operation. Thus after translating the user
query, the system executes a query evaluation plan.
Query Evaluation Plan
o In order to fully evaluate a query, the system needs
to construct a query evaluation plan.
o The annotations in evaluation plan may refer to the
algorithms to be used for particular index or the
specific operations.
o Such relational algebra with annotations is known
as Evaluation Primitives. Evaluation primitives carry
instructions needed for evaluation of operation.
o Thus, a query evaluation plan defines a sequence of
primitive operations used for evaluating a query. The
query evaluation plan is also known as query
execution plan.
o A query execution engine is responsible for
generating the output of the given query. It takes
the query execution plan, executes it, and finally
makes the output for the user query.
Concurrency Control
Concurrency Control is the management procedure that
is required for controlling concurrent execution of the
operations that take place on a database.
But before knowing about concurrency control, we
should know about concurrent execution.
Concurrent Execution in DBMS
o In a multi-user system, multiple users can access and
Concurrency Control
Concurrency Control is required for controlling and
managing the concurrent execution of database
operations and thus avoiding the inconsistencies in the
database. Thus, for maintaining the concurrency of the
database, we have the concurrency control protocols.
1. Lock-Based Protocol
In this type of protocol, any transaction cannot read
or write data until it acquires an appropriate lock on
it. There are two types of lock:
a) Shared lock:
It is also known as a Read-only lock. In a shared
lock, data item can be only read by transaction.
It can be shared between the transactions
because when the transaction holds a lock, then
it can't update the data on the data item.
b) Exclusive lock:
In the exclusive lock, the data item can be both
reads as well as written by the transaction.
This lock is exclusive and in this lock, multiple
transactions do not modify the same data
simultaneously.
Advantages of TO protocol
o This protocol ensures serializability since the
precedence graph is as follows:
o This protocol ensures freedom from deadlock that
means no transaction ever waits.