Bcs Sem II Dbms Unit III
Bcs Sem II Dbms Unit III
Unit III
DATABASE DESIGN & THE E-R DATA MODEL
Database design develops the logical and physical structure of a database in a given
database management system so that it contains all the information required by the user.
Properly designed database are easy to maintain, improves data consistency and are cost
effective in terms of disk storage space.
Information for needs: What kind of information is needed –that is, what output
(reports and queries) must be generated by the system, what information does the
current system generate, and to what extent is that information adequate?
Information of users: Who will use the information? How is the information to be
used? What are the various end-user data views?
Interfacing with the systems design group: The database design process is part of
the Systems Development Life Cycle(SDLC). In some cases, the systems analyst in
charge of designing the new system will also develop the conceptual database model.
S1
Student
S2
Entity
S3
Entity-Set
b. Relationship Sets:
The association among entities is called a relationship. For example, an employee
works at a department, a student enrolls in a course. Here, Works at and Enrolls are
called relationships.
For example, ‘Enrolled in’ is a relationship type that exists between entity
Student and Course. In ER diagram, relationship type is represented by a diamond and
connecting the entities with lines.
1. 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.
2. 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.
3. N-ary Relationship-
When there are n entities set participating in a relation, the relationship is called as n-ary
relationship. For example, an employee works in a department at a specific location.
The number of times an entity of an entity set participates in a relationship set is known as
cardinality. Cardinality can be of different types:
1 has 1 Passport
2. One-to-Many: When a single row of an entity is associated with more than one rows
of another entity then it is called one to many relationships. For example-a customer
can place many orders but an order cannot be placed by many customers.
1 placed M Order
3. Many-to-many: When more than one rows of an entity are associated with more than
one rows of another entity then it is called many to many relationships. For example,
a can be assigned to many projects and a project can be assigned to many students.
M Assigned M Project
Types of attributes
1. Key attribute: A key attribute can uniquely identify an entity from an entity set. It
can be simple attribute and also a single-valued attribute.
For e.g.: Student’s roll number can uniquely identify a student from a set of students. Key
attribute is represented by oval same as other attributes however the text of key attribute is
underlined to represent it as a primary key.
3. Multi-valued attributes: Attributes with more than one value are multi-valued
attributes. It is represented with double ovals in an ER diagram.
For e.g.: Mobile_No, Email_id, etc.
4. Derived attributes- Attributes that do not have physical existence in the database but
get their values from existing attributes are derived attributes. It is represented by
dashed oval in an ER diagram.
For e.g.: DOB derives age, average salary derived from salary.
This ensures the accuracy and reliability of the data in the database. Constraints could
be either on a column level or a table level. The column level constraints are applied to the
whole table.
Some of the available constraints in SQL are:
1. NOT NULL:The NOT NULL constraint specifies that the column does not accept NULL
values. When we don’t provide value for a particular column while inserting a record into
a table, it takes NULL value by default. By specifying NULL constraint, we can be sure
that a particular column(s) cannot have NULL values.
Adding constraint in a New Table:
SQL>CREATE TABLE STUDENT(RNO INT NOT NULL<NAME
VARCHAR(10),CLASS VARCHAR(5));
Adding constraint in an existing Table:
SQL>ALTER TABLE STUDENT ADD NOT NULL(RNO);
Dropping Constraint:
SQL>ALTER TABLE STUDENT DROP NOT NULL RNO;
2. UNIQUE: The UNIQUE constraint specifies that the column does not have accept
(redundant) duplicate values. It ensures that each row for a column must have a unique value.
We can have more than one UNIQUE columns in a table.
3. PRIMARY KEY: A PRIMARY KEY is a combination of not null & unique constraints
i.e. does not allow null and duplicate values in the column. Primary key constraint uniquely
identifies each record in a database. Multiple UNIQUE constraints can be defined on a table.
Dropping Constraint:
SQL> ALTER TABLE STUDENT DROP PRIMARY KEY;
4. FOREIGN KEY: FOREIGN KEY is a field in a table which uniquely identifies each row
of a another table i.e. this field points to primary key of another table. The relationship
between 2 tables matches the primary key in one of the tables with a foreign key in the
second table. FOREIGN KEY constraint applied column must have same data type as the
reference on another table column.
5. CHECK: Using the CHECK constraint we can specify a condition for a field, which
should be satisfied at the time of entering values for this field.
Check constraint is used to restrict the value of a column between a range. It performs check
on the values, before storing them into the database.
For example, the below query creates a table Student and specifies the condition of the field
AGE as (AGE>=18), i.e. the user will not be allowed to enter any record in the table with
AGE<18.
Adding Constraint in a New Table:
SQL>CREATE TABLE STUDENT(RNO INT, NAME VARCHAR(10),CLASS
VARCHAR(5),AGE INT NOT NULL CHECK(AGE>=18));
Adding Constraint in an existing Table:
SQL>ALTER TABLE STUDENT ADD CHECK(AGE>=18);
SQL>ALTER TABLE STUDENT ADD CONSTRAINT VERIFY_AGE
CHECK(AGE>=18);
Dropping Constraint:
6. DEFAULT: This constraint is used to provide a default value for the fields. That is, if at
the time of entering new records in the table if the user does not specify any value for these
fields then the default value will be assigned to them.
KEYS:
A DBMS key is an attribute or set of an attribute which helps you to identify a row(tuple)
in a relation (table). They allow you to find the relation between two tables.
Keys help you uniquely identify a row in a table by a combination of one or more
columns in that table.
DBMS has following seven types of keys each has their own functionality:
2. Foreign Key: In a relationship between two tables, a primary key of one table is
referred as a foreign key in another table. Foreign key can have duplicate values in it
and can also keep null values if column is defined to accept null values.
3. Alternate Key: A table can have multiple choices for a primary key but only one can
be set as the primary key. All the keys which are not primary key are called as
Alternate key.
5. Super Key: A super key is a group of single or multiple keys which identifies rows in
a table. A super key may have additional attributes that are not needed for unique
identification. Primary key, Alternate Key are a subset of super keys.
6. Candidate Key: Candidate key is a super key with no repeated attributes. The
primary key should be selected from the candidate keys. Every table must have at
least a single candidate key. A table can have multiple candidate keys but only a
single primary key.
7. Unique Key: We can have other fields also in a table beyond primary key which are
also be able to uniquely identify the record. It can accept only one null value and it
cannot have duplicate values in it.
Participation Constraints: In a Relationship, participation constraint specifies the
presence of an entity when it is related to another entity in a relationship type. It is also
called the minimum cardinality constraint.
This constraint specifies the number of instances of an entity that are participating in
the relationship.
1. Total Participation:
It specifies that each entity present in the entity set must mandatorily participate in at
least one relationship instance of that relationship set, for this reason, it is also called
as mandatory participation.
It is represented using a double line between the entity set and relationship set.
For e.g., It specifies that each student must be enrolled in at least one course where
the “student” is the entity set and relationship “enrolled in” signifies total
participation. It means that every student must have enrolled at least in one course.
2. Partial participation:
It specifies that each entity in the entity set may or may not participate in the
relationship instance of the relationship set, is also called as optional participation.
It is represented using a single line between the entity set and relationship set in the
ER diagram.
A single line between the entities i.e. courses and enrolled in a relationship signifies
the partial participation, which means there might be some courses where enrollments
are not made i.e. enrollments are optional in that case.
An Entity-relationship model (ER model) describes the structure of a database with the
help of a diagram, which is known as Entity Relationship Diagram (ER Diagram). An ER
model is a design or blueprint of a database that can later be implemented as a database. The
main components of E-R model are: entity set and relationship set.
An ER diagram shows the relationship among entity sets. An entity set is a group of
similar entities and these entities can have attributes. Peter Chen developed ERDs in 1976.
ER diagram uses certain standard shapes to denote different components of a database
object. There are five main components of an ERD as:
Student
2. Attributes:
i. Simple Attribute
ii. Composite Attribute
iii. Multi-valued Attribute
iv. Derived Attribute