Upload
others
View
11
Download
0
Embed Size (px)
Citation preview
Mr. Satish Pise
Topic 2: Relational Data Model and Security and Integrity Specification
Database Model
Q. List various data models.
Q. List any two data model.
Relational Database Model,
Hierarchical Model,
Network Model,
E-R Model
Hierarchical Model
In this model each entity has only one parent but can have several children . At the top of hierarchy there is
only one entity which is called Root.
Mr. Satish Pise
Network Model
In the network model, entities are organised in a graph,in which some entities can be accessed through
sveral path
Mr. Satish Pise
Relational Model
In this model, data is organised in two-dimesional tables called relations. The tables or relation are related to
each other.
The most popular data model in DBMS is the Relational Model. It is more scientific a model
than others. This model is based on first-order predicate logic and defines a table as an n-ary
relation.
Mr. Satish Pise
The main highlights of this model are −
Data is stored in tables called relations.
Relations can be normalized.
In normalized relations, values saved are atomic values.
Each row in a relation contains a unique value.
Each column in a relation contains values from a same domain.
Q. Compare network and hierarchical model.
Network Model Hierarchical Model
Supports many to many relationships. Supports one to many relationships.
It is based on records and links. It is based on tree like structure with one root.
More popular than Hierarchical Less popular
Network model is upgraded version of the
hierarchical model so used in the networks.
The main application of hierarchical data model is
in the mainframe database system
Data storage is in the form of arbitrary graphs. Data storage is in the form of data tree i.e. parent
child relationship
Uses links to relate data Uses pointers to relate data
It uses client server architecture. It does not uses client server architecture.
Entity-Relationship Model
Entity-Relationship (ER) Model is based on the notion of real-world entities and relationships
among them. While formulating real-world scenario into the database model, the ER Model
creates entity set, relationship set, general attributes and constraints.
Mr. Satish Pise
ER Model is best used for the conceptual design of a database. ER Model is
based on −
Entities and their attributes.
Relationships among entities.
These concepts are explained below.
Entity − An entity in an ER Model is a real-world entity having properties
o called attributes. Every attribute is defined by its set of values called domain. For example,
in a school database, a student is considered as an entity. Student has various attributes like
name, age, class, etc.
Relationship − The logical association among entities is calledrelationship. Relationships are
mapped with entities in various ways. Mapping cardinalities define the number of association
between two entities.
o Mapping cardinalities −
one to one
one to many
many to one
many to many
Mr. Satish Pise
Q. What is domain and entity? Domain: All permissible values of attributes is called as a domain.
Entity:An entity is a thing or object in the real world with an independent existence. An entity
may be an object with a physical existence.
RDBMS Concepts
A Relational Database management System(RDBMS) is a database management system based on
relational model introduced by E.F Codd. In relational model, data is represented in terms of tuples(rows).
RDBMS is used to manage Relational database. Relational database is a collection of organized set of
tables from which data can be accessed easily. Relational Database is most commonly used database. It
consists of number of tables and each table has its own primary key.
What is Table ?
In Relational database, a table is a collection of data elements organised in terms of rows and
columns. A table is also considered as convenient representation of relations. But a table can
have duplicate tuples while a true relation cannot have duplicate tuples. Table is the most
simplest form of data storage. Below is an example of Employee table.
Student_ID S_Name AGE Year
101 Satish Pise 20 2016
102 Sandip Patil 20 2016
What is a Record ?
A single entry in a table is called a Record or Row. A Record in a table represents set of
related data. For example, the above Employee table has 4 records. Following is an example
of single record.
Mr. Satish Pise
What is Field ?
A table consists of several records(row), each record can be broken into several smaller entities
known asFields. The above Employee table consist of four fields, ID, Name, Age and Salary.
What is a Column ?
In Relational table, a column is a set of value of a particular type. The term Attribute is also used to
represent a column. For example, in Employee table, Name is a column that represent names of employee.
Database Keys
Keys are very important part of Relational database. They are used to establish and identify relation
between tables. They also ensure that each record within a table can be uniquely identified by combination
of one or more fields within a table.
Q. Define super key.
Definition: Super key of an entity set is a set of one or more attributes whose values uniquely
determine each entity.
Super Key : - Super Key is defined as a set of attributes within a table that uniquely identifies each record
within a table. Super Key is a superset of Candidate key.
Q. Explain candidate key and primary key.
Q. What is Primary Key? Give example.
Q. Define the term.
(i) Candidate key
(ii) Primary key
i) Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected. It is an attribute or set
of attribute that can act as a primary key for a table to uniquely identify each record in that table.
Mr. Satish Pise
ii) Primary Key
Primary key is a candidate key that is most appropriate to become main key of the table. It is a key that
uniquely identify each record in a table.
Primary key: Within a relation there is always one attribute which has values that are
unique in a relation and thus can be used to identify tuple of that relation. Such a unique
identifier is called the primary key.
E.g. In the Student(Rollno,name,class,address) relation Rollno is the primary key.
Composite Key
Key that consist of two or more attributes that uniquely identify an entity occurance is
called Composite key. But any attribute that makes up the Composite key is not a simple key in its own.
Mr. Satish Pise
Secondary or Alternative key
The candidate key which are not selected for primary key are known as secondary keys or
alternative keys
Non-key Attribute
Non-key attributes are attributes other than candidate key attributes in a table.
Non-prime Attribute
Non-prime Attributes are attributes other than Primary attribute.
Mr. Satish Pise
ER Diagram
Q. List and draw any four symbols used in ER Model.
Symbols and Notations
Mr. Satish Pise
Components of E-R Diagram
Q. Explain strong and weak entity set with example.
Q. Explain strong entity and weak entity set.
Strong Entity Set: An entity set that has sufficient attributes to form a primary key iscalled as
strong entity set.
Example: Cust_id is a primary key of Customer Entity so customer is strong entity set..
Weak Entity set: An entity set that does not have sufficient attribute to form a primary key is
called as Weak Entity Set.
Example: transaction is not having sufficient attribute to form a primary key so, transaction a
weak entity set
Mr. Satish Pise
Q. What is the difference between weak entity set and strong entity set?
Mr. Satish Pise
Attribute
Entities are represented by means of their properties, called attributes. All attributes have
values. For example, a student entity may have name, class, and age as attributes.
Types of Attributes
Simple attribute − Simple attributes are atomic values, which cannot be divided further. For
example, a student's phone number is an atomic value of 10 digits.
Composite attribute − Composite attributes are made of more than one simple attribute. For
example, a student's complete name may have first_name and last_name.
Derived attribute − Derived attributes are the attributes that do not exist in the physical database, but
their values are derived from other attributes present in the database. For example, average_salary in a
department should not be saved directly in the database, instead it can be derived. For another
example, age can be derived from data_of_birth.
Single-value attribute − Single-value attributes contain single value. For example −
Social_Security_Number.
Multi-value attribute − Multi-value attributes may contain more than one values. For example, a
person can have more than one phone number, email_address, etc.
Q. Define attribute and entity.
Entity: An entity is a thing or object in the real world with an independent existence. An entity may
be an object with a physical existence.
Attribute: Describing properties of an entity is called attributes. For example, a student entity may
have name, class, and age as attributes.
Q. Explain single value and multivalued attribute of E-R model.
i) Single Valued attribute: Attributes that can have single value at a particular instance of time
are called single valued.
Example: age of a person is a single-valued attribute.
Mr. Satish Pise
ii)Multi valued attributes: A multi-valued attribute can have more than one value at one time.
Example: phone_no of a person is a multi-valued attribute since a person can have more than
one phone_no.
These attribute types can come together in a way like −
simple single-valued attributes
simple multi-valued attributes
composite single-valued attributes
composite multi-valued attributes
An Attribute describes a property or characterstic of an entity. For example, Name, Age, Address etc can
be attributes of a Student. An attribute is represented using eclipse.
Mr. Satish Pise
Key Attribute
Key attribute represents the main characterstic of an Entity. It is used to represent Primary key.
Ellipse with underlying lines represent Key Attribute.
Composite Attribute
An attribute can also have their own attributes. These attributes are known as Composite attribute.
Multivalued Attribute
Mr. Satish Pise
Q. Draw E-R diagram for customer and loan. Assume suitable attribute.
Q. Draw E-R diagram for airline reservation system. Here a passenger can
book ticket from personal for a flight on same date.
Mr. Satish Pise
Q. Draw an E-R diagram of hospital management system.
Q. Draw an ER diagram for Library Management System
Mr. Satish Pise
Relationship
A Relationship describes relations between entities. Relationship is represented using diamonds.
There are three types of relationship that exist between Entities.
Binary Relationship
Recursive Relationship
Ternary Relationship
Mr. Satish Pise
Binary Relationship
Binary Relationship means relation between two Entities. This is further divided into three types.
1. One to One : This type of relationship is rarely seen in real world.
The above example describes that one student can enroll only for one course and a course will also have
only one Student. This is not what you will usually see in relationship.
2. One to Many : It reflects business rule that one entity is associated with many number of same entity.
The example for this relation might sound a little weird, but this menas that one student can enroll to
many courses, but one course will have one Student.
The arrows in the diagram describes that one student can enroll for only one course.
3. Many to One : It reflects business rule that many entities can be associated with just one entity. For example,
Student enrolls for only one Course but a Course can have many Students.
Mr. Satish Pise
4. Many to Many :
The above diagram represents that many students can enroll for more than one courses.
Relationship
The association among entities is called a relationship. For example, an employee works_at a
department, a student enrolls in a course. Here, Works_at and Enrolls are called relationships.
Relationship Set
A set of relationships of similar type is called a relationship set. Like entities, a relationship too
can have attributes. These attributes are called descriptive attributes.
Degree of Relationship
The number of participating entities in a relationship defines the degree of the relationship.
Binary = degree 2
Ternary = degree 3
n-ary = degree
Mapping Cardinalities
Cardinality defines the number of entities in one entity set, which can be associated with the
number of entities of other set via relationship set.
One-to-one − One entity from entity set A can be associated with at most one entity of entity set B
and vice versa.
Mr. Satish Pise
One-to-many − One entity from entity set A can be associated with more than one entities of entity
set B however an entity from entity set B, can be associated with at most one entity.
Mr. Satish Pise
Many-to-one − More than one entities from entity set A can be associated with at most one entity of
entity set B, however an entity from entity set B can be associated with more than one entity from
entity set A.
Many-to-many − One entity from A can be associated with more than one entity from B and vice
versa.
Mr. Satish Pise
Enhanced Entity Relationship Diagram
Q. Describe enhanced E-K model with the help of example.
The enhanced entity-relationship model is a high level or conceptual data model
incorporating extensions to the original Entity – Relationship model (E-R) model,
used in design of database
EER model includes all modeling concepts of the ER model. In addition, EER includes: Subclasses and
superclasses, specialization and generalization.
Generalization
Generalization is a bottom-up approach in which two lower level entities combine to form a
higher level entity. In generalization, the higher level entity can also combine with other lower
level entity to make further higher level entity.
Specialization
Specialization is opposite to Generalization. It is a top-down approach in which one higher level
entity can be broken down into two lower level entity. In specialization, some higher level
entities may not have lower-level entity sets at all.
What is an Enhanced ERD?
An enhanced entity-relationship model, also known as an extended entity-relationship model, is a
type of database diagram that's similar to regular ERDs. Enhanced ERDs are high-level conceptual
models that accurately represent the requirements of complex databases.
Enhanced ERDs include the same concepts that ordinary ER diagrams encompass. In addition,
EERDs include:
Subtypes and supertypes (sometimes known as subclasses and superclasses)
Specialization or generalization
Mr. Satish Pise
Category or union type
Attribute and relationship inheritance
Enhanced ERD Definitions and Examples
The modeling concepts of EERDs differ somewhat from those of ERDs. See the list below for definitions
of concepts that are unique to enhanced entity-relationship diagrams. Before you dive in, be sure to review
our ERD pages, including this comprehensive look atER diagram symbols and meanings. When you fully
understand ERD structure, you're ready to acquaint yourself with enhanced entity-relationship diagrams.
SUPERTYPES & SUBTYPES
Supertype - an entity type that has a relationship with one or more subtypes.
Subtype - a subgroup of entities with unique attributes.
Inheritance - the idea that subtype entities inherit the values of all supertype attributes. Remember
than a subtype instance is also classified as a supertype instance.
GENERALIZATION & SPECIALIZATION
Generalization - the process of defining a general entity type from a collection of specialized
entity types.
Specialization - the inverse of generalization, since it defines subtypes of the supertype and forms
relationships between supertype and subtupe.
Inheritance - the idea that subtype entities inherit the values of all supertype attributes. Remember
than a subtype instance is also classified as a supertype instance.
Mr. Satish Pise
Q. Explain the term specialization and generalization with suitable example. Generalization
Generalization is a bottom-up approach in which two lower level entities combine to form a
higher level entity. In generalization, the higher level entity can also combine with other lower
level entity to make further higher level entity.
Specialization
Specialization is opposite to Generalization. It is a top-down approach in which one higher level
entity can be broken down into two lower level entity. In specialization, some higher level
entities may not have lower-level entity sets at all.
Mr. Satish Pise
Aggregation
Aggregration is a process when relation between two entity is treated as a single entity. Here
the relation between Center and Course, is acting as an Entity in relation with Visitor.
Relational Algebra
Relational algebra is a procedural query language, which takes instances of relations as input
and yields instances of relations as output. It uses operators to perform queries. An operator can
be either unary or binary. They accept relations as their input and yield relations as their
output. Relational algebra is performed recursively on a relation and intermediate results are
also considered relations.
The fundamental operations of relational algebra are as follows −
1. Selection(σ)
2. Projection (π)
3. Cartesian Product (Χ)
4. Natural Join( ⋈)
5. Union(U)
6. Set Difference ( – )
7. Rename (ρ)
Mr. Satish Pise
Q List the notation used in relational algebra.
Q. List different relational algebraic operators any four.
1. Selection(σ)
2. Projection (π)
3. Cartesian Product (Χ)
4. Natural Join( ⋈)
5. Union(U)
6. Set Difference ( – )
7. Rename (ρ)
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation. Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is prepositional logic formula
which may use connectors like and, or, and not. These terms may use relational operators like
− =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
σsubject = "database" and price = "450" or year > "2010"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450 or those books
published after 2010.
Mr. Satish Pise
Project Operation (∏)
It projects column(s) that satisfy a given predicate.
Notation − ∏A1, A2, An (r)
Where A1, A2 , An are attribute names of relation r.
Duplicate rows are automatically eliminated, as relation is a set.
For example −
∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Union Operation (∪)
It performs binary union between two given relations and is defined as −
r ∪ s = { t | t ∈ r or t ∈ s}
Notion − r U s
Where r and s are either database relations or relation result set (temporary relation).
For a union operation to be valid, the following conditions must hold −
r, and s must have the same number of attributes.
Attribute domains must be compatible.
Duplicate tuples are automatically eliminated.
Mr. Satish Pise
∏ author (Books) ∪ ∏ author (Articles)
Output − Projects the names of the authors who have either written a book or an article or both.
Set Difference (−)
The result of set difference operation is tuples, which are present in one relation but are not in
the second relation.
Notation − r − s
Finds all the tuples that are present in r but not in s.
∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
Cartesian Product (Χ)
Combines information of two different relations into one.
Notation − r Χ s
Where r and s are relations and their output will be defined as −
r Χ s = { q t | q ∈ r and t ∈ s}
σauthor = 'tutorialspoint'(Books Χ Articles)
Output − Yields a relation, which shows all the books and articles written by tutorialspoint.
Rename Operation (ρ)
The results of relational algebra are also relations but without any name. The rename operation
allows us to rename the output relation. 'rename' operation is denoted with small Greek letter
rho ρ.
Mr. Satish Pise
Notation − ρ x (E)
Where the result of expression E is saved with name of x.
Additional operations are −
Set intersection
Assignment
Natural join
Q. Consider the following Relational algebra schema
STUDENT(RNO, Name, DOB, Percentage, DNO) DEPARTMENT (DNO,
DNAME, HEAD) Write relational algebra expressions:
i) Find students name and course from Computer Dept.
ii) Get the students name who has percentage greater than 70
1. Π Name, DNAME(σ dname= Computer(STUDENT⋈DEPARTMENT))
2. ΠName(σPercentage>70(STUDENT))
Q. Consider the structure as
Product_Master = {prod_id, prod_name, rate} Purchase_details = {prod_id,
quantity, dept_no,purchase_date} Write a relational algebra expression for
the following:
i) Get product_id, product_name and quantity for all purchased product
ii) Get the products with rates between 100 and 4500.
i) Π prod_id, prod_name, quantity (σ Product_Master.prod_id=Purchase_details.prod_id
(Product_Master Purchase_details))
OR
Π prod_id, prod_name, quantity (Product_Master Purchase_details)
ii) Π prod_name,rates (σ rates>100 ^ rates < 4500 (Product_Master ))
Mr. Satish Pise
Q. Consider student schema(studid, studname, studaddr, studcity, studper)
Write relational algebra expression of the following:
i) Find the name of the student those who scored first class.
ii) Find studid, studaddr from the student database.
i) Find the name of the student those who scored first class.
П studname (σstudper>=60 (student))
OR
П studname ( studper= “FIRST”(student))
ii) Find studid, studaddr from the student database.
П studid, studaddr (student)
Q. Given:
driver (driver_id, driver_name, age, rating)
bus (bus_id, bus_name, color)
reserves (driver_id, bus_id, date);
Write Relational Algebra Queries for
i) Find the colors of bus reserved by „John Mark‟.
ii) Find the names of drivers who have not reserved a yellow bus.
Mr. Satish Pise
Q. Explain the set operator with help of example.
Set operators combine the results of two component queries into a single result. Queries
containing set operators are called as compound queries. Set operators in SQL are represented
with following special keywords as: Union, Union all, intersection & minus.
Consider data from two tables emp1 and emp2 as
emp1 emp2
ename ename
abc pqr
xyz xyz
lmn
1) Union : The Union of 2 or more sets contains all elements, which are present in either or both.
Union works as or.
Eg select ename from emp1 union select ename from emp2;
The output considering above data is :
Ename
abc
xyz
lmn
pqr
2) Union all : The Union of 2 or more sets contains all elements, which are present in both,
including duplicates.
Eg select ename from emp1 union all select ename from emp2;
The output considering above data is :
Ename
abc
xyz
lmn
pqr
xyz
Mr. Satish Pise
3) Intersect: The intersection of 2 sets includes elements which are present in both.
Eg select ename from emp1 intersect select ename from emp2;
The output considering above data is:
Ename
Xyz
4) Minus: The minus of 2 sets includes elements from set1 minus elements of set2.
Eg select ename from emp1 minus select ename from emp2;
The output considering above data is:
Ename
abc
lmn
Question 1 :
Consider the following relational Schema and give the answers of following :
Dealer(Dealer_no, Dealer_name, Address)
Part(Part_no, Part_Name, Color)
Assigned_To(Dealer_no, Part_no, cost)
a) Find names of dealers that supply red parts.
Answer
b) Find names of dealers that supply whole red parts.
Answer
c) Find names of dealers that supply both red and yellow parts.
Answer
Mr. Satish Pise
d) Find names of dealers who supply all parts.
Answer
Question 2 :
Consider the following relational Schema and give the answers of following :
Employee(E_name, Street, City)
Works_For(E_name, Company_Name, Salary)
a) Name of employees who are not working.
Answer
b) Find city and name of employee who are working.
Answer
c) Name of employees who are working.
Answer
d) Find city and name of employees who are not working.
Answer
e) Name of employees who are not working for Wipro.
Answer
f) Name of employees who are working for Wipro.
Answer
Mr. Satish Pise
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural query language, that
is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Q. Explain tuple relational calculus with example.
Q. Explain tuple relational calculus with example.
Tuple relational calculus:-It is a non-procedural calculus. It describes information
without giving a specific procedure for obtaining that information. A query in tuple
calculus is expressed as
{t | p(t)}
i.e. the set of all tuples (t) such that predicate (P[condition]) is true for „t‟. We use t[a] to
denote the value of tuple on attribute „a‟ & we use „tϵ R‟ to denote that tuple„t‟ is in
relation „R‟. There are different symbols with specific meaning which can be used to
write tuple calculus expression;-
1.Î belong to
2.∃ There exits
3.∀ for all
4.¬ not
5.=> implies
6.^ and
7.Ú or v
Mr. Satish Pise
Example.
Find records of employees where salary is more than 20000.
{t| t Î employee(t[salary]>20000)}
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{ T.name | Author(T) AND T.article = 'database' }
Output − Returns tuples with 'name' from Author who has written article on 'database'.
TRC can be quantified. We can use Existential (∃) and Universal Quantifiers (∀).
For example −
{ R| ∃T ∈ Authors(T.article='database' AND R.name=T.name)}
Output − The above query will yield the same result as the previous one.
Q1: Retrieve the name and address of all employees who work for 'X' department.
Answer
{t.FNAME, t.LNAME, t.ADDRESS | EMPLOYEE(t) and ((E d) (DEPARTMENT(d) and d.DNAME =
'X' and d.DNUMBER = t.DNO)) }
Note: The only FREE tuple variables should be those appearing to the left of the bar |
Mr. Satish Pise
Q2: For every project located in 'Y', retrieve the project number, the controlling department
number, and the last name, birthdate, and address of the manager of the department.
Answer
{p.PNUMBER, p.DNUM, m.LNAME, m.BDATE, m.ADDRESS | PROJECT(p) and EMPLOYEE(m)
and p.PLOCATION = 'Y' and ((E d) (DEPARTMENT(d) and p.DNUM = d.DNUMBER and d.MGRSSN
= m.SSN)) }
Q3: Retrieve the employee's first and last name and the first and last name of his or her immediate
supervisor.
Answer
{e.FNAME, e.LNAME, s.FNAME, s.LNAME | EMPLOYEE(e) and EMPLOYEE(s) and e.SUPERSSN
= S.SSN }
Q4: Make a list of all projects that involve an employee whose last name is 'Smith' as a worker or
as manager of the controlling department of the project.
Answer
{p.PNUMBER | PROJECT(p) and ((E e)(E w)(EMPLOYEE(e) and WORKS_ON(w) and
w.PNO=p.PNUMBER and e.LNAME='Smith' and e.SSN = w.ESSN))
OR
((E m)(E d)(EMPLOYEE(m) and DEPARTMENT(d) and p.DNUM=d.DNUMBER and
d.MGRSSN=m.SSN and m.LNAME='Smith')) }
Domain Relational Calculus (DRC)
In DRC, the filtering variable uses the domain of attributes instead of entire tuple values (as
done in TRC, mentioned above).
Notation −
Mr. Satish Pise
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where a1, a2 are attributes and P stands for formulae built by inner attributes.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint, where subject is
database.
Just like TRC, DRC can also be written using existential and universal quantifiers. DRC also
involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation Calculus is equivalent
to Relational Algebra.
{t | P (t)} or {t | condition (t)}
this is also known as expression of relational calculus
Where t is the resulting tuples, P(t) is the condition used to fetch t.
{t | EMPLOYEE (t) and t.SALARY>10000}
implies that it selects the tuples from EMPLOYEE relation such that resulting employee tuples will have
salary greater than 10000. It is example of selecting a range of values.
{t | EMPLOYEE (t) AND t.DEPT_ID = 10}
this select all the tuples of employee name who work for Department 10.
For example, select EMP_ID and EMP_NAME of employees who work for department 10
{<EMP_ID, EMP_NAME> | <EMP_ID, EMP_NAME> ? EMPLOYEE Λ DEPT_ID = 10}
Mr. Satish Pise
Get name of the department name that Alex works for.
{DEPT_NAME |< DEPT_NAME > ? DEPT Λ ? DEPT_ID (<DEPT_ID> ? EMPLOYEE Λ
EMP_NAME = Alex)}
Here green color expression is evaluated to get the department Id of Alex and then it is used to get the
department name form DEPT relation.
Let us consider another example where select EMP_ID, EMP_NAME and ADDRESS the employees from the
department where Alex works. What will be done here?
{<EMP_ID, EMP_NAME, ADDRESS, DEPT_ID > | <EMP_ID, EMP_NAME, ADDRESS,
DEPT_ID> ? EMPLOYEE Λ ? DEPT_ID (<DEPT_ID> ? EMPLOYEE Λ EMP_NAME = Alex)}
First, formula is evaluated to get the department ID of Alex (green color), and then all the employees with that
department is searched (red color).
Other concepts of TRC like free variable, bound variable, WFF etc remains same in DRC too. Its only
difference is DRC is based on attributes of relation.
Mr. Satish Pise
Mr. Satish Pise
What is RDBMS?
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL,
and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and
Microsoft Access.
A Relational database management system (RDBMS) is a database management system
(DBMS) that is based on the relational model as introduced by E. F. Codd.
What is table?
The data in RDBMS is stored in database objects called tables. The table is a collection of
related data entries and it consists of columns and rows.
Remember, a table is the most common and simplest form of data storage in a relational
database. Following is the example of a CUSTOMERS table:
+----
+
---------- +-----
+
+----------
+
-----------
| ID | NAME | AGE | ADDRESS | SALARY |
+----
+
---------- +-----
+
+----------
+
-----------
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----
+
---------- +-----
+
+----------
+
-----------
Mr. Satish Pise
What is field?
Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS table
consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information about every
record in the table.
What is record or row?
A record, also called a row of data, is each individual entry that exists in a table. For example
there are 7 records in the above CUSTOMERS table. Following is a single row of data or record
in the CUSTOMERS table:
+---- +---------- +----- -----------+ +----------
+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
+---- +---------- +----- -----------+ +----------
+
A record is a horizontal entity in a table.
What is column?
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
For example, a column in the CUSTOMERS table is ADDRESS, which represents location
description and would consist of the following:
Mr. Satish Pise
What is NULL value?
A NULL value in a table is a value in a field that appears to be blank, which means a field with
a NULL value is a field with no value.
It is very important to understand that a NULL value is different than a zero value or a field that
contains spaces. A field with a NULL value is one that has been left blank during record creation.
SQL Constraints:
Constraints are the rules enforced on data columns on table. These are used to limit the type of
data that can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints could be column level or table level. Column level constraints are applied only to one
column where as table level constraints are applied to the whole table.
Following are commonly used constraints available in SQL:
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database table.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.
Q. Explain not null constraints by suitable example.
Mr. Satish Pise
Not null constraint: It is a type of Domain integrity constraint. It is used to maintain the value
according to user specification. By default columns allows null values. When not null constraint is
assigned on a column or set of column, then it will not allow null values.
Syntax:
Create table <tablename>(column1 datatype(size),column2 datatype(size) not null);
Example:
Create table Emp (EmpId number(4),Empname varchar(20) not null);
Q. Explain Domain integrity constraint with syntax and example.
Q. Explain any four integrity constraints
1. Not Null constraint: This constraint ensures all rows in the table contain a definite
value for the column which is specified as not null. Which means a null value is not
allowed.
syntax: create table <table name>( Column_name Datatype (Size) [CONSTRAINT
constraint name] NOT NULL );
Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee
( id number(5),
name char(20) CONSTRAINT nm_nn NOT NULL,
dept char(10), age number(2),
salary number(10),
location char(10)
);
OR
For Example: To create a employee table with Null value, the query would be like
CREATE TABLE employee
( id number(5),
name char(20) NOT NULL,
dept char(10),
age number(2),
Mr. Satish Pise
salary number(10),
location char(10)
);
2) Check constraint: it defines a condition that each row must satisfy. A single
column can have multiple check constraints that reference the column in its
definition.
Syntax at table creation:
Create table <table_name>
(column_name1 datatype(size) constraint <constraint_name> check <condition or
logical expression>,
---
column_name n datatype(size)
);
Example:
create table emp( empno number(5), ename varchar2(25), salary number(7,2)
constraint emp_sal_ck check(salary > 5000), job varchar2(15) );
After table creation
Syntax:
Alter table <table_name> add constraint<constraint_name> check <condition>;
Example:
alter table emp add constraint emp_deptno_ck check(deptno>5);
3). Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of
specified column in table. It restricts null values too.
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME PRIMARY KEY);
Example:
SQL> CREATE TABLE EMP
(ID NUMBER (5)CONSTRAINT ID_PK PRIMARY KEY,
NAME VARCHAR2 (10),
SAL NUMBER (10));
Mr. Satish Pise
4). Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database
table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for
a column or set of columns.
Syntax:
CREATE TABLE TABLE_NAME
(COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME UNIQUE);
Example:
CREATE TABLE PERSONS
(P_ID NUM CONSTRAINT P_UK UNIQUE ,
FIRSTNAME VARCHAR(20),
CITY VARCHAR(20) );
5). Referential Integrity Constraint: It is a relational database concept in which multiple tables
share a relationship based on the data stored in the tables, and that relationship must remain
consistent. A value of foreign key is derived from primary key which is defined in parent table.
Syntax:
CREATE TABLE TABLE_NAME
(COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME REFERENCES
PARENT_TABLE_NAME (PARENT_TABLE_COL_NAME ON DELETE CASCADE,
COLUMN_NAME DATA_TYPE);
Example:
CREATE TABLE DEPARTMENT (EMP_ID NUMBER(5) REFERENCES EMP(EMP_ID),
DNO NUMBER(3));
Data Integrity:
The following categories of the data integrity exist with each RDBMS:
Entity Integrity: There are no duplicate rows in a table.
Mr. Satish Pise
Domain Integrity: Enforces valid entries for a given column by restricting the type, the format, or the
range of values.
Referential integrity: Rows cannot be deleted, which are used by other records.
User-Defined Integrity: Enforces some specific business rules that do not fall into entity, domain or
referential integrity.
Q. Explain Data Redundancy and Integrity.
Data redundancy :
Data redundancy is the unnecessary repetition of data.
Since different programmers create the files and application programs over a long period ,
the various files are likely to have different structures and programs may be written in
several programming languages. The same piece of information or program may be
duplicated in several places.e.g accounting department and registration department both
keep student name, number and address.
Data Redundancy
Increases the size of the database unnecessarily
Causes data inconsistency.
Increases the access cost and decreases efficiency of database.
May cause data corruption.
Such data redundancy in DBMS can be prevented by database normalization.
Data integrity:
Data integrity refers to maintaining and assuring the accuracy and consistency of data
over its entire life-cycle.
Data integrity is usually imposed during the database design phase through the use of
standard procedures and rules.
Data integrity can be maintained through the use of various error checking methods and
validation procedures. e.g The balance of certain type of bank account may never fall below
a prescribed amount( Rs.5000). We can handle this through program code and declaring
integrity constraint along with definition.
Mr. Satish Pise
Functional Dependency
Functional dependency (FD) is a set of constraints between two attributes in a relation. Functional
dependency says that if two tuples have same values for attributes A1, A2,..., An, then those two
tuples must have to have same values for attributes B1, B2, ..., Bn.
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand
side.
Q. Explain functional dependencies and 2 NF with example.
Q. Describe functional dependencies with example.
Functional Dependency:
A functional dependency occurs when one attribute in a relation uniquely determine
another attribute.
OR
Let „R‟ be a relation and let X and Y be any arbitrary attributes of R, then it can be said
that Y is functionally dependent on X if and only if, each X value is associated with
precisely one Y value. And it can be shown as X Y.
e.g. emp_id ename meaning ename functionally dependent on emp_id
Second Normal Form (2NF): A relation is said to be in the second normal form if it is
in first normal form and all the non key attributes are fully functionally dependent on the
primary key.
Example: If in the relation Supp(SNO,SNAME, LOCATION,PNO,QTY),the atrributes
SNAME AND LOCATION depends on SNO and QTY dependence on (SNO, PNO) so
the table can be split up into two tables as Supplier (SNO,SNAME,LOCATION) and
SP(SNO,PNO,QTY) and now both the tables are in second normal form.
Supplier:
Mr. Satish Pise
SP:
Q. What are the atomic values?
A piece of data in a database table that cannot be broken down any further called atomic
value.
OR
The value of each attribute containing only a single value from that attribute domain is
called atomic value.
Q. Given: Student (Roll_No, Name, Class, Total_Marks, Percentage, Grade)
find appropriate dependencies and normalize up to 3NF Functional Dependencies:
Roll_No Name
Roll_No Class
Total_Marks Percentage
Percentage Grade
1NF:
1) Student (Roll_No, Name, Class, Total_Marks, Percentage, Grade)
2NF:
1) Student(Roll_No, Name, Class)
2) Marks(Roll_No, Total_Marks, Percentage, Grade)
3NF:
Mr. Satish Pise
1) Student(Roll_No, Name, Class )
2) Marks(Roll_No, Total_Marks, Percentage)
3) Grade (Percentage, Grade)
Q. Explain Functional and Transitive dependency. Functional Dependency:
A functional dependency occurs when one attribute in a relation uniquely determine
another attribute.
OR
In the given relation R, attribute „Y‟ of R is functionally dependent on attribute „ „X‟ if
and only if whenever two tuples of R are having same „X‟ value then „Y‟ value will be
ndent on emp_id)
Transitive dependency:
A transitive dependency can occur only in a relation that has three or more attributes. Let
A, B, and C designates three distinct attributes (or distinct collections of attributes) in the
relation. Suppose all three of the following conditions hold:
1. A → B
2. It is not the case that B → A
3. B → C
Then the functional dependency A → C is a transitive dependency.
OR
If we have a primary key A and a non-key domain B and C, where C is more dependent on B than
A and B is directly dependent on A, then C can be considered transitively dependent on A.
Example:
Consider the relation SUPPLIER (SNO, SNAME, CITY, STATUS).
In this dependency SNAME is functionally dependent on SNO and STATUS is dependent
on CITY with CITY dependent on SNO, which shows a transitive dependency i.e.
STATUS is depending on SNO(via CITY).
Mr. Satish Pise
Normalization
Q. Define normalization.
Q. What is meant by database normalization?
“Normalization can be defined as process of decomposition of database tables to avoid the data
redundancy.”
If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.
Update anomalies − If data items are scattered and are not linked to each other properly, then it could
lead to strange situations. For example, when we try to update one data item having its copies scattered
over several places, a few instances get updated properly while a few others are left with old values.
Such instances leave the database in an inconsistent state.
Deletion anomalies − We tried to delete a record, but parts of it was left undeleted because of
unawareness, the data is also saved somewhere else.
Insert anomalies − We tried to insert data in a record that does not exist at all.
Normalization is a method to remove all these anomalies and bring the database to a consistent
state.
Q. List advantages of Normalization.
Advantages of the normalization.
More efficient data structure.
Avoid redundant fields or columns.
More flexible data structure i.e. we should be able to add new rows and data values easily
Better understanding of data.
Ensures that distinct tables exist when necessary.
First Normal Form
Mr. Satish Pise
First Normal Form is defined in the definition of relations (tables) itself. This rule defines that all
the attributes in a relation must have atomic domains. The values in an atomic domain are
indivisible units.
We re-arrange the relation (table) as below, to convert it to First Normal Form.
Each attribute must contain only a single value from its pre-defined domain.
Second Normal Form
Before we learn about the second normal form, we need to understand the following −
Prime attribute − An attribute, which is a part of the prime-key, is known as a prime attribute.
Non-prime attribute − An attribute, which is not a part of the prime-key, is said to be a non-prime
attribute.
If we follow second normal form, then every non-prime attribute should be fully functionally
dependent on prime key attribute. That is, if X → A holds, then there should not be any proper
subset Y of X, for which Y → A also holds true.
We see here in Student_Project relation that the prime key attributes are Stu_ID and Proj_ID.
According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must be dependent upon
both and not on any of the prime key attribute individually. But we find that Stu_Name can be
Mr. Satish Pise
identified by Stu_ID and Proj_Name can be identified by Proj_ID independently. This is
calledpartial dependency, which is not allowed in Second Normal Form.
We broke the relation in two as depicted in the above picture. So there exists no partial
dependency.
Third Normal Form
For a relation to be in Third Normal Form, it must be in Second Normal form and the following
must satisfy −
No non-prime attribute is transitively dependent on prime key attribute.
For any non-trivial functional dependency, X → A, then either −
X is a superkey
Q. Explain Third normal form.
Q. Explain 3NF with example.
Mr. Satish Pise
3NF (Third normal form)
1) After removing all transitive dependencies and making separate relations, relations get into 3NF.
2) Transitive dependency is can be stated as, let R be relation and A, B and C be the set of
attributes then, they are transitive dependent if C depends on B, B depends on A and therefore C
depends on A.
Example
Student_Detail Table :
Student_id
Student_name DOB treet city State Zip
In this table Student_id is Primary key, but street, city and state depends upon Zip. The dependency
between zip and other fields is called transitive dependency. Hence to apply 3NF, we need to
move the street, city and state to new table, with Zip as primary key.
New Student_Detail Table :
Student_id
Student_name DOB zip
Address Table :
Zip Street City state
Boyce-Codd Normal Form
Boyce-Codd Normal Form (BCNF) is an extension of Third Normal Form on strict terms. BCNF
states that −
For any non-trivial functional dependency, X → A, X must be a super-key.
In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the super-key
in the relation ZipCodes. So,
Mr. Satish Pise
Stu_ID → Stu_Name, Zip
and
Zip → City
Which confirms that both the relations are in BCNF.
Q. State properties of Boyce Codd Normal Form.
Properties of Boyce Codd normal form
1. BCNF: A relation R is in Boyce - Codd normal form (BCNF) if and only if every determinant is a
candidate key.
2. In BCNF non-trivial functional dependency is preserved for super key.
3. A table can be in 3NF but not in BCNF.
4. 3NF does not deal satisfactorily with the case of a relation with overlapping candidates keys, in such
case BCNF can be used.
5. The violation of BCNF means that the table is subject to anomalies.
Q. Explain BCNF with example.
BCNF: Definition: A relation R is in Boyce-Codd normal form (BCNF) if and only if
every determinant is a candidate key.
OR
BCNF: A relation R is in BCNF if whenever on nontrivial functional dependency
X→A holds in R, then X is a superkey of R.
Example:
Consider the relation SUPPLIER (SNO, SNAME, PH_NO, CITY) having SNO
and SNAME unique. In this there are 2 determinants SNO, SNAME as PH_NO and
CITY dependence upon them and both are candidate keys. So this is in BCNF.
Multivalued Dependencies
1. Functional dependencies rule out certain tuples from appearing in a relation.
Mr. Satish Pise
If A B, then we cannot have two tuples with the same A value but
different B values.
2. Multivalued dependencies do not rule out the existence of certain tuples.
Instead, they require that other tuples of a certain form be present in the
relation.
3. Let R be a relation schema, and let and .
The multivalued dependency
holds on R if in any legal relation r(R), for all pairs of tuples and in r such
that , there exist tuples and in r such that:
Q. What is multi-valued dependency? Multivalued dependencies occur when the presence of one or more rows in a table
Mr. Satish Pise
implies the presence of one or more other rows in that same table.
OR
A multivalued dependency (MVD):In a relational table R with columns A, B and C
then R. A ->> R.B (column A multi-determines column B) is true if and only if the set
of B –values matching a given pair of A-values and C- values in R depends only on A- value and is
independent of C-value
OR
The multivalued dependency α ->> β holds on R if, in any legal relation r(R), for all
pairs of tuples t1 and t2 in r such that t1[α] = t2[α], there exist tuples t3 and t4 in r such
that
t1[α] = t2[α] = t3[α] = t4[α]
t3[β] = t1[β]
t3[R − β] = t2[R − β]
t4[β] = t2[β]
t4[R − β] = t1[R − β]
Normalization Using Multivalued
Dependencies (not to be covered)
1. Suppose that in our banking example, we had an alternative design including
the schema:
2. BC-schema = (loan#, cname, street, ccity)
3. We can see this is not BCNF, as the functional dependency
cname street ccity
holds on this schema, and cname is not a superkey.
Mr. Satish Pise
4. If we have customers who have several addresses, though, then we no longer
wish to enforce this functional dependency, and the schema is in BCNF.
5. However, we now have the repetition of information problem. For each
address, we must repeat the loan numbers for a customer, and vice versa.
DBMS – Joins
Join is a combination of a Cartesian product followed by a selection process. A Join operation
pairs two tuples from different relations, if and only if a given join condition is satisfied.
We will briefly describe various join types in the following sections.
Theta (θ) Join
Theta join combines tuples from different relations provided they satisfy the theta condition. The
join condition is denoted by the symbol θ.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that the
attributes don‟t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Mr. Satish Pise
Outer Joins
Theta Join, Equijoin, and Natural Join are called inner joins. An inner join includes only those
tuples with matching attributes and the rest are discarded in the resulting relation. Therefore, we
need to use outer joins to include all the tuples from the participating relations in the resulting
relation. There are three kinds of outer joins − left outer join, right outer join, and full outer join.
Q. What is OUTER JOIN? Explain in detail.
Q. Explain types of JOINs.
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join
condition along with rows which do not satisfy the join condition. The SQL OUTER JOIN
operator (+) is used only on one side of the join condition only.
Outer join are of three types:
1. Left outer join (also known as left join): this join returns all the rows from left table combine
with the matching rows of the right table. If we get no matching in the right table it returns NULL
values.
Left Outer Join syntax : SELECT column-name-list from tablename LEFT OUTER JOIN table-
name2 on table-name1.columnname = table-name2.column-name;
2. Right outer join (also known as right join): this join returns all the rows from right table are
combined with the matching rows of left table .If we get no column matching in the left table, it
returns null value.
Right Outer Join Syntax:SELECT column-name-list from tablename1 RIGHT OUTER JOIN
table-name2 on tablename1. column-name = table-name2.column-name;
3. The full outer join keyword returns all records when there is a match in either left (table1) or
right (table2) table records.
FULL OUTER JOIN Syntax:SELECT column_name(s)from table1 full outer join table2 on
table1.column_name =table2.column_name;
4. INNER JOIN or EQUI JOIN:
A join which is based on equalities is called equi join. In equi join comparison operator
“=” is used to perform a Join.
Mr. Satish Pise
Syntax:
SELECT tablename.column1_name,tablename.column1_name
FROM table_name1,table_name2
where table_name1.column_name=table_name2.column_name;
Example:
Select stud_info.stud_name,stud_info.branch_code,branch_details.location
From stud_info,branch_details
Where
Stud_info.branch_code=branch_details.branch_code;
5. SELF JOIN:
The SQL SELF JOIN is used to join a table to itself, as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
Syntax:
SELECT a.column_name, b.column_name...
FROM table1 a, table1 b
WHERE a.common_filed = b.common_field;
Example:
Select x.stud_name, y.stud_name from stud_infox,stud_info y
Where x.leader= y.stud_id;
Q. Explain Inner join and Outer join with example.
INNER Join: This is a simple JOIN in which the result is based on matched data as per
the condition specified in the query.
Inner Join Syntax :
SELECT column_name_list
from table_name1
INNER JOIN
table_name2
on table_name1.column_name = table_name2.column_name;
Inner Join Example :
SELECT * from emp inner join dept on emp.id = dept.id;
Outer Join is based on both matched and unmatched data. Outer Joins subdivide
Mr. Satish Pise
further into,
Left Outer Join
Right Outer Join
Full Outer Join
Left Outer Join
The left outer join returns a result table with the matched data of two tables then
remaining rows of the left table and null for the right table's column.
Left Outer Join syntax :
SELECT column-name-list from table-name LEFT OUTER JOIN table-name2
on table-name1.column-name = table-name2.column-name;
Left Outer Join Example:
SELECT * FROM emp LEFT OUTER JOIN dept ON (emp.id=dept.id);
Right Outer Join
The right outer join returns a result table with the matched data of two tables then
remaining rows of the right table and null for the left table's columns.
Right Outer Join Syntax:
select column-name-list from table-name1 RIGHT OUTER JOIN table-name2
on table-name1.column-name = table-name2.column-name;
Right Outer Join Example:
SELECT * FROM emp RIGHT OUTER JOIN dept on (emp.id=dept.id)
Full Outer Join
The full outer join returns a result table with the matched data of two table then
remaining rows of both left table and then the right table.
Full Outer Join Syntax :
select column-name-list from table-name1 FULL OUTER JOIN table-name2
on table-name1.column-name = table-name2.column-name;
Full Outer Join Example:
select empname,sal from emp FULL OUTER JOIN dept on emp.id = dept.id;
Mr. Satish Pise
Left Outer Join(R S)
All the tuples from the Left relation, R, are included in the resulting relation. If there are tuples
in R without any matching tuple in the Right relation S, then the S-attributes of the resulting
relation are made NULL.
Right Outer Join: ( R S )
All the tuples from the Right relation, S, are included in the resulting relation. If there are tuples
in S without any matching tuple in R, then the R-attributes of resulting relation are made
NULL.
Courses HoD
Full Outer Join: ( R S)
All the tuples from both participating relations are included in the resulting relation. If there are
no matching tuples for both relations, their respective unmatched attributes are made NULL.
Courses HoD
Mr. Satish Pise
Integrity Constraints
1. Integrity constraints provide a way of ensuring that changes made to the
database by authorized users do not result in a loss of data consistency.
2. We saw a form of integrity constraint with E-R models:
o key declarations: stipulation that certain attributes form a candidate
key for the entity set.
o form of a relationship: mapping cardinalities 1-1, 1-many and
many-many.
3. An integrity constraint can be any arbitrary predicate applied to the database.
4. They may be costly to evaluate, so we will only consider integrity
constraints that can be tested with minimal overhead.
Domain Constraints
A domain is defined as the set of all unique values permitted for an attribute. For example, a
domain of date is the set of all possible valid dates, a domain of integer is all possible whole
numbers, a domain of day-of-week is Monday, Tuesday ... Sunday.
This in effect is defining rules for a particular attribute. If it is determined that an attribute is a
date then it should be implemented in the database to prevent invalid dates being entered.
Entity Integrity
A requirement of E F Codd in his seminal paper is that a primary key of an entity, or any part of
it, can never take a null value.
Oracle, and most other relational database management systems, will enforce this.
Mr. Satish Pise
Referential Integrity
This is the most common type of integrity constraint. This is used to manage the relationships
between primary and foreign keys.
Referential integrity is best illustrated by an example.
Let's assume the department and employee entities have been implemented as tables in a
relational database system.
When entering a new employee, the department in which they work needs to be specified.
Department number is the foreign key in the employee table and the primary key in the
department table.
In order to preserve the integrity of the data in the database there are a set of rules that need to be
observed:
If inserting an employee in the employee table, the insertion should only be allowed if
their department number exists in the department table
If deleting a department in the department table, the deletion should only be allowed if
there are no employees working in that department
If changing the value of a department number in the department table, the update should
only be allowed if there are no employees working in the department whose number is
being changed
If changing the value of a department number in the employee table, the update should
only be allowed if the new value exists in the department table
If any of the above is allowed to happen then we have data in an inconsistent state. The integrity
of the data is compromised - the data does not make sense.
Cascade Events
Some relational database management systems have extended the options when a referential
integrity rule is broken. Rather than give an error message they perform further actions on the
data to preserve data integrity. These are known as cascade events. When physically creating a
database table (see next section) foreign keys can, optionally, be defined with cascade rules.
These state:
Mr. Satish Pise
Cascade Delete: if a row in the referenced table is deleted, then all rows in the
referencing table with a foreign key value equal to the primary key value of the row
should also be deleted. Using our previous example, when a department is deleted from
the department table, any rows in the employee table that reference the department
number are also deleted.
Cascade Update: if the primary key value of a row in a referenced table is updated, all
rows in the referencing table with a foreign key value equal to the primary key value of
this row, should also be updated to the new value. Using our previous example, when the
value of a department number in the department table is updated, any department
numbers in the employee table equal to the department number being updated are also
changed to the new value.
Set To Null: this is similar to Cascade Delete except rather than deleting the referencing
rows, the foreign key in the referencing table is set to null (see the later discussions
on null values). That is, an unknown value. Oracle supports Cascade Delete (referred to
as 'On Delete Cascade') and Set To Null (referred to as 'On Delete Set Null') when
creating tables. However, you will not be asked to implement these rules.
Q. Explain entity integrity constraints with syntax and example.
There are two Entity constraints: 1. Primary Key constraint
2. Unique Constraint
1. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the
row of specified column in table. It restricts null values too.
Syntax:
CREATE TABLE TABLE_NAME
(COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME PRIMARY
KEY);
Example:
SQL> CREATE TABLE EMP
(ID NUMBER (5)CONSTRAINT ID_PK PRIMARY KEY,
NAME VARCHAR2 (10),
Mr. Satish Pise
SAL NUMBER (10));
Unique Constraint: The UNIQUE constraint uniquely identifies each record in a
database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee
for uniqueness for a column or set of columns.
Syntax:
CREATE TABLE TABLE_NAME
(COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME UNIQUE);
Example:
CREATE TABLE PERSONS
(P_ID NUM CONSTRAINT P_UK UNIQUE ,
FIRSTNAME VARCHAR(20),
CITY VARCHAR(20) );
Q. List and explain the types of integrity constraints in detail.
Integrity constraints: Not Null constraint, CHECK constraint, Primary Key constraint, Unique
Constraint, Referential Integrity Constraint
1. Not Null: By default all columns in tables allows null values. When a NOT NULL constraint
is enforced on column or set of columns it will not allow null values.
Syntax for NOT NULL CREATE TABLE TABLE_NAME (COLUMN_NAME
DATA_TYPE, COLUMN_NAME DATA_TYPE NOT NULL);
Example: SQL>CREATE TABLE STUDENT (ROLL_NO NUMBER(5), NAME
VARCHAR2(20) NOT NULL);
2. CHECK: The constraint defines a condition that each row must satisfy. A single column can
have multiple check condition.
Mr. Satish Pise
Syntax:- CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME CHECK
<CONDITION>);
Example: SQL> CREATE TABLE EMP (ID NUMBER(5), NAME VARCHAR2(10), SAL
NUMBER(10) CONSTRINT CHK_SAL CHECK (SAL>15000));
3. Primary Key constraint: It is use to avoid redundant/duplicate value entry within the row of
specified column in table. It restricts null values too.
Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME PRIMARY KEY);
Example: SQL> CREATE TABLE EMP (ID NUMBER (5) CONSTRAINT ID_PK PRIMARY
KEY, NAME VARCHAR2 (10), SAL NUMBER (10));
4. Unique Constraint: The UNIQUE constraint uniquely identifies each record in a database
table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness
for a column or set of columns. It allows null value.
Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME UNIQUE);
Example: CREATE TABLE PERSONS (P_ID NUMBER CONSTRAINT P_UK UNIQUE,
FIRSTNAME VARCHAR (20), CITY VARCHAR (20));
5. Referential Integrity Constraint: It is a relational database concept in which multiple tables
share a relationship based on the data stored in the tables, and that relationship must remain
consistent. A value of foreign key is derived from primary key which is defined in parent table.
Mr. Satish Pise
Syntax: CREATE TABLE TABLE_NAME (COLUMN_NAME DATA_TYPE,
COLUMN_NAME DATA_TYPE CONSTRAINT CONSTRAINT_NAME REFERENCES
PARENT_TABLE_NAME (PARENT_TABLE_COL_NAME ON DELETE CASCADE,
COLUMN_NAME DATA_TYPE);
Example: CREATE TABLE DEPARTMENT (EMP_ID NUMBER(5) REFERENCES
EMP(EMP_ID), DNO NUMBER(3));
Q. Explain referential integrity constraints with example.
Referential integrity constraint:
It is used to established a parent child relationship between two tables.
A value of foreign key is derived from the primary key.
Primary key is defined in a parent table and foreign key is defined in child table. The
child table contains the values for foreign key column which are present in parent tables
primary key column but not other than that.
Syntax:
Create table tablename (column datatype size references parenttablename (primary
key attribute)….)
Example:
Create table product (EmpId number (4) references Emp (EmpId), PName
varchar2(10));
After table creation the foreign key is added as:
Alter table product add constraint fk_prod foreign key (EmpId) references Emp (EmpId);
Mr. Satish Pise
Q. Explain Database security with its requirements.
Q. What do you mean by database security?
Database Security: Database security refers to the collective measures used to protect
and secure a database or database management software from illegal use and malicious threats
and attacks. Database security covers and enforces security on all aspects and components of
databases like Data stored in database, Database server, DBMS.
Data Security Requirements:
1. Authentication: System verifies a user's identity.
2. Authorization: Which database operations that user may perform (like read, update,
drop etc.) and which data objects that user may access.
3. Secure Storage of Sensitive Data: Once confidential data has been entered, its
integrity and privacy must be protected on the databases and servers wherein it
resides.
4. Integrity: Data integrate means that data is protected from deletion and corruption
5. Availability: A secure system makes data available to authorized users, without delay.
6. Confidentiality: A secure system ensures the confidentiality of data. This means
that it allows individuals to see only the data they are supposed to see.
Importance of Security in Database Environment
Database security is the protection of the database against intentional and unintentional threats
that may be computer-based or non-computer-based. Database security is the business of the
entire organization as all people use the data held in the organization's database and any loss or
corruption to data would affect the day-to-day operation of the organization and the performance
of the people. Therefore, database security encompasses hardware, software, infrastructure,
people and data of the organization.
Mr. Satish Pise
Data Security Risks
We have seen that the database security is the concern of the entire organization. The
organization should identify all the risk factors and weak elements from the database security
Perspective and find solutions to counter and neutralize each such threat.
A threat is any situation, event or personnel that will adversely affect the database security and
the smooth and efficient functioning of the organization. A threat may be caused by a situation or
event involving a person, action or circumstance that is likely to bring harm to the organization.
The harm may be tangible, such as loss of data, damage to hardware, loss of software or
intangible such as loss of customer goodwill or credibility and so on.
Data Tampering
Privacy of communications is essential to ensure that data cannot be modified or viewed in
transit. The chances of data tampering are high in case of distributed environments as data moves
between sites. In a data modification attack, an unauthorized party on the network intercepts data
in transit and changes that data before retransmitting it. An example of this is changing the
amount of a banking transaction from Rs. 1000 to Rs. 10000.
Data Theft
Data must be stored and transmitted securely, so that information such as credit card numbers
cannot be stolen. Over the Internet and Wide Area Network (WAN)
environments, both public carriers and private network owners often route portions of their
network through insecure landlines, extremely vulnerable microwave and satellite links, or a
number of servers. This situation leaves valuable data opens to view by any interested party. In
Local Area Network (LAN) environments within a building or campus, insiders with access to
the physical wiring can potentially view data not intended for them.
Falsifying User Identities
Mr. Satish Pise
In a distributed environment, it becomes more feasible for a user to falsify an identity to gain
access to sensitive and important information. Criminals attempt to st.eal users' credit card
numbers, and then make purc~1ases against the accounts. Or they steal other personal data, such
as bank account numbers and driver's license numbers, and setup bogus credit accounts in
someone else's name.
Password-Related Threats
In large systems, users must remember multiple passwords for the different applications and
services that they use. Users typically respond to the problem of managing multiple passwords in
several ways:
• They may select easy-to-guess password
• They may also choose to standardize passwords so that they are the same on all machines or
websites.
All these strategies compromise password secrecy and service availability. Moreover,
administration of multiple user accounts and passwords is complex, time-consuming, and
expensive.
Unauthorized Access to Tables and Columns
The database may contain confidential tables, or confidential columns in a table, which should
not be available indiscriminately to all users authorized to access the database. It should be
possible to protect data on a column level.
Unauthorized Access to Data Rows
Certain data rows may contain confidential information that should not be available
indiscriminately to users authorized to access the table. For example, in a shared environment'
businesses should have access only to their own data; customers should be able to see only their
own orders.
Mr. Satish Pise
Lack of Accountability
If the system administrator is unable to track users' activities, then users cannot ~e held
responsible for their actions. There must be some reliable ways to monitor who is performing
what operations on the data.
Complex User Management Requirements
System must often support large number of users and therefore they must be scalable.
In such large-scale environments, the burden of managing user accounts and passwords makes
your system yulnerable to error and attack.
Security Levels
To protect the database, we must take security measures at several levels:
• Physical: The sites containing the computer systems must be secured against armed or
surreptitious entry by intruders.
• Human: Users must be authorized carefully to reduce the chance of any such user giving
access to an intruder in exchange for a bribe or other favors .
•Operating System: No matter how secure the database system is, weakness in operating system
security may serve as a means of unauthorized access to the database.
• Network: Since almost all database systems allow remote access through terminals or
networks, software-level security within the network software is as important as physical
security, both on the Internet and in networks private to an enterprise.
• Database System: Some database-system users may be authorized to access only a limited
portion of the database. Other users may be allowed to issue queries, but may be forbidden to
Mr. Satish Pise
modify the data. It is responsibility of the database system to ensure that these authorization
restrictions are not violated.
Security at all these levels must be maintained if database security is to be ensured. A weakness
at a low level of security (physical or human) allows circumvention of strict high level (database)
security measures.
Data Security Requirements
Confidentiality
A secure system ensures the confidentiality of data. This means that it allows individuals to see
only the data they are supposed to see. Confidentiality has several aspects like privacy of
communications, secure storage of sensitive data, authenticated users and authorization of users.
Privacy of Communications
The DBMS should be capable of controlling the spread of confidential personal information such
as health, employment, and credit records. It should also keep the corporate data such as trade
secrets, proprietary information about products and processes, competitive analyses, as well as
marketing and sales plans secure and away from the unauthorized people.
Secure Storage of Sensitive Data
Once confidential data has been entered, its integrity and privacy must be protected on the
databases and servers wherein it Resides.
Authentication
Mr. Satish Pise
One of the most basic concepts in database security is authentication, which is quite simply the
process by which it system verifies a user's identity, A user can respond to a request to
authenticate by providing a proof of identity, or an authentication token
claim to being a person authorized to access the environment, and the password is protected and
you are the only person who knows it.
Authorization
An authenticated user goes through the second layer of security, authorization. Authorization is
the process through which system obtains information about the authenticated user, including
which database operations that user may perform and which data objects that user may access.
Integrity
A secure system en sums that the data it contains is valid. Data integrate means that data is
protected from deletion and corruption, both while it resides within the data-case, and while it is
being transmitted over the network. The detailed discussion on Integrity is un next section.
Availability
A secure system makes data available to authorized users, without delay. Denial of service
attacks are attempts to block authorized users' ability to access and use the system when needed.