Professional Documents
Culture Documents
Badgujar Dipak D
Integrity Constraints
• Integrity constraints are a set of rules. It is used to maintain the quality of
information.
• Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is not
affected.
• Thus, integrity constraint is used to guard against accidental damage to the
database.
Integrity Constraints
• Types of Integrity Constraints:
Domain Constraints
• Domain constraints can be defined as the definition of a valid set of values
for an attribute.
• The data type of domain includes string, character, integer, time, date,
currency, etc. The value of the attribute must be available in the
corresponding domain.
Entity Integrity Constraints
• The entity integrity constraint states that primary key value can't be null.
• This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
• A table can contain a null value other than the primary key field.
Referential Integrity Constraints
• A referential integrity constraint is specified between two tables.
• In the Referential integrity constraints, if a foreign key in Table 1 refers to
the Primary Key of Table 2, then every value of the Foreign Key in Table 1
must be null or be available in Table 2.
Key Constraints
• Keys are the entity set that is used to identify an entity within its entity set
uniquely.
• An entity set can have multiple keys, but out of which one key will be the
primary key. A primary key can contain a unique and not null value in the
relational table.
Keys in DBMS
• Super Key – A super key is a group of single or multiple keys which identifies
rows in a table.
EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
• In this example, OrderNo and ProductID can’t be a primary key as it does not uniquely identify
a record. However, a compound key of Order ID and Product ID could be used as it uniquely
identified each record.
Keys in DBMS
• Composite Key- is a combination of two or more columns that uniquely
identify rows in a table. The combination of columns guarantees uniqueness,
though individually uniqueness is not guaranteed. Hence, they are combined
to uniquely identify records in a table.
• The difference between compound and the composite key is that any part of
the compound key can be a foreign key, but the composite key may or maybe
not a part of the foreign key.
Keys in DBMS
• Surrogate Keys is An artificial key which aims to uniquely identify each record
is called a surrogate key. This kind of partial key in dbms is unique because it is
created when you don’t have any natural primary key. They do not lend any
meaning to the data in the table. Surrogate key in DBMS is usually an integer.
A surrogate key is a value generated right before the record is inserted into a
table.
Fname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00
• Above, given example, shown shift timings of the different employee. In this
example, a surrogate key is needed to uniquely identify each employee.
Keys in DBMS
Primary Key Foreign Key
Helps you to uniquely identify a record It is a field in the table that is the
in the table. primary key of another table.
A foreign key may accept multiple null
Primary Key never accept null values.
values.
A foreign key cannot automatically
Primary key is a clustered index and
create an index, clustered or
data in the DBMS table are physically
non-clustered. However, you can
organized in the sequence of the
manually create an index on the
clustered index.
foreign key.
You can have the single Primary key in You can have multiple foreign keys in a
a table. table.
Anomalies in DBMS
• There are three types of anomalies that occur when the database is not
normalized. These are
• Insertion
• Updation
• Deletion
Emp_Id Emp_Name Emp_Address Emp_Dept
101 Dipak Jalgaon D101
101 Dipak Jalgaon D102
123 Aniket Pune D105
166 Shreya Nasik D108
166 Shreya Nasik D109
Anomalies in DBMS
• Update Anomalies: In the table we have two rows for employee Dipak who
works for 2 departments of company. If we update the address of Dipak then
we need to update it in two rows if not Done then data will be inconsistent.
Emp_Id Emp_Name Emp_Address Emp_Dept
101 Dipak Jalgaon D101
101 Dipak Jalgaon D102
123 Aniket Pune D105
166 Shreya Nasik D108
166 Shreya Nasik D109
• Insert Anomalies: If the new employee has joined as trainee if he has not
allocated the dept. We can’t insert his information if emp_dept is not
accepting null values.
• Delete Anomalies: If company want to close the operation of Dept D105 then
we will lost the information of employee 123 as he is the single employee
working in that dept.
Anomalies in DBMS
• To overcome these anomalies we need to normalize the data through
Normalization Process.
Functional Dependency
• The functional dependency is a relationship that exists between two attributes. It
typically exists between the primary key and non-key attribute within a table.
• Functional dependency helps you to maintain the quality of data in the database.
• A functional dependency is denoted by an arrow →.
• The functional dependency of X on Y is represented by X → Y.
• Functional Dependency plays a vital role to find the difference between good and
bad database design.
• One of the attributes is called the determinant and the other attribute is called the
determined. For each value of the determinant there is associated one and only
one value of the determined.
• If A is the determinant and B is the determined then we say that A functionally
determines B and graphically represent this as A -> B
Types of Functional Dependency
• Trivial functional dependency
• A → B has trivial functional dependency if B is a subset of A.
• The following dependencies are also trivial like: A → A, B → B
• Example
• Consider a table with two columns Employee_Id and Employee_Name.
• {Employee_id, Employee_Name} → Employee_Id is a trivial functional dependency as
• Employee_Id is a subset of {Employee_Id, Employee_Name}.
• Also, Employee_Id → Employee_Id and Employee_Name → Employee_Name are trivial
dependencies
• Non-trivial functional dependency
• A → B has a non-trivial functional dependency if B is not a subset of A.
• When A intersection B is NULL, then A → B is called as complete non-trivial.
• Example
• ID → Name,
• Name → DOB
Types of Functional Dependency
• Multivalued dependency
• Multivalued dependency occurs in the situation where there are multiple independent
multivalued attributes in a single table. A multivalued dependency is a complete constraint
between two sets of attributes in a relation. It requires that certain tuples be present in a
relation.
Car_Model Manufacture_Year Color
H1001 2017 Metallic
H1001 2017 Grey
H1005 2018 Blue
H1005 2018 Red
H166 2015 Silver
• In this example, maf_year and color are independent of each other but dependent on
car_model. In this example, these two columns are said to be multivalue dependent on
car_model.
• Functional Dependency is Represented as
• car_model -> maf_year and
• car_model-> colour
Types of Functional Dependency
• Transitive Dependency:
• A functional dependency is said to be transitive if it is indirectly formed by two functional
dependencies.
• X -> Z is a transitive dependency if the following three functional dependencies hold true:
• X->Y
• Y does not ->X Book Author Author_age
• Y->Z
Game of Thrones George R. R. Martin 66
Harry Potter J. K. Rowling 49
Dying of the Light George R. R. Martin 66
• A transitive dependency can only occur in a relation of three of more attributes. This
dependency helps us normalizing the database in 3NF (3rd Normal Form).
• {Book} ->{Author} (if we know the book, we knows the author name)
• {Author} does not ->{Book}
• {Author} -> {Author_age}
• Therefore as per the rule of transitive dependency: {Book} -> {Author_age} should hold, that
makes sense because if we know the book name we can know the author’s age.
Types of Functional Dependency
• Fully Functional Dependency:
• If X and Y are an attribute set of a relation , Y is fully functional dependent on X, if Y is
functionally dependent on X but not on any proper subset of X.
• Example –
In the relation ABC->D , attribute D is fully functional dependent on ABC if it is fully functional
dependent on ABC and not on any proper subset of ABC. That means that subsets of ABC like AB ,BC
,A,B ,etc cannot determine D .
supplier_id item_id price
From the table, we can clearly see that neither
1 1 540
supplier_id nor item_id can uniquely determine
price but both supplier_id and item_id together can 2 1 545
do so. So we can say that price is fully functionally 1 2 200
dependent on { supplier_id , item_id } .
Hence Fully Functional Dependency: 2 2 201
2 2 201
3 1 542
Types of Functional Dependency
• Partial Functional Dependency :
• A functional dependency X->Y is a partial dependency if Y is functionally dependent on X
and Y cannot be determined by any proper subset of X.
Ravi 2 DBMS
Tim 3 OS
John 5 Java
• Here , we can see that both the attributes name and roll_no alone is able to uniquely
identify a course. Hence , we can say that the relationship is partial dependent .
Difference
Full Functional Dependency Partial Functional Dependency
A functional dependency X->Y is a fully functional dependency A functional dependency X->Y is a partial dependency if Y is
if Y is functionally dependent on X and Y is not functionally functionally dependent on X and Y cannot be determined by
dependent on any proper subset of X. any proper subset of X.
In full functional dependency , the non-prime attribute is In partial functional dependency , non-prime attribute is
functionally dependent on the candidate key. functionally dependent on part of a candidate key.
In fully functional dependency, if we remove any attribute of In partial functional dependency, if we remove any attribute
X, then the dependency will not exist anymore. of X, then the dependency will still exist .
An attribute A is fully functional dependent on another An attribute A is partially functional dependent on other
attribute B if it is functionally dependent on that attribute , attribute B if it is functionally dependent any part (subset) of
and not on any part (subset) of it . that attribute .
• Consider this relation is decomposed into two sub relations R1( A , B ) and R2( B
, C )- A (R1) B B (R2) C
2 1 R1 ⋈ R2 = R We must get the same relation
1 2
2 5 5 3
3 3 3 3
A C B C
2 1 R1 ⋈ R2 ⊃ R
1 1
2 3 5 3
3 3 3 3
Decomposition
Now, if we perform the natural join ( ⋈ ) of the sub relations R1 and R2 we get-
A B C
1 2 1
2 5 3
2 3 3
3 5 3
3 3 3
Problem on Decomposition
Normalization
• Normalization is a technique of organizing the data in the database.
• Normalization is a systematic approach of decomposing tables to eliminate
data redundancy(repetition) and undesirable characteristics like Insertion,
Update and Deletion Anomalies.
• It is a multistep process that puts data into tabular form, removing
duplicated data from the relation tables.
• Normalization is used for mainly two purposes
• Eliminating redundant(useless) data.
• Ensuring data dependencies make sense i.e data is logically stored.
Normalization
•First Normal Form(1NF):
• If a relation contain composite or multi-valued attribute, it violates first normal form or a
relation is in first normal form if it does not contain any composite or multi-valued
attribute. A relation is in first normal form if every attribute in that relation is singled
valued attribute.
RollNo Name Course Course Contains Multivalued Hence it is not in
401 Dipak DBMS,CN 1NF.
402 Sandip TOC
To convert it into First Normal Form we must
403 Ramesh DS,SE
insert Data in the following Manner.
Or We can Create Another Column
RollNo Name Course Course2,Course3, If data is present then we
401 Dipak DBMS can put or we can put Null values.
401 Dipak CN We can Split the Table in 2 Tables(
402 Sandip TOC Rollno,Name) (RollNo,Course)
403 Ramesh DS
403 Ramesh SE
Normalization
•Second Normal Form(2NF):
• To be in second normal form, a relation must be in first normal form and
relation must not contain any partial dependency. A relation is in 2NF if it
has No Partial Dependency, i.e., no non-prime attribute (attributes which are
not part of any candidate key) is dependent on any proper subset of any
candidate key of the table.
• Partial Dependency – If the proper subset of candidate key determines
non-prime attribute, it is called partial dependency.
Normalization
•Second Normal Form(2NF): In the table Candidate Key is (Cust_Id,Store_Id)
Cust_ID Store_ID Location Which are also prime attributes. The non prime
1 1 Delhi attribute is location which is dependent on
1 3 Mumbai Store_ID not on the Candidate Key. Hence it
2 1 Delhi has partial dependency.
3 2 Pune To convert this into 2NF we must split the Table
4 3 Mumbai
Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP dependent on EMP_ID. The non-prime
attributes (EMP_STATE, EMP_CITY) transitively dependent on super key(EMP_ID). It violates the rule of third normal
form.
Normalization
•Third Normal Form(3NF):
EMP_ID EMP_NAME EMP_ZIP
201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Normalization
• Boyce Codd normal form (BCNF)
• BCNF is the advance version of 3NF. It is stricter than 3NF.
• A table is in BCNF if every functional dependency X → Y, X is the super key of the table.
• For BCNF, the table should be in 3NF, and for every FD, LHS is super key.
EMP_ID EMP_COUNTRY EMP_DEPT DEPT_TYPE EMP_DEPT_NO
EMP_DEPT
EMP_ID EMP_DEPT
EMP_DEPT_MAPPING
D394 283 Functional dependencies:
D394 300 1.EMP_ID → EMP_COUNTRY
2.EMP_DEPT → {DEPT_TYPE, EMP_DEPT_NO}
D283 232
■
Algorithm 11.3: Relational Decomposition into BCNF with
Lossless (non-additive) join property
■
Input: A universal relation R and a set of functional
dependencies F on the attributes of R.
1. Set D := {R};
2. While there is a relation schema Q in D that is not in BCNF
do {
choose a relation schema Q in D that is not in BCNF;
find a functional dependency X Y in Q that violates BCNF;
replace Q in D by two relation schemas (Q - Y) and (X υ Y);
};
■
Algorithm 11.4 Relational Synthesis into 3NF with Dependency
Preservation and Lossless (Non-Additive) Join Property
■
Input: A universal relation R and a set of functional
dependencies F on the attributes of R.
1. Find a minimal cover G for F (Use Algorithm 10.2).
2. For each left-hand-side X of a functional dependency that appears in
G,
create a relation schema in D with attributes {X υ {A1} υ {A2} ...
υ {Ak}},
where X A1, X A2, ..., X –>Ak are the only dependencies in
G with X as left-hand-side (X is the key of this relation).
3. If none of the relation schemas in D contains a key of R, then create
one more relation schema in D that contains attributes that form a key
of R. (Use Algorithm 11.4a to find the key of R)