Unit III Correct
Unit III Correct
UNIT III
Integrity and Security: Domain Constraints – Referential Integrity – Assertions –Triggers – Security and
Authorization – Authorization in SQL .Relational-Database Design: Normalization -First Normal Form,
Second Normal Form, Third Normal Form, Boyce-Codd Normal Form.
Integrity constraints provide a means of ensuring that changes made to the database by
authorized users do not result in a loss of data consistency. Thus, integrity constraints guard
against accidental damage to the data base.
Key declarations: stipulation (condition) that certain attributes form a candidate key for a
given entity set. The set of legal insertions and updates is constrained to those that do not
create two entities with the same value on a candidate key.
Form of a relationship: many- to- many, one- to- many, one to one. A one-to-one or one –
to-many relationship restricts the set of legal relationships among entities of a collection of
entity sets.
Domain Constraints
Referential Integrity
Assertions
Triggers
Security
Authorization
Authorization in SQL
Domain Constraints:
Domain of possible values must be associated with every attribute.
Domains could be specified as below:
Note that NAME1 and NAME2 are both character strings of length 10 but they now
belong to different (semantic) domains.
It is important to denote different domains too
Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1
UNIT –III DBMS
1.Domain Constraints
Integrity constraints guard against accidental damage to the database, by ensuring that
authorized changes to the database do not result in a loss of data consistency.
Domain constraints are the most elementary form of integrity constraint.
They test values inserted in the database, and test queries to ensure that the
comparisons make sense.
Number of standard domain types, such as integer types, character types, and
date/time types defined in SQL.
Domain constraints are the most elementary form of integrity constraint. They are
tested easily by the system whenever a new data item is entered into the database.
“Find all customers who have the same name as a branch” to be a meaningful query. Thus, if
we view the database at the conceptual, rather than the physical, level, customer-name and
branch-name should have distinct domains.
The create domain clause can be used to define new domains. For example, the statements:
Values of one domain can be cast (that is, converted) to another domain. If the attribute A or
relation r is of type Dollars, we can convert it to Pounds by writing
Use check clause to ensure that an hourly-wage domain allows only
values greater than a specified value.
o The domain has a constraint that ensures that the hourly-wage is greater than 4.00
The domain HourlyWage has a constraint that ensures that the hourly wage is greater than
4.00. The clause constraint wage-value-test is optional, and is used to give the name wage-
value-test to the constraint.
The check clause can also be used to restrict a domain to not contain any null values:
As another example, the domain can be restricted to contain only a specified set of values by
using the in clause:
The preceding check conditions can be tested quite easily, when a tuple is inserted or
modified. However, in general, the check conditions can be more complex (and harder to
check), since subqueries that refer to other relations are permitted in the check condition. For
example, this constraint could be specified on the relation deposit:
The check condition verifies that the branch-name in each tuple in the deposit relation is
actually the name of a branch in the branch relation. Thus, the condition has to be checked
not only when a tuple is inserted or modified in deposit, but also when the relation branch
changes (in this case, when a tuple is deleted or modified in relation branch). The preceding
constraint is actually an example of a class of constraints called referential-integrity
constraints.
2.Referential Integrity
A referential integrity constraint requires that a foreign key in one relation refers to an
actual, existing tuple in another relation.
i.e. the value that appears in one relation for a given set(‘s’) of attributes also appears
for certain set of attributes in another relation for (‘r’).
Primary and candidate keys and foreign keys can be specified as part of the
SQL create table statement
The primary key clause lists of attributes that comprise the primary key.
The unique key clause lists of attributes that comprise a candidate key.
The foreign key clause lists the attributes that comprise the foreign key and the name
of the relation referenced by the foreign key. By default, a foreign key references the primary
key attributes of the referenced table.
Example :
Consider relationship set R between entity sets E1 and E2. The relational schema for
R includes the primary keys K1 of E1 and K2 of E2.
Then K1 and K2 form foreign keys on the relational schemas for E1 and E2
respectively.
Example:
account-numberchar(10),
The following tests must be made in order to preserve the following referential
integrity constraint:
(r2) K (r1)
Insert. If a tuple t2 is inserted into r2, the system must ensure that there is a tuple t1
in r1 such that t1[K] = t2[]. That is
t2 [] K (r1)
Delete. If a tuple, t1 is deleted from r1, the system must compute the set of tuples in
r2 that reference t1:
= t1[K] (r2)
If a tuple t1 is updated in r1, and the update modifies values for the primary key (K), then a
test similar to the delete case is made:
Primary and candidate keys and foreign keys can be specified as part of the SQL create table
statement:
The primary key clause of the create table statement includes a list of the attributes that
constitute the primary key.
The unique clause of the create table statement includes a list of the attributes that constitute
a candidate key.
The foreign key clause of the create table statement includes both a list of the attributes that
constitute the foreign key and the name of the relation referenced by the foreign key.
Primary and foreign key declarations using the partial SQL DDL Definition of our bank
database shown in fig:6.2
We can use the following short form as part of an attribute definition to declare that the
attribute forms a foreign key:
However, a foreign key clause can specify that if a delete or update action on the referenced
relation violates the constraint, then, instead of rejecting the action, the system must take
steps to change the tuple in the referencing relation to restore the constraint. Consider this
definition of an integrity constraint on the relation account:
Because of the clause on delete cascade associated with the foreign-key declaration,if a
delete of a tuple in branch results in this referential-integrity constraint being violated,the
system does not reject the delete. Instead, the delete “cascades” to the account relation,
deleting the tuple that refers to the branch that was deleted.
SQL also allows the foreign keyclause to specify actions other than cascade, if the constraint
is violated: The referencing field (here, branch-name) can be set to null (by using set null in
place of cascade),or to the default value for the domain (by using set default).
3.Assertions
When an assertion is made, the system tests it for validity, and tests it again on every
update that may violate the assertion
o This testing may introduce a significant amount of overhead; hence assertions
should be used with great care.
Asserting
for all X, P(X)
is achieved in a round-about fashion using
not exists X such that not P(X)
Example
When an assertion is created, the system tests it for validity. If the assertion is valid,then any
future modification to the database is allowed only if it does not cause that assertion to be
violated.
4.Triggers
Trigger Example
Suppose that instead of allowing negative account balances, the bank deals with
overdrafts by
o setting the account balance to zero
o creating a loan in the amount of the overdraft
o giving this loan a loan number identical to the account number of the
overdrawn account
The condition for executing the trigger is an update to the account relation that results
in a negative balance value.
4.1Need of Triggers
Triggers are useful mechanisms for alerting humans or for starting certain tasks
automatically when certain conditions are met. As an illustration, suppose that, instead of
allowing negative account balances, the bank deals with overdrafts by setting the account
balance to zero, and creating a loan in the amount of the overdraft. The bank gives this loan a
loan number identical to the account number of the overdrawn account.
For this example, the condition for executing the trigger is an update to the account relation
that results in a negative balance value. Suppose that Jones’ withdrawal of some money from
an account made the account balance negative. Let t denote the account tuple with a negative
balance value. The actions to be taken are:
Set t[balance] to 0.
4.2.Triggers in SQL
SQL-based database systems use triggers widely, although before SQL:1999 they were not
part of the SQL standard. Unfortunately, each database system implemented its
Centralized actions can be defined using a non declarative approach (writing PL/SQL
code) with database triggers.
A database trigger is a stored procedure that is fired (implicitly executed) when an
INSERT, UPDATE, or DELETE statement is issued against the associated table.
Database triggers can be used to customize a database management system:
value-based auditing
automated data generation
the enforcement of complex security checks
enforce integrity rules
enforce complex business rules
For updates, the trigger can specify columns whose update causes the trigger to execute. For
instance if the first line of the overdraft trigger were replaced by
then the trigger would be executed only on updates to balance; updates to other attributes
would not cause it to be executed.
Example:1
suppose the value in a phone number field of an inserted tuple is blank, which indicates
absence of a phone number. We can define a trigger that replaces the value by the null value.
The set statement can be used to carry out such modifications.
Example:2
Returning to our warehouse inventory example, suppose we have the following relations:
• inventory(item, level), which notes the current amount (number/weight/volume) of the item
in the warehouse
minlevel(item, level), which notes the minimum amount of the item to be maintained
• reorder(item, amount), which notes the amount of the item to be ordered when
its level falls below the minimum
Example:
5.Security
The data stored in the database need to be protected from unauthorized access, malicious
destruction or alteration, and accidental introduction of consistency.
Security Violations
Misuse of the database can be categorized as being either intentional(malicious) or
accidental. Accidental loss of data consistency may result from
Database security refers to protection from malicious access. Absolute protection of the
database from malicious abuse is not possible, but the cost to the perpetrator can be made
high enough to deter most if not all attempts to access the database without proper authority.
Security within the operating system is implemented at several levels, ranging from
passwords for access to the system to the isolation of concurrent processes running within the
system. The file system also provides some degree of protection.
Finally, network-level security has gained widespread recognition as the Internet has evolved
from an academic research platform to the basis of international electronic commerce.
6.Authorization
The drop and delete authorization differ in that delete authorization allows deletion of tuples
only. If a user deletes all tuples of a relation, the relation still exists, but it is empty. If a
relation is dropped, it no longer exists.
Resource authorization. A user with resource authorization who creates a new relation is
given all privileges on that relation automatically.
Index authorization may appear unnecessary, since the creation or deletion of an index does
not alter data in relations. Rather, indices are a structure for performance
enhancements.However, indices also consume space, and all database modifications are
required to update indices. If index authorization were granted to all users, those who
performed updates would be tempted to delete indices, whereas those who issued queries
would be tempted to create numerous indices. To allow the database administrator to
regulate the use of system resources, it is necessary to treat index creation as a privilege.
6.1 Authorization and Views
Users can be given authorization on views, without being given any authorization on
the relations used in the view definition
Ability of views to hide data serves both to simplify usage of the system and to
enhance security by allowing users access only to data they need for their job
A combination or relational-level security and view-level security can be used to
limit a user’s access to precisely the data that user needs.
View Example
Suppose a bank clerk needs to know the names of the customers of each branch, but
is not authorized to see specific loan information.
Approach: Deny direct access to the loan relation, but grant access to the view cust-loan,
which consists only of the names of customers and the branches at which they have a loan.
Clearly, the clerk is authorized to see the result of this query. However, when the query
processor translates it into a query on the actual relations in the database, it produces a query
on borrower and loan. Thus, the system must check authorization on the clerk’s query before
it begins query processing
6.2Authorization on Views
A user who creates a view does not necessarily receive all privileges on that view. User
receives only those privileges that provide no additional authorization beyond those that user
already had.
For example, a user cannot be given update authorization on a view without having update
authorization on the relations used to define the view. If a user creates a view on which no
authorization can be granted, the system will deny the view creation request. In our cust-loan
view example, the creator of the view must have read authorization on both the borrower and
loan relations.
6.3.Granting of Privileges
In the sample graph in Figure 6.6, observe that user U5 is granted authorization by both U1
and U2; U4 is granted authorization by only U1.A user has an authorization if and only if
there is a path from the root of the authorizationgraph (namely, the node representing the
database administrator) down to
the node representing the user.
Suppose that the database administrator decides to revoke the authorization of user U1. Since
U4 has authorization from U1, that authorization should be revoked as well. However, U5
was granted authorization by both U1 and U2. Since the database administrator did not
revoke update authorization on loan from U2, U5 retains update authorization on loan. If U2
eventually revokes authorization from U5, then U5 loses the authorization.
A pair of devious users might attempt to defeat the rules for revocation of authorization by
granting authorization to each other, as shown in Figure 6.7a. If the database administrator
revokes authorization from U2, U2 retains authorization through U3, as in Figure 6.7b. If
authorization is revoked subsequently from U3, U3 appears to retain authorization through
U2, as in Figure 6.7c. However, when the database administrator revokes authorization from
U3, the edges fromU3 to U2 and
from U2 to U3 are no longer part of a path starting with the database administrator.
7. Authorization in SQL
The SQL standard includes the privileges delete, insert, select, and update.
The select privilege corresponds to the read privilege.
SQL also includes a references privilege that permits a user/role to declare foreign
keys the user/role must have been granted references privilege on those attributes.
The SQL data-definition language includes commands to grant and revoke privileges. The
grant statement is used to confer authorization. The basic form of this statement is:
The privilege list allows the granting of several privileges in one command. The following
grant statement grants users U1, U2, and U3 select authorization on the account relation:
The update authorization may be given either on all attributes of the relation or on only
some. If update authorization is included in a grant statement, the list of attributes on which
update authorization is to be granted optionally appears in parentheses immediately after the
update keyword.
If the list of attributes is omitted, the update privilege will be granted on all attributes of the
relation.
This grant statement gives users U1, U2, andU3 update authorization on the amount attribute
of the loan relation:
The SQL references privilege is granted on specific attributes in a manner like that for the
update privilege. The following grant statement allows user U1 to create relations that
reference the key branch-name of the branch relation as a foreign key:
The privilege all privileges can be used as a short form for all the allowable
privileges.Similarly, the user name public refers to all current and future users of the system.
SQL also includes a usage privilege that authorizes a user to use a specified domain
Roles
Roles can then be granted privileges just as the users can, as illustrated in this statement:
By default, a user/role that is granted a privilege is not authorized to grant that privilege to
another user/role. we append the with grant option clause to theappropriate grant command.
For example, if we wish to allow U1 the select privilegeon branch and allow U1 to grant this
privilege to others, we write
In this case, the system returns an error if there are any cascading revokes, and does
not carry out the revoke action. The following revoke statement revokes only the
grant option, rather than the actual select privilege:
8.Encryption
Data may be encrypted when database authorization provisions do not offer sufficient
protection.
Properties of good encryption technique:
Relatively simple for authorized users to encrypt and decrypt data.
Encryption scheme depends not on the secrecy of the algorithm but on the
secrecy of a parameter of the algorithm called the encryption key.
Extremely difficult for an intruder to determine the encryption key.
Data Encryption Standard (DES) substitutes characters and rearranges their order on
the basis of an encryption key which is provided to authorized users via a secure mechanism.
Scheme is no more secure than the key transmission mechanism since the key has to be
shared.
Advanced Encryption Standard (AES) is a new standard replacing DES, and is based
on the Rijndael algorithm, but is also dependent on shared secret keys
Public-key encryption is based on each user having two keys:
o public key – publicly published key used to encrypt data, but cannot be used to
decrypt data
o private key -- key known only to individual user, and used to decrypt data.
Need not be transmitted to the site doing encryption.
Encryption scheme is such that it is impossible or extremely hard to decrypt data given
only the public key.
The RSA public-key encryption scheme is based on the hardness of factoring a very
large number (100's of digits) into its prime components.
9.Authentication
Normalization is a technique for producing set of relation with desirable properties given the
data requirements of an enterprise. It is the processes of removing redundant data from are
tables to improve storage efficiency data integrity and scalability. It generally involves
splitting the existing tables into multiple ones which must be rejoined or linked each time a
query is used.
The undesirable properties that a bad relational database design may have are:
Repetition of information
Inability to represent certain information
Loss of information
Purpose of normalization
To avoid redundancy by storing each fact within the database only once
To put a data into a form that confirm to relational principles
To put data into form that is more able to accurately accommodate change.
To avoid updating anomalies
To facilitate the enforcement of data constraints.
Benefits:
Redundancy:
Repeating groups
Anomalies
Insert
Update
Delete
Functional dependency
Example: A->B
Compound determinant
It use all the attribute of the composite determinant to identify its object uniquely
It only use a subset of attributes of the composite determinant to identify its object uniquely
Transitive dependency
Example: A->B->c
A->C
A property of decomposition which ensures that no spurious tuples are generated when
relations are reunited to a natural join dependency or operator.
Types of Normalization
The relation is said to be first normal form(1NF) if and only if each attribute of the relation is
atomic more simplify to be in 1nf each column must contain only a single value and each row
must contain the same columns.
In the above table this skill field has multi value attributes and unnormalized form.a
relation table must not have repeating groups.
So,it can be converted into first normal form as shown as below
Benefits of 1NF:
o Easy to query or sort the data
o More scable
o Each row can be identified for updating.
A relation is said to be second normal form if and only if every non key field depend on the
entire primary key, not on part of a composite key. If a database has only single field primary
keys, it is automatically in second normal form.
EMPNO SKILLS
1 C
1 perl
1 java
2 LINUX
2 MAC
3 oracle
3 java
3 DB2
FUNCTIONAL DEPENDENCIES
The property of one or more attribute that uniquely determined the value of other attribute.
Functional dependencies is a relationship between or among attribute such that the value of
one attribute depend on or determined by the values of other attributes.
Features of 2NF
-> Remove columns that are not depend up the primary key.
-> Remove columns that are not fully depend up on the primary key.
->BCNF require that all non- trivial dependencies be of the form α->β, where, where α is a
super key. 3NF relaxes this constrain by allowing non-trivial functional dependencies whose
left side is not a super key.
-> A relational schema R is in 3NF with respect to set F of functional dependencies, if , for
all functional dependencies in F+ of the form α->β where αCR and βCR, at least one of the
following holds:
I:=0
Then begin
I:=i+1;
Ri:=αβ;
And
Then begin
I:=i+1;
End
Return(R1, R2,……Ri)
The relation R is said to be 3NF, if and only if it is a second normal form and no
transitive dependency exist between non-key attribute and key attribute.
Transitive functional dependency arises only when non-key attribute.
A relation is said to be transitively dependent when
(i) Z is transitively dependent on X.
(ii) When X determines Y and Y determines Z.
(iii) Thus Z is indirectly dependent X through Y.
EG; Employee
EMPLOYEE
PROJECT NO NAME
P1 Rose
P2 Smith
P3 Rose
P4 Rose
Employee name
NAME ADDRESS
Rose 86,Nehru st.
Smith 42, Nehru st.
Here the name attribute is used to link between two tables and act as the
foreign key.
A relation schema R is in BCNF with respect to a set Fof functional dependencies if for
all functional dependencies in f+ of the form α->β where α subset of R and ,β _c R atleast
one of the following holds:
Example:
Suppose each student may maior in several areas .each student has one tutor for each
area.each area has several tutors but tutor advices in only one area each tutor advices
several students in an area.
The schema is in third normal form because there are no partial dependencies and no
interdata dependency and hence anamolies will raise.
Suppose the data 12MC01 changes one of the major form computer science to
information system.By doing this changes, we loss information about staff number 234
tutor on CSE.this is an anamolies.
To insert a new row to establish the fact, staff number 789 an computer
science.We cannot do this until atleast one student take this area as the major. This is an
insertion anamoly.
These anamoly occur bscause there are two overlapping candidate key
occur.So BCNF indentify this problem and purpose to solution.
Every table has only one candidate key. The above relation can be
represented in BCNF by splitting the table into two relations called student number.,staff
In one relation, here staff number is refered as second relation with area which is as
below.
To improve the database design, by decomposing this schema into fourth normal form
decomposition.
A relation schema R is in fourth normal form(4NF) with respect to a set D of functional and
multivalve dependencies if ,for all multivalve dependencies in D+ of the form A->-->B.
Hold:
A database design is in 4NF if each member of the set of relation schemas that constitutes
the design is in 4 NF.
The table set to be 4NF if and only if it’s in BCNF and multivalve dependency or functional
dependency.
Example:
In the above table primary key identifier is car, color, and engine. None of the attributes of
dependencies of other attributes there is any possible dependencies or decomposition.
Each color for each car is paired with each engine, size for each car and this continuous. With
every other car, in the restated car this car reduces in the relational.
If a row is deleted from the table car,”c1” comes for the row of the table this type of reduces
by multivalve decomposition. Suppose there is a relation R of set of attribute S that contain
multivalve dependencies X ->> Y.
Two tables:-
Car color:
Car Color
C1 Red
C1 White
C1 Blue
C2 Red
C2 Green
Car Engine:
Car Engine
C1 F3.2L
C1 F4.5L
C2 C3.2L
C2 C4.5L
When this table structure we can determine the 4NF. For example, Red, C1 car can have
either “F3.2L”, “F4.5L”. However to delete one piece of (row) information do not actually
lose the information as oppressed to when a row is eliminate from the original relation.