You are on page 1of 6

UNIT-1

CHAPTER 1.3
(Mapping constraints)

Mapping Constraints
● A mapping constraint is a data constraint that expresses the number of entities to
which another entity can be related via a relationship set.
● It is most useful in describing the relationship sets that involve more than two entity
sets.
● For binary relationship set R on an entity set A and B, there are four possible mapping
cardinalities. These are as follows:
1. One to one (1:1)
2. One to many (1:M)
3. Many to one (M:1)
4. Many to many (M:M)

One-to-one
● When a single instance of an entity is associated with a single instance of another
entity, then it is called as one to one cardinality
● Here each entity of the entity set participate only once in the relationship
Example for one to one cardinality

Assume that the only male can be married to only one female and one female can be
married to only one male, this can be viewed as one to one cardinality

In one-to-one mapping, an entity in E1 is associated with at most one entity in E2, and an
entity in E2 is associated with at most one entity in E1.

One-to-many
● When is a single instance of an entity is associated with more than one instance of
another entity then this type of relationship is called one to many relationships
●Here entities in one entity set can take participation in any number of times in
relationships set and entities in another entity set can take participation only once in a
relationship set
Example for one to many cardinalities

 For example in the Real world, many students can study in a single college but the student
cannot apply to more than one college at the same time

In one-to-many mapping, an entity in E1 is associated with any number of entities in E2, and
an entity in E2 is associated with at most one entity in E1.

Many-to-one

When entities in one entity set can participate only once in a relationship set and entities in
another entity can participate more than once in the relationship set, then such type of
cardinality is called many-to-one

Example for many to one cardinality


● In real time a student takes only one course but a single course can be taken by any
number of students. so many to one relationship is observed here
● It means that  one course can be taken by any number of students but only one
course can be allotted to one student 

In one-to-many mapping, an entity in E1 is associated with at most one entity in E2, and an
entity in E2 is associated with any number of entities in E1.
Many-to-many
● Here  more than one instance of an entity is associated with more than one instance of
another entity then it is called many to many relationships
● In this cardinality, entities in all entity sets can take participate any number of
times in the relationship cardinality is many to many.
Example for many to many cardinality

In the Real world assume that a student can take more than one course and the single
course can be taken by any number of students this relationship will be many to many
relationship

In many-to-many mapping, an entity in E1 is associated with any number of entities in E2,


and an entity in E2 is associated with any number of entities in E1.

We can have these constraints in place while creating tables in database.

Example:

CREATE TABLE Customer (

customer_id int PRIMARY KEY NOT NULL,

first_name varchar(20),

last_name varchar(20)
);

CREATE TABLE Order (

order_id int PRIMARY KEY NOT NULL,

customer_id int,

order_details varchar(50),

constraint fk_Customers foreign key (customer_id)

references dbo.Customer

);

Assuming, that a customer orders more than once, the above relation represents one to
many relation. Similarly we can achieve other mapping constraints based on the
requirements.

Constraints on Relational database model

On modeling the design of the relational database we can put some restrictions like what
values are allowed to be inserted in the relation, what kind of modifications and deletions are
allowed in the relation. These are the restrictions we impose on the relational database.

In models like ER models, we did not have such features.

Constraints in the databases can be categorized into 3 main categories:

1. Constraints that are applied in the data model is called Implicit constraints.


2. Constraints that are directly applied in the schemas of the data model, by specifying
them in the DDL( Data Definition Language). These are called as schema-based
constraints or Explicit constraints.
3. Constraints that cannot be directly applied in the schemas of the data model. We call
these Application based or semantic constraints.

So here we will deal with Implicit constraints.

Mainly Constraints on the relational database are of 4 types:

1. Domain constraints
2. Key constraints
3. Entity Integrity constraints
4. Referential integrity constraints

Let discuss each of the above constraints in detail.


1. Domain constraints :

1. Every domain must contain atomic values(smallest indivisible units) it means


composite and multi-valued attributes are not allowed.
2. We perform datatype check here, which means when we assign a data type to a
column we limit the values that it can contain. Eg. If we assign the datatype of
attribute age as int, we cant give it values other then int datatype.

Example:

Explanation:

In the above relation, Name is a composite attribute and Phone is a multi-values attribute, so
it is violating domain constraint.

2. Key Constraints or Uniqueness Constraints :

1. These are called uniqueness constraints since it ensures that every tuple in the relation
should be unique.
2. A relation can have multiple keys or candidate keys(minimal superkey), out of which
we choose one of the keys as primary key, we don’t have any restriction on choosing
the primary key out of candidate keys, but it is suggested to go with the candidate key
with less number of attributes.
3. Null values are not allowed in the primary key, hence Not Null constraint is also a
part of key constraint.

Example:

Explanation:

In the above table, EID is the primary key, and first and the last tuple has the same value in
EID ie 01, so it is violating the key constraint.

3. Entity Integrity Constraints :

1. Entity Integrity constraints says that no primary key can take NULL value, since
using primary key we identify each tuple uniquely in a relation.

Example:

Explanation:
In the above relation, EID is made primary key, and the primary key cant take NULL values
but in the third tuple, the primary key is null, so it is a violating Entity Integrity constraints.

4. Referential Integrity Constraints :

1. The Referential integrity constraints is specified between two relations or tables and
used to maintain the consistency among the tuples in two relations.
2. This constraint is enforced through foreign key, when an attribute in the foreign key
of relation R1 have the same domain(s) as the primary key of relation R2, then the
foreign key of R1 is said to reference or refer to the primary key of relation R2.
3. The values of the foreign key in a tuple of relation R1 can either take the values of the
primary key for some tuple in relation R2, or can take NULL values, but can’t be
empty.

Example:

Explanation:

In the above, DNO of the first relation is the foreign key, and DNO in the second relation is
the primary key. DNO = 22 in the foreign key of the first table is not allowed since DNO =
22

is not defined in the primary key of the second relation. Therefore Referential integrity
constraints is violated here

OTHER REFRENCES

•     DBMS Mapping constraints - javatpoint

•      Mapping Constraints in DBMS » PREP INSTA

•    Mapping constraints in DBMS (beginnersbook.com)

SUGGESTED BOOK REFERENCES

1. Ramez Elmasri and Shamkant B. Navathe,“Fundamentals of Database System”, The


Benjamin / Cummings Publishing Co.
2. Korth and Silberschatz Abraham, “Database SystemConcepts”, McGraw Hall.
3. Pratt,”DBMS”, Cengage Learning.

You might also like