Unit 2 DMS
Unit 2 DMS
Unit 2 DMS
(313302)
Instructor
Prof. S.P.Chivate
K. K. Wagh Polytechnic, Nashik
Course Outcome
Course Outcome:
CO-2: Design the database for given problem.
Unit Outcomes:
TLO 2.2: State types of keys with example
Relational Structures
1. Entity:
• An entity is a “thing” or “object” in the real world.
• An entity contains attributes, which describe that entity.
• So anything about which we store information is called an entity.
• For example: A student, An employee, or bank a/c, etc. all are entities.
Relational Structures
2. Domain:
- A domain is the set of permitted values for one or more attributes.
- It defines the potential values that an attribute may hold.
- For e.g. If the age of the student in class is between 15 and 20, then we can
define a set of values for the age attribute of student as the set of integers
between 15 and 20.
3. Attribute:
- An attribute is a named column of a relation.
- For e.g. Student table has stud_id, Stud_name, Marks as attributes
Relational Structures
4. Tuple:
- Tuple can also be called as record.
- Record can be defined as any row of a table.
- For e.g. data of one student in one row in student table is tuple.
5. Relation:
- A relation is a table with columns and rows.
- Tables are used to hold information about the objects to be represented in
the database.
Relational Structures
DBMS Keys
- A DBMS key is an attribute or a set of attributes which help you uniquely identify a record
or row of data in a table.
i) Primary Key
Constraints :-
- Constraints are the rules that we can apply on the type of data in a table.
- Constraints in SQL means we are applying certain conditions or restrictions on
the database.
- This further means that before inserting data into the database, we are checking
for some conditions.
- If the condition we have applied to the database holds true for the data which is
to be inserted, then only the data will be inserted into the database tables.
Data Constraints
Integrity Constraints :-
- These are a set of rules.
- They ensure that when the authorized users modify the database they do not
disturb the data consistency.
- These are introduced while designing the database schema.
- The constraints are specified within the DDL commands like ‘create table’ and
‘alter table’ command.
- Types of Relational Integrity Constraints are as follows:
1. Domain constraints
2. Entity integrity constraints
3. Referential Integrity Constraints
Data Constraints
1. Domain constraints :-
- Domain constraint defines the domain or 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.
- There are 2 constraints which we can study under domain constraint.
i) Not null constraint
ii) Check clause constraint
1. Domain constraints
i. Not Null constraints
- By default, a column can hold NULL values.
- The NOT NULL constraint enforces a column to NOT accept NULL values.
- This enforces a field to always contain a value, which means that you cannot insert a new
record, or update a record without adding a value in the field.
- For example: in student database, every student must have an associated student name.
- Let us see how do we specify an attribute to be not null.
create table Student
(Student_id varchar (5) ,
name varchar (20) not null,
dept_name varchar (20));
1. Domain Constraints
ii. Check Constraint
- Defines a condition that each row must satisfy.
- Used to restrict the value of a column between ranges.
- It is like condition checking before saving data into a column.
- The check constraint ensures that when a new tuple is inserted in relation it must satisfy
the condition specified in the check clause.
- For example :
Create table student (
Roll_no number(2) not null CHECK (Roll_no>0),
Name varchar(50) not null);
- The above example creates a CHECK constraint on the Roll_no column.
- The CHECK constraint specifies that the column Roll_no must only include integers
greater than 0.
2. Entity integrity constraints
i. Primary Key Constraints
- The UNIQUE key constraint uniquely identifies each record in a database table.
- UNIQUE key constraint ensures that a field or column will only have unique values.
- It is used to prevent duplicate values in a column or group.
- It allows null values.
- The unique key and primary key both provide a guarantee for uniqueness for a column or a
set of columns.
- There may be many unique key constraints for one table, but only one PRIMARY KEY
constraint for one table.
2. Entity integrity constraints
-Unique Key Example
- The above SQL creates a UNIQUE constraint on the "Roll_no" and ‘‘Seat_no ‘‘
column when the "student" table is created.
3. Referential Integrity constraint
Referential Integrity constraint
- Referential Integrity Constraint is also known as foreign key constraint.
- It is defined between two tables to maintain the consistency among tuples in
the two relations.
- It is used to establish parent child relationship between two tables having a
common column.
- It states that if there is a foreign key in one table, it should match with
primary key from other table.
- Primary key is defined in parent table. Parent table is also called as
referenced table.
- Foreign key is defined in child table.
3. Referential Integrity constraint
Referential Integrity Example
Dept
- E-R Diagram is a visual representation of data that describes how data is related
to each other.
Entity Student
E-R Model
Relationship :-
- A relationship represents the association between entity types.
- For example. ‘Enrolled in’ is a relationship that exists between entity Student and
Course.
- In ER diagram, relationship is represented by a diamond and connecting the entities
with lines.
Example
E-R Model
Degree of a Relationship :-
- It refers to number of entities participated in the relation.
A) Unary Relationship
B) Binary Relationship
C) Ternary Relationship
D) Quaternary Relationship
ER Model - Degree of a Relationship
a) Unary Relationship:
- When there is only ONE entity set participating in a relation, the relationship is called as
unary relationship.
- For example, one person is married to only one person.
ER Model - Degree of a Relationship
b) Binary Relationship –
- When there are TWO entities set participating in a relation, the relationship is
called as binary relationship.
- For example, Student is enrolled in Course.
ER Model - Degree of a Relationship
c) Ternary Relationship –
- When there are THREE entities set participating in a relation, the relationship is
called as Ternary relationship.
- For example, Teacher teahces the subject to the student.
ER Model - Degree of a Relationship
d) Quaternary Relationship –
- When four entities get associated to form a relationship is called quaternary
relationship.
- For example studies is a relationship where four entities are involved-
STUDENT, TEACHER, COURSE_MATERIAL and SUBJECT. A Student is studying a
subject by a teacher with the help of the course material.
E-R Model
Attributes :-
- An Attribute describes a property or characteristic of an entity.
- For example, Name, Age, Address can be attributes of a Student.
Attribute
- For example:
Name Age Address
E-R Model
Types of Attributes :-
a) Simple attribute−
PHONE
- these are atomic values, which cannot be divided further. NO
- For example, a student's phone number is an atomic value of 10 digits.
b) Single-valued attribute-
- If an attribute can take only a single value for each entity instance,
it is a single valued attribute.
- For example age of a student can be single valued attribute.
25
- It can take only one value for a particular student.
E-R Model
c) Composite attribute-
Key
Attribute
E-R Model
Entity Set
- collection of similar types of entities.
- set of entities of the same type that share the same properties or attributes.
- For e.g. the set of all students is defined as entity set.
- There are two types of entity set:
a) Strong Entity Set
b) Weak Entity Set
E-R Model
a) Strong Entity Set:
- The entity set that have sufficient attributes to form a primary key is called as
Strong entity set.
- represented by a rectangle.
- also called as owner entity set.
- relationship between two strong entity set is represented by a diamond symbol.
Strong Entity
E-R Model
b) Weak Entity Set:
- The entity set which does not have sufficient attributes to form a primary key is
called as Weak entity set.
- Weak entity depends on the strong entity for its existence.
- Weak entity is represented by double rectangle.
- The relationship between one strong entity set and one weak entity set is
represented by a double diamond sign.
E-R Model
Entity Set -example
E-R Model
Difference between Strong and Weak Entity Set
- Multivalued dependency is one where more than one attributes are dependent on
single attribute.
- Here COLOR and MANU_YEAR are dependent on BIKE_MODEL and independent of each other.
- Here, two columns can be called as multivalued dependent on BIKE_MODEL.
- They are represented like:
BIKE_MODEL →→ MANU_YEAR
BIKE_MODEL →→ COLOR
- Here, "BIKE_MODEL multidetermines MANU_YEAR" and "BIKE_MODEL multidetermines COLOR".
Normalization - Functional Dependency
iv. Transitive dependency
- A functional dependency is said to be transitive if it is indirectly formed by two functional
dependencies. A transitive dependency can occur in a relation of three or more attributes.
- e.g. X -> Z is a transitive dependency if the following three functional dependencies hold true
X->Y
Y does not ->X
Y->Z
- {Book} ->{Author} (if we know the book, we knows the author name)
{Author} -> {Author_age}
- Therefore as per the rule of transitive dependency: {Book} -> {Author_age} should be there
because if we know the book name we can know the author’s age.
Normal Forms
First Normal Form (1NF)
- 1NF is a property of a relation in a relational database.
- A database is in first normal form if it satisfies the following conditions:
i) Contains only atomic values
ii) There are no repeating group.
- An atomic value is a value that cannot be divided, means it should have single
valued attributes.
- There are no repeating groups of data. This means a table should not contain
repeating columns.
Normal Forms
First Normal Form- Example
- This table is not in first normal form
because the [Color] column can
contain multiple values.
- As the first row includes values "red"
and "green" values for [Color]
column.
- To bring this table to first normal
form, we split the table into two
tables.
Normal Forms
First Normal Form- Example
- In the table, [Book ID] determines [Genre ID], and [Genre ID] determines [Genre Type].
- Therefore, [Book ID] determines [Genre Type] via [Genre ID] here, we have transitive
functional dependency so this structure does not satisfy third normal form.
- To bring this table to third normal form, we split the table into two as follows:
Normal Forms
Third Normal Form- Example
- Now all non-key attributes are fully functional dependent only on the primary key.
- In [TABLE_BOOK], both [Genre ID] and [Price] are only dependent on [Book ID].
- In [TABLE_GENRE], [Genre Type] is only dependent on [Genre ID].
Normal Forms
Third Normal Form- Example
- Consider a single table consisting following columns. Convert it into 3NF :
(supplier_no, supplier_name, supplier_city, order_no, order_quantity, order_amount,
product_code, product_name)
- To convert the above tables in 3NF ,We have to decompose them in three tables satisfying
the transitive dependencies property.
ANS : Given Table Schema - (supplier_no, supplier_name, supplier_city, order_no,
order_quantity, order_amount, product_code, product_name)
- Table 1- Supplier Details (supplier_no , supplier_name, supplier_city)
- Table 2- Product Details (product_code, product_name)
- Table 3- Order Details (order_no, product_code, supplier_no, order_ quantity,
order_amount)
- Hence the above three tables are satisfying Transitive dependencies. Thus they are in 3NF.
Normal Forms
Boyce Codd Normal Form
- BCNF is the advance version of 3NF.
- For BCNF, the table should be in 3NF and for every Functional Dependency, Left Hand Side
is a super key.
- Example: Let's assume there is a company where employees work in more than one
department. They store the data like this:
emp_dept_mapping table
- Functional dependencies:
emp_id -> emp_nationality
emp_dept -> {dept_type, dept_no}
- Candidate keys:
For first table: emp_id
For second table: emp_dept
For third table: {emp_id, emp_dept}
- This is now in BCNF as in both the functional dependencies left side part is a key
Normal Forms
Difference between 3NF and BCNF
3NF BCNF
There should be no transitive functional For any trivial dependency in a relation R
dependency. say X->Y,
Where X should be a super key of relation R.
3NF can be obtained without sacrificing all Dependencies may not be preserved in BCNF.
dependencies.
Lossless decomposition can be achieved in Lossless decomposition is hard to achieve in
3NF. BCNF.
More redundancy. Less redundancy.
3NF can't capture all the anomalies. BCNF can capture all the anomalies.