DBMS Unit-Iii
DBMS Unit-Iii
OVERVIEW:
• The SQL language has several aspects to it.
• DML (Data Manipulation Language)
• DDL (Data Definition Language)
• Triggers and Advanced Integrity Constraints
• Embedded and Dynamic SQL
• SQL code to be called from a host language such as C or COBOL or JAVA
• Dynamic SQL (Query to be constructed (and executed) as run-time
• Client-Server Execution and Remote Database Access:
• These commands control how a client application program can connect to an SQL db
server; or access db over a network.
• Transaction Management (explicitly control aspects of how a transaction is to be executed)
• Security: mechanisms to control users access to data objects such as tables & views
• Advanced Features: OO features, recursive queries, DS queries, DM, spatial data etc.
THE FORM A BASIC SQL QUERY
• The basic form of an SQL query is as follows:
• SELECT [DISTINCT] select-list
• FROM from-list
• WHERE qualification
• Every query must have a SELECT clause, which specifies columns to be retained in the result, and a FROM
clause, which specifies a cross-product of tables.
• The optional WHERE Clause specifies selection conditions on the tables mentioned in the FROM clause.
(Q15) Find the names and ages of all sailors.
SELECT DISTINCT S.sname, S.age FROM Sailors S
SELECT clause used to do projection Whereas selections in the relational algebra sense are expressed using the WHERE
clause
The from-list in the FROM clause is a list of table names.
Table name can be followed by a range variable; (useful when same table name appears more than once)
(Q1) Find the names of sailors who have reserved boat number 103.
SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid = 103
(Q16) Find the sids of sailors who have reserved a red boat.
SELECT R.sid FROM Boats B, Reserves R WHERE B.bid = R.bid AND B.color = ‘red’
(Q2) Find the names of sailors who have reserved a red boat
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
(Q4) Find the names of sailors who have reserved at least one boat.
SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid
Each item in a select-list can be of the form expression AS column name, where expression is any arithmetic or string
expression over column names and constants, and column name is a new name for this column in the output of the query.
(Q17) Compute increments for the ratings of persons who have sailed two different boats on the same day
SELECT S.sname, S.rating+1 AS rating FROM Sailors S, Reserves R1, Reserves R2 WHERE S.sid = R1.sid AND S.sid
= R2.sid AND R1.day = R2.day AND R1.bid <> R2.bid
In addition, SQL provides support for pattern matching through the LIKE operator, along with the use of the wild-card
symbols %
Thus ‘AB%’ denotes a pattern matching every string that contains at least three characters, with the second and third
characters being A and B respectively.
(Q18) Find the ages of sailors whose name begins and ends with B and has at least three characters
(Q5) Find the names of sailors who have reserved a red or a green boat.
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid and R.bid = B.bid AND (B.color = ‘red’
OR B.color = ‘green’)
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
UNION
SELECT S2.sname FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND
B2.color = ‘green’ AND B1.color = ‘red’ AND B2.color = ‘green’
(Q6) Find the names of sailors who have reserved both a red and a green boat
SELECT S.sname FROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats B2 where S.sid = R1.sid AND R1.bid =
B1.bid AND S.sid = R2.sid AND R2.bid = B2.bid
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
INTERSECT
SELECT S2.sname FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid = R2.bid AND R2.bid = B2.bid AND
B2.color = ‘green’
(Q19) Find the sids of all sailors who have reserved red boats but not green boats.
SELECT S.sid FROM Sailors S, Reserves R, Boats B WHERE S.sid = R.sid AND R.bid = B.bid AND B.color = ‘red’
EXCEPT
SELECT S2.sid FROM Sailors S2, Reserves R2, Boats B2 WHERE S2.sid = R2.sid AND R2.bid = B2.bid AND B2.color
= ‘green’
OR
SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = ‘red’
EXCEPT
SELECT R2.sid FROM Boats B2, Reserves R2 WHERE R2.bid = B2.bid AND B2.color = ‘green’
20. Find all sailors who have a rating of 10 or reserved boat 104.
NESTED QUERIES
A Nested Query is a query that has another query within it; the embedded query is called a subquery.
SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid = 103 )
SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R
WHERE R.bid IN ( SELECT B.bid FROM Boats B WHERE B.color = `red' )
(Q21) Find the names of sailors who have not reserved a red boat.
(Q1) Find the names of sailors who have reserved boat number 103.
(Q22) Find sailors whose rating is better than some sailor called Horatio.
SELECT S.sid FROM Sailors S WHERE S.rating > ANY ( SELECT S2.rating
FROM Sailors S2 WHERE S2.sname = `Horatio' )
SELECT S.sid FROM Sailors S WHERE S.rating >= ALL (SELECT S2.rating FROM Sailors S2 )
(Q6) Find the names of sailors who have reserved both a red and a green boat.
(Q30) Find the names of sailors who are older than the oldest sailor with a rating of 10.
Where I = 1,2….10. Writing 10 such queries is tedious. More important, we may not know what rating levels exists in
advance.
To write such queries, we need a major extension to the basic SQL query form, namely, the GROUP BY clause.
The extension also includes an option HAVING clause that can be used to specify qualifications over groups.
(Q32) Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for each rating level with at least two
such sailors
SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating
HAVING COUNT (*) > 1
(Q33) For each red boat, find the number of reservations for this boat
SELECT B.bid, COUNT (*) AS sailorcount FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = `red'
GROUP BY B.bid
(Q34) Find the average age of sailors for each rating level that has at least two sailors
SELECT S.rating, AVG (S.age) AS avgage FROM Sailors S GROUP BY S.rating HAVING COUNT (*) > 1
(Q35) Find the average age of sailors who are of voting age (i.e., at least 18 years old)
for each rating level that has at least two sailors.
(Q36) Find the average age of sailors who are of voting age (i.e., at least 18 years old) for each rating level that has at least two such
sailors
SELECT S.rating, AVG ( S.age ) AS avgage FROM Sailors S WHERE S. age > 18 GROUP BY S.rating
HAVING 1 < ( SELECT COUNT (*) FROM Sailors S2 WHERE S.rating = S2.rating AND S2.age >= 18 )
(Q37) Find those ratings for which the average age of sailors is the minimum overall Ratings
SELECT S.rating FROM Sailors S WHERE AVG (S.age) = ( SELECT MIN (AVG (S2.age)) FROM Sailors S2
GROUP BY S2.rating )
We can specify complex constraints over a single table using table constraints, which have the form CHECK conditional-expression.
For example, to ensure that rating must be an integer in the range 1 to 10, we could use:
To enforce the constraint that Interlake boats cannot be reserved, we could use:
CREATE TABLE Reserves ( sid INTEGER,
bid INTEGER,
day DATE,
FOREIGN KEY (sid) REFERENCES Sailors
FOREIGN KEY (bid) REFERENCES Boats
CONSTRAINT noInterlakeRes
CHECK ( `Interlake' <>
( SELECT B.bname
FROM Boats B
WHERE B.bid = Reserves.bid )))
Domain Constraints and Distinct Types
A user can de_ne a new domain using the CREATE DOMAIN statement, which makes uses of CHECK constraints.
CREATE DOMAIN ratingval INTEGER DEFAULT 0 CHECK ( VALUE >= 1 AND VALUE <= 10 )
INTEGER is the base type for the domain ratingval, and every ratingval value must be of this type. Values in ratingval are further
restricted by using a CHECK constraint; in de_ning this constraint, we use the keyword VALUE to refer to a value
in the domain.
The optional DEFAULT keyword is used to associate a default value with a domain. If the domain ratingval is used for a column in
some relation, and no value is entered for this column in an inserted tuple, the default value 0 associated with ratingval is used.
Table constraints are associated with a single table, although the conditional expression in the CHECK clause can refer to other tables.
Table constraints are required to hold only if the associated table is nonempty. Thus, when a constraint involves two or more tables,
the table constraint mechanism is sometimes cumbersome and not quite what is desired. To cover such situations, SQL supports the
creation of assertions, which are constraints not associated with any one table.
As an example, suppose that we wish to enforce the constraint that the number of boats plus the number of sailors should be less than
100.
CREATE TABLE Sailors ( sid INTEGER,
sname CHAR(10),
rating INTEGER,
age REAL,
PRIMARY KEY (sid),
CHECK ( rating >= 1 AND rating <= 10)
CHECK ( ( SELECT COUNT (S.sid) FROM Sailors S )
+ ( SELECT COUNT (B.bid) FROM Boats B )
< 100 ))
A trigger can be thought of as a `daemon' that monitors a database, and is executed when the database is modified in a way that
matches the event specification. An insert, delete or update statement could activate a trigger, regardless of which user or application
invoked the activating statement; users may not even be aware that a trigger was executed as a side effect of their program.
In an active database system, when the DBMS is about to execute a statement that modifies the database, it checks whether some
trigger is activated by the statement. If so, the DBMS processes the trigger by evaluating its condition part, and then (if the Condition
evaluates to true) executing its action part.
If a statement activates more than one trigger, the DBMS typically processes all of them, in some arbitrary order. An important point
is that the execution of the action part of a trigger could in turn activate another trigger. In particular, the execution of the action part
of a trigger could again activate the same trigger; such triggers are called
recursive triggers. The potential for such chain activations, and the unpredictable order in which a DBMS processes activated
triggers, can make it difficult to understand the effect of a collection of triggers.
SCHEMA REFINEMENT AND NORMAL FORMS
● Redundant Storage
● Update Anomalies
● Insertion Anomalies
● Deletion Anomalies
Decompositions
The essential idea is that many problems arising from redundancy can be addressed by replacing a relation with a
collection of ‘smaller’ relations.
A decomposition of a relation schema R consists of replacing the relation schema by two (or more) relation schemas
that each contain a subset of the attributes of R and together include all attributes in R. Institutively, we want to store the
information in any given instance of R by storing projections of the instance.
To help with the first question, several normal forms have been proposed for relations. If a relation schema is in one of
these normal forms, we know that certain kinds of problems cannot arise.
W.r.t the second question, two properties of decompositions are of particular interest. The lossless-join property enables us
to recover any instance of the decomposed relation from corresponding instances of the smaller relations. The dependency-
preservation property enables us to enforce any constraint on the original relation by simply enforcing some constraints on
each of the smaller relations.
Functional Dependencies
A functional dependency (FD) is a kind of IC that generalizes the concept of a key.
Let R be a relation schema and let X and Y be nonempty sets of attributes in R. We say that an instance r of R satis_es the FD X ! Y 1 if
the following holds for every pair of tuples t1 and t2 in r:
We use the notation t1:X to refer to the projection of tuple t1 onto the attributes in X, in a natural extension of our TRC notation (see
Chapter 4) t:a for referring to attribute a of tuple t. An FD X->Y essentially says that if two tuples agree on the values in attributes X,
they must also agree on the values in attributes Y.
Figure 15.3 illustrates the meaning of the FD AB -> C by showing an instance that satisfies this dependency. The first two tuples show
that an FD is not the same as a key constraint: Although the FD is not violated, AB is clearly not a key for the
relation. The third and fourth tuples illustrate that if two tuples differ in either the A field or the B field, they can differ in the C field
without violating the FD. On the other hand, if we add a tuple ha1; b1; c2; d1i to the instance shown in this figure, the resulting
instance would violate the FD; to see this violation, compare the first tuple
in the figure with the new tuple
What is Relation?
A relation is a named two–dimensional table of data. Each relation consists of a set of named columns and an arbitrary
number of unnamed rows.
For example, a relation named Employee contains following attributes, emp-id, ename, dept name and salary.
marketing 42000
A functional dependency is a constraint between two attributes (or) two sets of attributes.
For example, the table EMPLOYEE has 4 columns that are Functionally dependencies on EMP_ID.
Partial functional dependency: It is a functional dependency in which one or more nonkey attributes are functionally
dependent on part of the primary key. Consider the following graphical representation, in that some of the attributes are
partially depend on primary key
In this example, Ename, Dept_name, and salary are fully functionally depend on Primary key of Emp_id. But Course_title
and date_completed are partial functional dependency. In this case, the partial functional dependency creates redundancy
in that relation
NORMALIZATION: Normalization is the process of decomposing relations to produce smaller, well-structured relation.
To produce smaller and well structured relations, the user needs to follow six normal forms
Steps in Normalization:
A normal form is state of relation that result from applying simple rules from regarding functional dependencies
relationships between attributes to that relation. The normal form are
1. First normal form
2. Second normal form
3. Third normal form
4. Boyce/codd normal form
5. Fourth normal form
6. Fifth normal form
1) First Normal Form: Any multi-valued attributes (also called repeating groups) have been removed,
2) Second Normal Form: Any partial functional dependencies have been removed.
3) Third Normal Form: Any transitive dependencies have been removed.
4) Boyce/Codd Normal Form: Any remaining anomalies that result from functional dependencies have been removed.
5) Fourth Normal Form: Any multi-valued dependencies have been removed.
6) Fifth Normal Form: Any remaining anomalies have been removed.
Removing the multi valued attributes and converting single valued using First NF
To avoid this, convert this into Second Normal Form. The 2NF will decompose the relation into two relations, shown in
graphical representation
In the above graphical representation
¬ the EMPLOYEE relation satisfies rule of 1 NF in Second Normal form and
¬ the COURSE relation satisfies rule of 2 NF by decomposing into two relation
THIRD NORMAL FORM(3NF): A relation that is in Second Normal form and has no transitive dependencies present.
Transitive dependency: A transitive is a functional dependency between two non-key attributes. For example, consider
the relation Sales with attributes cust_id, name, sales person and region that shown in graphical representation.
In this example, to insert, delete and update any row that facing Anomaly
a) Insertion Anomaly: A new salesperson is assigned to North Region without assign a customer to that salesperson. This
causes insertion Anomaly.
b) Deletion Anomaly: If a customer number say 1003 is deleted from the table, we lose the information of salesperson
who is assigned to that customer. This causes, Deletion Anomaly.
c) Modification Anomaly: If salesperson Smith is reassigned to the East region, several rows must be changed to reflect
that fact. This causes, update anomaly.
To avoid this Anomaly problem, the transitive dependency can be removed by decomposition of SALES into two
relations in 3NF.
Consider the following example, that removes Anomaly by decomposing into two relations
SalesPerson Region
Smith South
Kiran West
Babu Rao East
Smith South
Somu North
BOYCE/CODD NORMAL FORM(BCNF): A relation is in BCNF if it is in 3NF and every determinant is a candidate
key.
Boyce-Codd normal form removes the remaining anomalies in 3NF that are resulting from functional dependency, we can
get the result of relation in BCNF.
For example, STUDENT-ADVIDSOR IN 3NF
In the above relation the primary key in student-id and major-subject. Here the part of the primary key major-subject is
dependent upon a non-key attribute faculty–advisor. So, here the determinant the faculty-advisor. But it is not candidate
key.
Here in this example there are no partial dependencies and transitive dependencies. There is only functional
dependency between part of the primary key and non key attribute. Because of this dependency there is anomaly in this
relation. Suppose that in maths subject the advisor’ B’ is replaced by X. this change must be made in two or more rows in
this relation. This is an updation anomaly.
To convert a relation to BCNF the first step in the original relation is modified that the determinant (non key attributes)
becomes a component of the primary key of new relation. The attribute that is dependent on determinant becomes a non-
key attributes.
The second step in the conversion process is decompose the relation to eliminate the partial functional dependency.
This results in two relations. These relations are in 3NF and BCNF. since there is only one candidate key. That is
determinant.
In these two relations the student relation has a composite key, which contains attributes student-id and faculty-advisor.
Here faculty–advisor a foreign key which is referenced to the primary key of the advisor relation.
A relation is in BCNF that contain no multivalued dependency. In this case, 1 NF will repeated in this step. For example,
R be a relation schema, X and Y be attributes of R, and F be a set of dependencies that includes both FDs and MVDs. (i.e.
Functional Dependency and Multi-valued Dependencies). Then R is said to be in Fourth Normal Form (4NF) if for every
MVD X ->-> Y that holds over R, one of the following statements is true.
1) Y с X or XY = R, or 2) X is a super key
Example: Consider a relation schema ABCD and suppose that are FD A -> BCD and the MVD B -> -> C are given as
shown in Table
It shows three tuples from relation ABCD that satisfies the given MVD B -> -> C. From the definition of a MVD given
tuples t1 and t2, it follows that tuples t3 must also be included in the above relation. Now, consider tuples t2 and t3. From
the given FD A -> BCD and the fact that these tuples have the same A-value, we can compute
the c1 = c2. Therefore, we see that the FD B -> C must hold over ABCD whenever the FD A-> BCD and the MVD
B-> -> C holds. If B -> C holds, the relation is not in BCNF but the relation is in 4 NF.
The fourth normal from is useful because it overcomes the problems of the various approaches in which it represents the
multi-valued attributes in a single relation.
Fifth Normal Form (5 NF): Any remaining anomalies from 4 NF relation have been removed.
A relation schema R is said to be in Fifth Normal Form (5NF) if, for every join dependency
* (R1, . . . . Rn) that holds over R, one of the following statements is true.
* The JD is implied by the set of those FDs over R in which the left side is a key for R. It deals with a property loss less
joins
LOSSELESS-JOIN DECOMPOSITION:
Let R be a relation schema and let F be a set FDs (Functional Dependencies) over R. A decomposition of R into two schemas
with attribute X and Y is said to be lossless-join decomposition with respect to F, if for every instance r of R that satisfies
the dependencies in Fr.
In simple words, we can recover the original relation from the decomposed relations
In general, if we take projection of a relation and recombine them using natural join, we obtain some additional tuples that
were not in the original relation
The decomposition of relation schema r i.e. SPD into SP i.e. PROJECTING πsp (r ) and PD i.e., projecting πPD (r)
is therefore lossless decomposition as it gains back all original tuples of relation ‘r’ as well as with some additional tuples
that were not in original relation ‘r’
UNION Operation : UNION is used to combine the results of two or more SELECT statements.
However it will eliminate duplicate rows from its resultset. In case of union, number of columns
and datatype must be same in both the tables, on which UNION operation is being applied.
Example of UNION
The First table, The Second table,
ID Name ID Name
1 abhi 2 adam
2 adam 3 Chester
UNION ALL: This operation is similar to Union. But it also shows the duplicate rows.
1 abhi
2 adam
2 adam
3 Chester
INTERSECT:
Intersect operation is used to combine two SELECT statements, but it only retuns the records which
are common from both SELECT statements. In case of Intersect the number of columns and
datatype must be same.
NOTE: MySQL does not support INTERSECT operator.
Example of Intersect
The First table, The Second table,
ID NAME ID NAME
1 abhi 2 adam
2 adam 3 Chester
Intersect query will be,
SELECT * FROM First INTERSECT SELECT * FROM Second;The result set table will look like
ID NAME
2 adam
MINUS:
The Minus operation combines results of two SELECT statements and return only those in the final
result, which belongs to the first set of the result.
Example of Minus
NESTED QUERIES
A Subquery or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows −
Find the names of sailors who have reserved a red and a green boat.
SELECT S.sname
FROM Sailors S, Reserves R1, Boats B1, Reserves R2, Boats B2
WHERE S.sid = R1.sid AND R1.bid = B1.bid AND S.sid = R2.sid
AND R2.bid = B2.bid AND B1.color=‘red’ AND B2.color = ‘green’
Aggregate functions :
Aggregate functions in DBMS take multiple rows from the table and return a value according to
the query.
All the aggregate functions are used in Select statement.
Syntax :
SELECT <FUNCTION NAME> (<PARAMETER>) FROM <TABLE NAME>;
AVG Function
This function returns the average value of the numeric column that is supplied as a parameter.
Example: Write a query to select average salary from employee table.
Select AVG(salary) from Employee;
COUNT Function
The count function returns the number of rows in the result. It does not count the null values.
Example: Write a query to return number of rows where salary > 20000.
Select COUNT(*) from Employee where Salary > 20000;
Types −
COUNT(*): Counts all the number of rows of the table including null.
COUNT( COLUMN_NAME): count number of non-null values in column.
COUNT( DISTINCT COLUMN_NAME): count number of distinct values in a column.
MAX Function
The MAX function is used to find maximum value in the column that is supplied as a parameter. It
can be used on any type of data.
Example − Write a query to find the maximum salary in employee table.
Select MAX(salary) from Employee
SUM Function
This function sums up the values in the column supplied as a parameter.
Example: Write a query to get the total salary of employees.
Select SUM(salary) from Employee
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block. The
GROUP BY clause must follow the conditions in the WHERE clause and must precede
the ORDER BY clause if one is used.
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2
The HAVING clause was added to SQL because the WHERE keyword cannot be used
with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
NULL VALUES: A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update a record without adding a
value to this field. Then, the field will be saved with a NULL value.
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
To enforce the constraints that interlake boat cannot be reserved we could use,
Create TABLE Reserves (sid INTEGER, bid INTEGER, day DATE, FOREIGN KEY (sid) REFERENCES
Sailors FOREIGN KEY (bid) REFERENCES Boats, CONSTRAINT noInterLakeRes CHECK (Interlake <>
(SELECT B.bname FROM Boats B WHERE B.bid = Reserves.bid)));
Domain Constraints
CREATE DOMAIN ratingval INTEGER DEFAULT 0 CHECK (VALUE >= 1 AND VALUE <=10)
Triggers:
A trigger is a stored procedure in database which automatically invokes
whenever a special event in the database occurs. For example, a trigger can
be invoked when a row is inserted into a specified table or when certain
table columns are being updated.
You can write triggers that fire whenever one of the following operations occurs:
– DML statements (INSERT, UPDATE, DELETE) on a particular table or view, issued by any
user
– DDL statements (CREATE or ALTER primarily) issued either by a particular schema/user
or by any schema/user in the database
– Database events, such as logon/logoff, errors, or startup/shutdown, also issued either
by a particular schema/user or by any schema/user in the database
– You can also use triggers to:
– Automatically generate derived column values
– Prevent invalid transactions
– Enforce complex security authorizations
– Enforce referential integrity across nodes in a distributed database
– Enforce complex business rules
– Provide transparent event logging
– Provide auditing
– Maintain synchronous table replicates
– Gather statistics on table access
– Modify table data when DML statements are issued against views
– Publish information about database events, user events, and SQL statements to
subscribing applications
Parts of a Trigger :
A trigger has three basic parts:
A triggering event or statement
A trigger restriction
A trigger action
Syntax:
create trigger [trigger_name]
[before | after]
{insert | update | delete} on [table_name]
[for each row]
[trigger_body]
Remove Trigger:
Active Databases
Active Database is a database consisting of set of triggers. These databases
are very difficult to be maintained because of the complexity that arises in
understanding the effect of these triggers. In such database, DBMS initially
verifies whether the particular trigger specified in the statement that modifies
the database) is activated or not, prior to executing the statement.
If the trigger is active then DBMS executes the condition part and then executes
the action part only if the specified condition is evaluated to true. It is possible to
activate more than one trigger within a single statement.
Features of Active Database:
1. It possess all the concepts of a conventional database i.e. data modelling
facilities, query language etc.
2. It supports all the functions of a traditional database like data definition, data
manipulation, storage management etc.
3. It supports definition and management of ECA rules.
4. It detects event occurrence.
5. It must be able to evaluate conditions and to execute actions.
6. It means that it has to implement rule execution.
Advantages :
1. Enhances traditional database functionalities with powerful rule processing
capabilities.
2. Enable a uniform and centralized description of the business rules relevant
to the information system.
3. Avoids redundancy of checking and repair operations.
4. Suitable platform for building large and efficient knowledge base and expert
systems.
SCHEMA REFINEMENT :
REDUNDANCY :
Redundancy takes place when there are more than one or multiple copies of the same relation in a
database. Simply the storage of same or similar value more than once in the field is referred to
as Redundancy.
Redundancy means having multiple copies of same data in the database. This problem arises
when a database is not normalized
Suppose a table of student details attributes are: student Id, student name, college name, college
rank, course opted.
As it can be observed that values of attribute college name, college rank, course is being repeated
which can lead to problems. Problems caused due to redundancy are: Insertion anomaly, Deletion
anomaly, and Updation anomaly.
1. Insertion Anomaly –
If a student detail has to be inserted whose course is not being decided yet then insertion will not
be possible till the time course is decided for student.
This problem happens when the insertion of a data record is not possible without adding some
additional unrelated data to the record.
2. Deletion Anomaly –
If the details of students in this table is deleted then the details of college will also get deleted
which should not occur by common sense.
This anomaly happens when deletion of a data record results in losing some unrelated
information that was stored as part of the record that was deleted from a table.
3. Updation Anomaly –
Suppose if the rank of the college changes then changes will have to be all over the database
which will be time-consuming and computationally costly.
If updation do not occur at all places then database will be in inconsistent state.
Normalization :
Normalization is the process of minimizing redundancy from a relation or set of
relations. Redundancy in relation may cause insertion, deletion, and update anomalies.
So, it helps to minimize the redundancy in relations. Normal forms are used to
eliminate or reduce redundancy in database tables.
Normal Description
Form
2NF A relation will be in 2NF if it is in 1NF and all non-key attributes are fully functional dependent
on the primary key.
4NF A relation will be in 4NF if it is in Boyce Codd's normal form and has no multi-valued
dependency.
5NF A relation is in 5NF. If it is in 4NF and does not contain any join dependency, joining should
be lossless.
Normalization
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.
Normalization is a method to remove all these anomalies and bring the database to a consistent
state.
Each attribute must contain only a single value from its pre-defined domain.
We broke the relation in two as depicted in the above picture. So there exists no partial dependency.
o A is prime attribute.
We find that in the above Student_detail relation, Stu_ID is the key and only prime key attribute.
We find that City can be identified by Stu_ID as well as Zip itself. Neither Zip is a superkey nor
is City a prime attribute. Additionally, Stu_ID → Zip → City, so there exists transitive
dependency.
To bring this relation into third normal form, we break the relation into two relations as follows −
o A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued dependency.
o For a dependency A → B, if for a single value of A, multiple values of B exists, then the relation
will be a multi-valued dependency.
Example
STUDENT
21 Computer Dancing
21 Math Singing
34 Chemistry Dancing
74 Biology Cricket
59 Physics Hockey
The given STUDENT table is in 3NF, but the COURSE and HOBBY are two independent entity.
Hence, there is no relationship between COURSE and HOBBY.
STUDENT_COURSE
STU_ID COURSE
21 Computer
21 Math
34 Chemistry
74 Biology
59 Physics
STUDENT_HOBBY
STU_ID HOBBY
21 Dancing
21 Singing
34 Dancing
74 Cricket
59 Hockey
A1 PQR Nut
A1 PQR Bolt
A1 XYZ Nut
A1 XYZ Bolt
Agent Company Product
A2 PQR Nut
The relation ACP is again decompose into 3 relations. Now, the natural Join of all the
three relations will be shown as:
Table – R1
Agent Company
A1 PQR
A1 XYZ
A2 PQR
Table – R2
Agent Product
A1 Nut
A1 Bolt
A2 Nut
Table – R3
Company Product
PQR Nut
PQR Bolt
XYZ Nut
XYZ Bolt
Advantages of Normalization
o Normalization helps to minimize data redundancy.
o Greater overall database organization.
o Data consistency within the database.
o Much more flexible database design.
o Enforces the concept of relational integrity.
Disadvantages of Normalization
o You cannot start building the database before knowing what the user needs.
o The performance degrades when normalizing the relations to higher normal forms,
i.e., 4NF, 5NF.
o It is very time-consuming and difficult to normalize relations of a higher degree.
o Careless decomposition may lead to a bad database design, leading to serious
problems.
DECOMPOSITION :
Decomposition is to break a relation into multiple relations to bring it into an
appropriate normal form. It helps to remove redundancy, inconsistencies,
and anomalies from a database. The decomposition of a relation R in a relational schema
is the process of replacing the original relation R with two or more relations in a
relational schema.
Issues of decomposition:
There are many problems regarding the decomposition in DBMS mentioned below:
Redundant Storage
Many instances where the same information gets stored in a single place can confuse the
programmers. It will take lots of space in the system.
Insertion Anomalies
It isn’t essential for storing important details unless some kind of information is stored in a consistent
manner.
Deletion Anomalies
It isn’t possible to delete some details without eliminating any sort of information.
Lossless Decomposition
Decomposition is lossless if it is feasible to reconstruct relation R from decomposed tables using
Joins. This is the preferred choice. The information will not lose from the relation when
decomposed. The join would result in the same original relation.
A lossless Join decomposition ensures two things:
No information is lost while decomposing from the original relation.
If we join back the sub decomposed relations, the same relation that was
decomposed is obtained.
We can follow certain rules to ensure that the decomposition is a lossless join
decomposition Let’s say we have a relation R and we decomposed it into R1 and R2, then
the rules are:
1. The union of attributes of both the sub relations R1 and R2 must contain all the
attributes of original relation R.
R1 ∪ R2 = R
2. The intersection of attributes of both the sub relations R1 and R2 must not be null,
i.e., there should be some attributes that are present in both R1 and R2.
R1 ∩ R2 ≠ ∅
3. The intersection of attributes of both the sub relations R1 and R2 must be the
superkey of R1 or R2, or both R1 and R2.
R1 ∩ R2 = Super key of R1 or R2
Let us see an example −
<EmpInfo>
Emp_ID Emp_Name Emp_Age Emp_Location Dept_ID Dept_Name
E001 Jacob 29 Alabama Dpt1 Operations
<DeptDetails>
Dept_ID Emp_ID Dept_Name
Dpt1 E001 Operations
Dpt2 E002 HR
Dpt3 E003 Finance
Therefore, the above relation had lossless decomposition i.e. no loss of information.
Decomposition is the process of breaking an original relation into multiple sub relations.
Decomposition helps to remove anomalies, redundancy, and other problems in a DBMS.
Decomposition can be lossy or lossless.
An ideal decomposition should be lossless join decomposition and dependency
preserving.