Professional Documents
Culture Documents
DBMSqueBank mst2 1567394904
DBMSqueBank mst2 1567394904
Key constraints
Domain constraints
Referential integrity constraints
Key Constraints
There must be at least one minimal subset of attributes in the relation, which can identify a tuple
uniquely. This minimal subset of attributes is called key for that relation. If there is more than
one such minimal subset, these are called candidate keys.
Key constraints force that −
In a relation with a key attribute, no two tuples can have identical values for key attributes.
A key attribute cannot have NULL values.
Key constraints are also referred to as Entity Constraints.
Domain Constraints
Attributes have specific values in real-world scenario. For example, age can only be a positive
integer. The same constraints have been tried to employ on the attributes of a relation. Every
attribute is bound to have a specific range of values. For example, age cannot be less than zero
and telephone numbers cannot contain a digit outside 0-9.
Referential integrity Constraints
Referential integrity constraints work on the concept of Foreign Keys. A foreign key is a key
attribute of a relation that can be referred in other relation.
Referential integrity constraint states that if a relation refers to a key attribute of a different or
same relation, then that key element must exist.
teacher_id teacher_age
111 38
222 38
333 40
teacher_subject table:
teacher_id subject
111 Maths
111 Physics
222 Biology
333 Physics
333 Chemistry
Solution: A database schema is the skeleton structure that represents the logical view of the
entire database. It defines how the data is organized and how the relations among them are
associated. It formulates all the constraints that are to be applied on the data.
A database schema defines its entities and the relationship among them. It contains a descriptive
detail of the database, which can be depicted by means of schema diagrams. It’s the database
designers who design the schema to help programmers understand the database and make it
useful.
Physical Database Schema − This schema pertains to the actual storage of data and its form of
storage like files, indices, etc. It defines how the data will be stored in a secondary storage.
Logical Database Schema − This schema defines all the logical constraints that need to be
applied on the data stored. It defines tables, views, and integrity constraints.
Intension
The intension is the schema of the relation and thus is independent of the time as it does not
change once created. So, it is the permanent part of the relation and consists of –
1. Naming Structure – Naming Structure includes the name of the relation and the attributes of
the relation.
2. Set of Integrity Constraints – The Integrity Constraints are divided into Integrity Rule 1 (or
entity integrity rule), Integrity Rule 2 (or referential integrity rule), key constraints, domain
constraints etc.
For example:
Employee (EmpNo Number (4) NOT NULL, EName Char(20), Age Number(2), Dept
Char(4)
Extension
The set of tuples appearing at any instant in a relation is called the extension of that relation. In
other words, instance of Schema is the extension of a relation. The extension varies with time as
instance of schema or the value in the database will change with time.
Q4. Explain inner join with the help of examples. Also, explain the need of theta join?
Solution: INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as
long as the condition satisfies. This keyword will create the result-set by combining all rows
from both the tables where the condition satisfies i.e value of the common field will be same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
This query will show the names and age of students enrolled in different courses.
Output:
Q5. What is normalization? Discuss the need for normalization with examples.
Solution: Normalization is a database design technique which organizes tables in a manner that
reduces redundancy and dependency of data. It divides larger tables to smaller tables and link
them using relationships.
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.
Here are the most commonly used normal forms:
First normal form(1NF)
Second normal form(2NF)
Third normal form(3NF)
Boyce & Codd normal form (BCNF)
Solution: i) Null Values: The SQL NULL is the term used to represent a missing value. A NULL
value in a table is a value in a field that appears to be blank. A field with a NULL value is a field
with no value.
A null value is placed in a field of a table when a specific value is either unknown or inappropriate.
Here it is unknown. A slightly different meaning would be if warehouse manager also had a percent
(commission) field, but since warehouse managers don't get commissions, would get null value.
A null value can be used for either a numeric or character type. BUT IT HAS A DIFFERENT VALUE
FROM ANY REAL FIELD. In particular, it is not zero (0) or the null string (''). It is handled specially
by commercial databases.
Dangling tuples: An attribute of the join operator is that it is possible for certain tuples to be
"dangling"; that is, they fail to match any tuple of the other relation in the common attributes.
Dangling tuples do not have any trace in the result of the join, so the join may not represent the
data of the original relations completely.
ii) Multivalued dependency occurs when there are more than one independent multivalued
attributes in a table, a multivalued dependency is a full constraint between two sets of attributes
in a relation.
In contrast to the functional dependency, the multivalued dependency requires that
certain tuples be present in a relation. Therefore, a multivalued dependency is a special case
of tuple-generating dependency. The multivalued dependency plays a role in the 4NF database
normalization.
A multivalued dependency is a special case of a join dependency, with only two sets of values
involved, i.e. it is a binary join dependency.
For example: Consider a bike manufacture company, which produces two colors (Black and white) in
each model every year.
bike_model manuf_year Color
Here columns manuf_year and color are independent of each other and dependent on
bike_model. In this case these two columns are said to be multivalued dependent on bike_model.
These dependencies can be represented like this:
bike_model ->> manuf_year
bike_model ->> color
Solution: A functional dependency A->B in a relation holds if two tuples having same value of
attribute A also have same value for attribute B. For Example, in relation STUDENT shown in
table 1, Functional Dependencies
STUD_NO->STUD_NAME, STUD_NO->STUD_ADDR hold
but
Functional Dependencies in a relation are dependent on the domain of the relation. Consider the
STUDENT relation given in Table 1.
Solution:
Q9. Discuss different types of keys in relational model with suitable examples.
Solution: Candidate Key: The minimal set of attribute which can uniquely identify a tuple is
known as candidate key. For Example, STUD_NO in STUDENT relation.
The value of Candidate Key is unique and non-null for every tuple.
There can be more than one candidate key in a relation. For Example, STUD_NO as well
as STUD_PHONE both are candidate keys for relation STUDENT.
The candidate key can be simple (having only one attribute) or composite as well. For
Example, {STUD_NO, COURSE_NO} is a composite candidate key for relation
STUDENT_COURSE.
Super Key: The set of attributes which can uniquely identify a tuple is known as Super Key. For
Example, STUD_NO, (STUD_NO, STUD_NAME) etc.
Primary Key: There can be more than one candidate key in a relation out of which one can be
chosen as primary key. For Example, STUD_NO as well as STUD_PHONE both are candidate
keys for relation STUDENT but STUD_NO can be chosen as primary key (only one out of many
candidate keys).
Alternate Key: The candidate key other than primary key is called as alternate key. For
Example, STUD_NO as well as STUD_PHONE both are candidate keys for relation STUDENT
but STUD_PHONE will be alternate key (only one out of many candidate keys).
Foreign Key: If an attribute can only take the values which are present as values of some other
attribute, it will be foreign key to the attribute to which it refers. The relation which is being
referenced is called referenced relation and corresponding attribute is called referenced attribute
and the relation which refers to referenced relation is called referencing relation and
corresponding attribute is called referencing attribute. Referenced attribute of referenced relation
should be primary key for it. For Example, STUD_NO in STUDENT_COURSE is a foreign key
to STUD_NO in STUDENT relation.
It may be worth noting that unlike, Primary Key of any given relation, Foreign Key can be
NULL as well as may contain duplicate tuples i.e. it need not follow uniqueness constraint.
For Example, STUD_NO in STUDENT_COURSE relation is not unique. It has been repeated
for the first and third tuple. However, the STUD_NO in STUDENT relation is a primary key and
it needs to be always unique and it cannot be null.
b) Transitive Dependency
If a non-prime attribute of the relation is getting derived by either another non-prime attribute or
the combination of part of the candidate key along with a non-prime attribute then such
dependency would be defined as transitive dependency.
(1) While identifying the transitive dependency make sure that the dependent is a non-prime
attribute and the determinant could be either a non-prime attribute or the combination of part of
the candidate key along with non-prime attribute.
(2) Transitive dependency will cause redundancy in the relation and hence it will get eliminated
by normalization technique.