Dbms Theory Notes Unit III
Dbms Theory Notes Unit III
DBMS
(Database Management System)
CLASS:- BCA III SEM SEC “All”
UNIT - III
Database Management System
Domains, Relations and Keys: domains, relations,
kind of relations, relational database, various
types of keys, candidate, primary, alternate and
foreign keys.
Relational Algebra & SQL: The structure,
relational algebra operations, idea of relational
calculus, basic structure and operations of SQL.
Domain
A table in DBMS is a set of rows and columns that contain data. Columns in
table have a unique name, often referred as attributes in DBMS.
A domain is a unique set of values that can be assigned to an attribute in a
database. For example, a domain of strings can accept only string values.
For example, a domain of month-of-year can accept January,
February….December as possible values, a domain of integers can accept
whole numbers that are negative, positive and zero.
Definition: Domain constraints are user defined data type and we can define
them like this:
Domain Constraint = data type + Constraints (NOT NULL / UNIQUE /
PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)
Domain
Syntax for domain constraint :
Domain Constraint = data type(integer / character/date / time / string / etc.)+Constraints
(NOT NULL / UNIQUE / PRIMARY KEY / FOREIGN KEY / CHECK / DEFAULT)
Types of Domain Constraint:-
2 DEFAULT Constraint :- Provides a default value for a column when none is specified.
3 UNIQUE Key :-Ensures that all the values in a column are different.
6 CHECK Constraint :-Ensures that all values in a column satisfy certain conditions.
7 INDEX Constraint :-Used to create and retrieve data from the database very quickly.
Key Constraints in DBMS
Key Constraints in DBMS
� The rules that are to be followed while entering data into columns of the database
table
� Constraints ensure that data entered by the user into columns must be within the
criteria specified by the condition.
There are 6 types of key constraints in DBMS
� NOT NULL: ensures that the specified column doesn’t contain a NULL value.
� UNIQUE : provides a unique/distinct values to specified columns.
� DEFAULT: provides a default value to a column if none is specified.
� CHECK :checks for the predefined conditions before inserting the data inside
the table.
� PRIMARY KEY: it uniquely identifies a row in a table.
� FOREIGN KEY: ensures referential integrity of the relationship
Key Constraints in DBMS
CREATE TABLE Orders
( OrderID int NOT NULL,
OrderNumber int NOT
NULL, PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID)
REFERENCES
Persons(PersonID) );
Key Constraints in DBMS
Example-
Consider the following Student schema-
Student ( roll , name , sex , age , address , class , section )
Examples of candidate keys since each set consists of minimal
attributes required to identify each student uniquely in the Student table-
(class , section , roll )
( name , address )
Different Types Of Keys in DBMS
3. Primary Key-
A primary key is a candidate key that the database designer selects while designing the
database.
OR
Candidate key that the database designer implements is called as a primary key.
StudI First
Roll No LastName Email
NOTES- D Name
1 11 Tom Price [email protected]
The value of primary key can never be NULL. 2 12 Nick Wright [email protected]
•The value of primary key must always be unique. 3 13 Dana Natan [email protected]
•The values of primary key can never be changed i.e. no updation is possible.
•The value of primary key must be assigned when inserting a record.
•A relation is allowed to have only one primary key.
Different Types Of Keys in DBMS
4. Alternate Key-
Candidate keys that are left unimplemented or unused after implementing the primary key
are called as alternate keys. OR Unimplemented candidate keys are called as alternate
keys. StudID Roll No First Name LastName Email
1 11 Tom Price [email protected]
8. Unique Key-
Unique key is a key with the following properties-
•It is unique for all the records of the table.
•Once assigned, its value can not be changed i.e. it is non-updatable.
•It may have a NULL value.
Example-
The best example of unique key is Adhaar Card Numbers.
• If it gets lost and another duplicate copy is issued, then the duplicate copy always has the
same number as before.
• Thus, it is non-updatable.
Different Types Of Keys in DBMS
9. Surrogate Key- Start
Fname Lastname End Time
Time
• It is unique for all the records of the table.
Anne Smith 09:00 18:00
• It is updatable.
Jack Francis 08:00 17:00
• It can not be NULL i.e. it must have some value.
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00
Example-
Mobile Number of students in a class where every student owns a mobile phone.
Secondary key is required for the indexing purpose for better and faster searching.
Primary Key
Properties of a Primary Key
CREATE TABLE STUDENTS • It doesn’t not allow
( stu_id int NOT NULL duplicates.
• A table can have only one
first_name VARCHAR(30) NOT NULL, primary key
last_name VARCHAR(25) NOT NULL, • Primary key is denoted
by underlining the attribute
dob DATE, name (column name).
• It uniquely identifies each
CONSTRAINT student_pk PRIMARY KEY (stu_id) record of the table
); • It doesn’t allow null values
to be inserted for the primary
key column.
• A primary key can consists
of more than one columns,
such primary key is known
as composite primary key.
FOREIGN KEY
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
Relational Algebra & SQL
Relational Algebra came in 1970 and was given by Edgar F. Codd (Father of
programmer/user has to mention two things, "What to Do" and "How to Do".
STUDENT Aman
Notation : R ∪ S
Anant
Where R is the first relation
Ashish
S is the second relation
We want all the names from Atul
STUDENT and EMPLOYEE relation. Baljeet
Harsh
Pranav
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE) Prateek
Relational Algebra & SQL
Basic Operations
Set Difference (-)
EMPLOYEE
EMPLOYEE_NO NAME AGE
Set Difference as its name indicates is the difference between two
E-1 Anant 20 relations (R-S).
E-2 Ashish 23 It is denoted by a "Hyphen"(-) and it returns all the tuples(rows)
E-3 Baljeet 25 which are in relation R but not in relation S.
E-4 Harsh 20 It is also a binary operator. NAME
E-5 Pranav 22
Aman
Notation : R – S
STUDENT Atul
Where R is the first relation
S is the second relation Prateek
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
Relational Algebra & SQL
Basic Operations Cartesian product (X)
EMPLOYEE Cartesian product is denoted by the "X" symbol. We have two relations
EMPLOYEE_NO NAME AGE R and S. Cartesian product will combine every tuple(row) from R
E-1 Anant 20
with all the tuples from S. EMPLO
E-2 Ashish 23
ROLL NAME AGE YEE_N NAME AGE
E-3 Baljeet 25 O
E-4 Harsh 20 Notation: R X S
1 Aman 20 E-1 Anant 20
E-5 Pranav 22 Where R is the first relation
1 Aman 20 E-2 Ashish 23
S is the second relation
1 Aman 20 E-3 Baljeet 25
STUDENT
1 Aman 20 E-4 Harsh 20
it is also a binary operator.
1 Aman 20 E-5 Pranav 22
Let's combine the two relations
2 Atul 18 E-1 Anant 20
STUDENT and EMPLOYEE.
2 Atul 18 E-2 Ashish 23
2 Atul 18 E-3 Baljeet 25
2 Atul 18 E-4 Harsh 20
Join Operations
Join Operation in DBMS are binary operations that allow us to combine two or more
relations. They are further classified into two types: Inner Join, and Outer Join.
DEPARTMENT
D_NO D_NAME E_NO MIN_EXPERIENCE
D-1 HR E-1 03
D-2 IT E-2 05
D-3 Marketing E-3 02
Relational Algebra & SQL
The Cartesian Product of the above two relations. It will be much easier to understand
Join Operations when we have the Cartesian Product.
MIN_EXPERIE
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO
NCE
E-1 Ram Delhi 04 D-1 HR E-1 03
E-1 Ram Delhi 04 D-2 IT E-2 05
E-1 Ram Delhi 04 D-3 Marketing E-3 02
E-2 Varun Chandigarh 09 D-1 HR E-1 03
E-2 Varun Chandigarh 09 D-2 IT E-2 05
E-2 Varun Chandigarh 09 D-3 Marketing E-3 02
E-3 Ravi Noida 03 D-1 HR E-1 03
E-3 Ravi Noida 03 D-2 IT E-2 05
E-3 Ravi Noida 03 D-3 Marketing E-3 02
E-4 Amit Bangalore 07 D-1 HR E-1 03
E-4 Amit Bangalore 07 D-2 IT E-2 05
E-4 Amit Bangalore 07 D-3 Marketing E-3 02
Relational Algebra & SQL
Inner Join
When we perform Inner Join, only those tuples returned that satisfy the certain
condition. It is also classified into three types: Theta Join, Equi Join and Natural Join.
Notation : R ⋈θ S
Where R is the first relation
S is the second relation
We want a relation where EXPERIENCE from EMPLOYEE >= MIN_EXPERIENCE
from DEPARTMENT.
Relational Algebra & SQL
EMPLOYEE⋈θ EMPLOYEE.EXPERIENCE>= DEPARTMENT.MIN_EXPERIENCE DEPARTMENT
MIN_EXPERIE
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME E_NO
NCE
Natural join operates on matching attributes where the values of the attributes in both relations
are the same and remove the duplicate ones. Preferably Natural Join is performed on
the foreign key.
Notation : R ⋈ S
Where R is the first relation
S is the second relation
We want to join EMPLOYEE and DEPARTMENT relation with E_NO as a common
attribute.
Notice, here E_NO has the same name in both the relations and also consists of the same
domain, i.e., in both relations E_NO is a string.
Relational Algebra & SQL
EMPLOYEE ⋈ DEPARTMENT
MIN_EXPERIEN
E_NO E_NAME CITY EXPERIENCE D_NO D_NAME
CE
E-1 Ram Delhi 04 D-1 HR 03
E-2 Varun Chandigarh 09 D-2 IT 05
E-3 Ravi Noida 03 D-3 Marketing 02
Outer Join
Inner Join which includes the tuple that satisfies the given condition, Outer Join
also includes some/all the tuples which don't satisfy the given condition. It is also of
three types: Left Outer Join, Right Outer Join, and Full Outer Join.
We have two relations R and S, then Below is the representation of Left, Right, and Full
Outer Joins.
Relational Algebra & SQL
Relational Algebra & SQL
Left Outer Join
Left Outer Join returns the matching tuples(tuples present in both relations) and the
tuples which are only present in Left Relation, Here R.
If the matching tuples are NULL, then attributes/columns of Right Relation, here S are
made NULL in the output relation.
EMPLOYEE ⟕EMPLOYEE.E_NO = DEPARTMENT.E_NO DEPARTMENT
Notation : R ∩ S
Where R is the first relation
S is the second relation NAME
We want the names which are present
in STUDENT as well as in EMPLOYEE relation, Baljeet
Relations we used in Basic Operations.
Harsh
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
Relational Algebra & SQL
Division (÷)
Division Operation is represented by "division"(÷ or /) operator and is used in queries that
involve keywords "every", "all", etc.
Notation : R(X,Y)/S(Y)
Here,
R is the first relation from which data is retrieved.
S is the second relation that will help to retrieve the data.
X and Y are the attributes/columns present in relation. We can have multiple attributes in
relation, but keep in mind that attributes of S must be a proper subset of attributes of R.
For each corresponding value of Y, the above notation will return us the value of X from
tuple<X,Y> which exists everywhere.
Relational Algebra & SQL
ENROLLED COURSE
STUDENT_ID COURSE_ID COURSE_ID
Student_1 DBMS DBMS
Student_2 DBMS OS
Student_1 OS
Student_3 OS
Now the query is to return the STUDENT_ID of students who are enrolled
in every course.
ENROLLED(STUDENT_ID, COURSE_ID)/COURSE(COURSE_ID)
STUDENT_ID
Output.
Student_1
Relational Calculus
What is Relational Calculus?
Before understanding Relational calculus in DBMS, we need to
understand Procedural Language and Declarative Language.
Procedural Language - Those Languages which clearly define how to get the
required results from the Database are called Procedural Language. Relational
algebra is a Procedural Language.
Declarative Language - Those Language that only cares about What to get
from the database without getting into how to get the results are called
Declarative Language.
Relational Calculus is a Declarative Language that uses Predicate Logic or
First-Order Logic to determine the results from Database.
Relational Calculus
Relational Calculus
Tuple Relational Calculus (TRC)
TRC in DBMS uses a tuple variable (t) that goes to each row of the table and
checks if the predicate is true or false for the given row. Depending on the given
predicate condition, it returns the row or part of the row.
The Tuple Relational Calculus expression Syntax
{t \| P(t)}
Where t is the tuple variable that runs over every Row
and P(t) is the predicate logic expression or condition.
Relational Calculus
Customer Table Customer_id Name Zip code
QUERY 1 Rohit 12345
Write a TRC query to get 2 Rahul 13245
3 Rohit 56789
all the data of customers
4 Amit 12345.
whose zip code is 12345.
of database.
Almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access) use SQL as the
» A represent attributes
» R represent relations
» P is a predicate
SQL Structure & Operations
The SELECT Clause
SELECT query is used to retrieve data from a table. It is the most used SQL
query.
We can retrieve complete table data, or partial by specifying conditions using