UNIT-II
Introduction to the Relational
Model
By
Dr Bagam Laxmaiah
Integrity Constraints
• Integrity constraints are a set of rules. It is used to
maintain the quality of information.
• Integrity constraints ensure that the data insertion,
updating, and other processes have to be performed in
such a way that data integrity is not affected.
• Integrity constraint is used to guard against accidental
damage to the database.
• (NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN
KEY, CHECK, DEFAULT)
Types of Integrity Constraint
1. Domain constraints
• Every domain must contain atomic values(smallest
indivisible units) which means composite and multi-
valued attributes are not allowed.
• We perform a datatype check here, which means when we
assign a data type to a column we limit the values that it
can contain. Eg. If we assign the datatype of attribute age
as int, we can’t give it values other than int datatype.
Example:
EID Name Phone
123456789
01 Bikash Dutta
234456678
• Explanation: In the above relation, Name is a composite attribute
and Phone is a multi-values attribute, so it is violating domain
constraint.
.
2. Key Constraints or Uniqueness
Constraints
• These are called uniqueness constraints since it ensures
that every tuple in the relation should be unique.
• A relation can have multiple keys or candidate
keys(minimal superkey), out of which we choose one
of the keys as the primary key, we don’t have any
restriction on choosing the primary key out of
candidate keys, but it is suggested to go with the
candidate key with less number of attributes.
• Null values are not allowed in the primary key, hence
Not Null constraint is also part of the key constraint.
Example:
EID Name Phone
01 Bikash 6000000009
02 Paul 9000090009
01 Tuhin 9234567892
• Explanation: In the above table, EID is the primary key, and the
first and the last tuple have the same value in EID ie 01, so it is
violating the key constraint.
3. Entity Integrity Constraints
• Entity Integrity constraints say that no primary key can take a NULL
value, since using the primary key we identify each tuple uniquely
in a relation.
EID Name Phone
01 Bikash 9000900099
02 Paul 600000009
NULL Sony 9234567892
• Explanation: In the above relation, EID is made the primary key,
and the primary key can’t take NULL values but in the third tuple,
the primary key is null, so it is violating Entity Integrity constraints.
.
4. Referential Integrity Constraints
• The Referential integrity constraint is specified
between two relations or tables and used to maintain
the consistency among the tuples in two relations.
• This constraint is enforced through a foreign key, when
an attribute in the foreign key of relation R1 has the
same domain(s) as the primary key of relation R2, then
the foreign key of R1 is said to reference or refer to the
primary key of relation R2.
• The values of the foreign key in a tuple of relation R1
can either take the values of the primary key for some
tuple in relation R2, or can take NULL values, but
can’t be empty.
Example:
DNO Place
12 Jaipur
13 Mumbai
14 Delhi
EID Name DNO
01 Divine 12
02 Dino 22
04 Vivian 14
.
Enforcing integrity constraints
Querying Relational Data
• A relational database query is a question about
the data, and the answer consists of a new
relation containing the result.
• For example, we might want to find
all students AGE less than 18 or all
students enrolled in particular
course.
Contd..
• The SELECT statement is used to fetch the
data from a database table which returns this
data in the form of a result table. These result
tables are called result-sets.
• Ex: SELECT column1, column2, ... FROM
table_name;
• SELECT * FROM table_name;
Example
• SELECT * FROM Students WHERE age < 18;
• SELECT ID,FirstName FROM Students WHERE
age < 18;
SQL Aliases
• Aliases are the temporary names given to
tables or columns. An alias is created with
the AS keyword.
• Column name:
• SELECT column_name AS alias_name FROM
table_name;
• Table Name:
• SELECT column_name(s) FROM table_name AS
alias_name;
Example
• SELECT studentID AS ID, FROM students AS S;
• Aliases can be useful when:
• There are more than one table involved in a query
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together.
SELECT data from Multiple
Tables
• We can also combine information from multiple tables.
• SELECT table1.column1, table2.column2 FROM table1,
table2 WHERE table1.column1 = table2.column1;
• Example:
• SELECT e.sname student1,
• 2 d.ssname student2
• 3 FROM student2 d, student1 e
• 4 WHERE D.ssid = E.sid;
LOGICAL DATABASE DESIGN
• Relational model can represent as a table with columns
and rows. Each row is known as a tuple. Each table of the
column has a name or attribute.
• Domain: It contains a set of atomic values that an
attribute can take.
• Attribute: It contains the name of a column in a
particular table. Each attribute Ai must have a domain,
dom(Ai)
• Relational instance: In the relational database system, the
.
relational instance is represented by a finite set of tuples.
Relation instances do not have duplicate tuples.
• Relational schema: A relational schema is a set of relational
tables and associated items that are related to one another.
• Relational key: In the relational key, each row has one or
more attributes. It can identify the row in the relation uniquely.
Entity sets to Tables
• An entity set mapped to a relation in a straight forward
way.
• Each attributes of the entity set becomes an attribute of
the table.
Ralationship sets (without constraints)
to tables
• A Relationship set like an entity set mappled to a relation in
the Relational model.
• To represent a relation, we must identify each participating
entity and give values to the descriptive attribute of the
relationship.
• Thus, the attribute of the relation includes:
• The primary key attributes of each participating entity
set as foreign key fields.
•
Translating relationship sets with key
constraints
• If a relationship set involves ‘n’ entity sets and some ‘m’
of them are linked via arrows in the ER-Diagram, the key
of any one of these ‘m’ entity sets constitutes a key for the
relation to which the relationship set is mapped.
• Hence, we have ‘m’ candidate keys, and one of these
should be designated as the primary key.
Translating relationship sets with
participation constraints
• Every department is required to have a one
manager, due to the participation constraint,
and at most one manager, due to the key
constraint.
Transalating Weak Entity
• A weak entity set always participates in a one-to-many
binary relationship and has a key constraint and total
participation.
Translating Heirarchies
• Class Heirarchies in an ER-diagram refers to the concept
of inheritance.
• Child entities inherits all the attributes of base entity in
addition to its own attributes.
Translating ER diagrams with
aggregation
• An aggregation represents a relation between two
relationship sets that is, one relationship set participates in
other relationship set. This participation is termed as
aggregation.
INTRODUCTION TO VIEWS
• Views in SQL are considered as a virtual table. A view
also contains rows and columns.
• To create the view, we can select the fields from one or
more tables present in the database.
• A view can either have specific rows based on certain
condition or all the rows of a table.
Advantages of View:
1. Complexity: Views help to reduce the complexity. Different views
can be created on the same base table for different users.
2. Security: It increases the security by excluding the sensitive
information from the view.
3. Query Simplicity: It helps to simplify commands from the user. A
view can draw data from several different tables and present it as a
single table.
4. Consistency: A view can present a consistent, unchanged image of
the structure of the database. Views can be used to rename the
columns without affecting the base table.
5. Data Integrity: If data is accessed and entered through a view, the
DBMS can automatically check the data to ensure that it meets the
specified integrity constraints.
6. Storage Capacity: Views take very little space to store the data.
7. Logical Data Independence: View can make the application and
database tables to a certain extent independent.
Disadvantages of View:
• The DML statements which can be performed on a view created
using single base table have certain restrictions are:
1. You cannot INSERT if the base table has any not null column that
do not appear in view.
2. You cannot INSERT or UPDATE if any of the column referenced in
the INSERT or UPDATE contains group functions or columns
defined by expression.
3. You can't execute INSERT, UPDATE, DELETE statements on a
view if with read only option is enabled.
4. You can't be created view on temporary tables.
5. You cannot INSERT, UPDATE, DELETE if the view contains group
functions GROUP BY, DISTINCT or a reference to a pseudo-
column rownum.
6. You can't pass parameters to the SQL server views.
7. You can't associate rules and defaults with views.
Student_Detail
STU_ID NAME ADDRESS
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
STU_ID NAME MARKS AGE
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
Creating view
• A view can be created using the CREATE
VIEW statement. We can create a view from a single
table or multiple tables.
• Syntax:
• CREATE VIEW view_name AS
• SELECT column1, column2.....
• FROM table_name
• WHERE condition;
Creating View from a single table
• In this example, we create a View named DetailsView
from the table Student_Detail.
• Query:
• CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM Student_Details
WHERE STU_ID < 4;
output
• SELECT * FROM DetailsView;
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Creating View from multiple tables
• View from multiple tables can be created by simply
include multiple tables in the SELECT statement.
• Query:
• CREATE VIEW MarksView AS
• SELECT Student_Detail.NAME,
Student_Detail.ADDRESS, Student_Marks.MARKS
• FROM Student_Detail, Student_Mark
• WHERE Student_Detail.NAME =
Student_Marks.NAME;
To display data of View MarksView:
NAME ADDRESS MARKS
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
Deleting View
• A view can be deleted using the Drop
View statement.
• Syntax
DROP VIEW view_name;
• Example:
• If we want to delete the
View MarksView, we can do this as:
DROP VIEW MarksView;
Types of Views:
Contd..
1. Join View: A join view is a view that has more than one
table or view in its from clause and it does not use any
Group by Clause, Rownum, Distinct and set operation.
2. Inline View: An inline view is a view that is created by
replacing a subquery in the from clause which defines the
data source that can be referenced in the main query. The
sub query must be given an alias for efficient working.
UPDATING VIEWS
1. There are certain conditions that need to be satisfied to update a
view. If any one of these conditions is not met, then we will not
be allowed to update the view.
2. The SELECT statement which is used to create the view should
not include GROUP BY clause or ORDER BY clause.
3. The SELECT statement should not have the DISTINCT keyword.
4. The View should have all NOT NULL values.
5. The view should not be created using nested queries or complex
queries.
6. The view should be created from a single table. If the view is
created using multiple tables then we will not be allowed to
update the view.
SQL ALTER TABLE
• The ALTER TABLE statement in Structured Query Language
allows you to add, modify, and delete columns of an existing
table. This statement also allows database users to add and
remove various SQL constraints on the existing tables.
• Syntax of ALTER TABLE ADD Column statement in SQL
• ALTER TABLE table_name ADD column_name column-
definition;
Ex:
• ALTER TABLE Cars ADD Car_Model Varchar(20);
• Table: Employee
o Suppose, you want to add two
columns, Emp_ContactNo. and Emp_EmailID, in the above
Employee table. For this, you have to type the following query
in the SQL:
• ALTER TABLE Employee ADD ( Emp_ContactNo. Number(
13), Emp_EmailID varchar(50) ;
ALTER TABLE MODIFY Column statement in SQL
• ALTER TABLE MODIFY Column statement in SQL
• The MODIFY keyword is used for changing the column
definition of the existing table.
• Syntax of ALTER TABLE MODIFY Column statement in
SQL
• ALTER TABLE table_name MODIFY column_name column-
definition;
• Table: Cars
o Suppose, you want to modify the datatype of
the Car_Color column of the above table. For this, you have
to type the following query in the SQL:
• ALTER TABLE Cars ADD Car_Color Varchar(50);
• ALTER TABLE DROP Column . statement in SQL
• In many situations, you may require to delete the columns
from the existing table. Instead of deleting the whole table or
database you can use DROP keyword for deleting the
columns.
• Syntax of ALTER TABLE DROP Column statement in
SQL
• ALTER TABLE table_name DROP Column column_name ;
• Table: Cars
o Suppose, you want to delete the Car_Color column from the
above table. For this, you have to type the following query in
the SQL:
• ALTER TABLE Cars DROP COLUMN Car_Color ;
Relational Query Language
• Types of Relational Query Language
• There are two types of relational query language:
• Procedural Query Language
• Non-Procedural Language
Procedural Query Language
• In Procedural Language, the user instructs the system to perform a series
of operations on the database to produce the desired results. Users tell
what data to be retrieved from the database and how to retrieve it.
Procedural Query Language performs a set of queries instructing the
DBMS to perform various transactions in sequence to meet user requests.
Non-Procedural Language
• In Non Procedural Language user outlines the desired information without
giving a specific procedure or without telling the step-by-step process for
attaining the information. It only gives a single Query on one or more
tables to get the data. The user tells what is to be retrieved from the
database but does not tell how to accomplish it.
Relational Algebra
• Relational algebra is a procedural query language. It gives
a step by step process to obtain the result of the query. It
uses operators to perform queries.
.
.
RELATIONAL CALCULUS
• There is an alternate way of formulating queries known as
Relational Calculus. Relational calculus is a non-
procedural query language.
• In the non-procedural query language, the user is
concerned with the details of how to obtain the end
results.
• The relational calculus tells what to do but never explains
how to do it.
Why it is called Relational Calculus?
• It is based on Predicate calculus, a name derived from
branch of symbolic language. A predicate is a truth-
valued function with arguments. On substituting values
for the arguments, the function result in an expression
called a proposition. It can be either true or false. It is a
tailored version of a subset of the Predicate Calculus to
communicate with the relational database.
Contd..
• Many of the calculus expressions involves the use of
Quantifiers. There are two types of quantifiers:
• Universal Quantifiers: The universal quantifier denoted
by ∀ is read as for all which means that in a given set of
tuples exactly all tuples satisfy a given condition.
• Existential Quantifiers: The existential quantifier
denoted by ∃ is read as for all which means that in a given
set of tuples there is at least one occurrences whose value
satisfy a given condition.
Types of Relational calculus
1. Tuple Relational Calculus (TRC)
• It is a non-procedural query language which is based on
finding a number of tuple variables also known as range
variable for which predicate holds true.
• It describes the desired information without giving a
specific procedure for obtaining that information.
• The tuple relational calculus is specified to select the
tuples in a relation. In TRC, filtering variable uses the
tuples of a relation. The result of the relation can have one
or more tuples.
Notation:
• A Query in the tuple relational calculus is expressed
as following notation
1.{T | P (T)} or {T | Condition (T)}
WHERE
• T is the resulting tuples
• P(T) is the condition used to fetch T.
• For example:
1.{ T.name | Author(T) AND T.article = 'database' }
• Output: This query selects the tuples from the
AUTHOR relation. It returns a tuple with 'name' from
Author who has written an article on 'database'.
• TRC (tuple relational calculus) can be quantified. In
TRC, we can use Existential (∃) and Universal
Quantifiers (∀).
For example:
1. { R| ∃T ∈ Authors(T.article='database' AND R.name)} .n
ame=T
• Output: This query will yield the same result as the
previous one.
2. Domain Relational Calculus (DRC)
• The second form of relation is known as Domain
relational calculus. In domain relational calculus, filtering
variable uses the domain of attributes.
• Domain relational calculus uses the same operators as
tuple calculus. It uses logical connectives ∧ (and), ∨ (or)
and ┓ (not).
• It uses Existential (∃) and Universal Quantifiers (∀) to
bind the variable.
Contd..
• Notation:
1.{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
• Where
• a1, a2 are attributes
P stands for formula built by inner attributes
• For example:
{< article, page, subject > | ∈ javatpoint ∧ su
bject = 'database’}
Output: This query will yield the article, page,
and subject from the relational javatpoint,
where the subject is a database.