Unit-2 Last Topic
Unit-2 Last Topic
Unit-2 Last Topic
between these data objects, and the interrelationship between these data
objects. It is a way of storing data on a computer so that it can be used in a
more efficient manner for further purposes.
Data model or data structure consists of following fundamental elements:
1. Data object :
The data object is actually a location or region of storage that contains a
collection of attributes or groups of values that act as an aspect,
characteristic, quality, or descriptor of the object. A vehicle is a data object
which can be defined or described with the help of a set of attributes or data.
Different data objects are present which are shown below:
External entities such as a printer, user, speakers, keyboard, etc.
Things such as reports, displays, signals.
Occurrences or events such as alarm, telephone calls.
Sales databases such as customers, store items, sales.
Organizational units such as division, departments.
Places such as manufacturing floor, workshops.
Structures such as student records, accounts, files, documents.
2. Attributes :
Attributes define the properties of a data object. The attribute is a quality or
characteristic that defines a person, group, or data objects. It is actually the
properties that define the type of entity. An attribute can have a single or
multiple or range of values as per our needs.
There are three types of attributes:
1. Naming attributes –
To name an instance of a data object, naming attributes are used. User
naming attributes identify user objects such as Login_names and User_Id
for some security purpose. For example- Make and model are naming
attributes in a vehicle data object.
2. Descriptive attributes –
These attributes are used to describe the characteristics or features or the
relationship of the data object. Sometimes also referred to as relationship
attributes. For example- In a vehicle, the color of a data object is a
descriptive attribute that describes the features of the object.
3. Referential attribute –
These are the attributes that are used to formalize binary and associative
relationships and in making reference to another instance in another
table. For example- The data object is a referential attribute in a vehicle.
3. Relationship :
The relationship represents the connection or relation between different data
objects and describes association among entities. Relationships are of three
types: one-to-many, many-to-many, and many-to-one.
For example, toy and shopkeeper are two objects that share the following
relationship:
The Shopkeeper order toys.
The shopkeeper sells toys.
The shopkeeper shows toys.
The Shopkeeper stocks toys.
1 001 C001
2 056 C005
Super Key
The set of attributes that can uniquely identify a tuple is known as
Super Key. For Example, STUD_NO, (STUD_NO, STUD_NAME), etc. A
super key is a group of single or multiple keys that identifies rows in
a table. It supports NULL values.
Adding zero or more attributes to the candidate key generates
the super key.
A candidate key is a super key but vice versa is not true.
Super Key values may also be NULL.
Example:
Consider the table shown above.
STUD_NO+PHONE is a super key.
Alternate Key
The candidate key other than the primary key is called an alternate
key.
All the keys which are not primary keys are called alternate keys.
It is a secondary key.
It contains two or more fields to identify two or more records.
These values are repeated.
Eg:- SNAME, and ADDRESS is Alternate keys
Example:
Consider the table shown above.
STUD_NO, as well as PHONE both,
are candidate keys for relation STUDENT but
PHONE will be an 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 a foreign key to the attribute to
which it refers. The relation which is being referenced is called
referenced relation and the corresponding attribute is called
referenced attribute. The referenced attribute of the referenced
relation should be the primary key to it.
It is a key it acts as a primary key in one table and it acts as
secondary key in another table.
It combines two or more relations (tables) at a time.
They act as a cross-reference between the tables.
For example, DNO is a primary key in the DEPT table and a non-
key in EMP
Example:
Refer Table STUDENT shown above.
STUD_NO in STUDENT_COURSE is a
foreign key to STUD_NO in STUDENT relation.
Table STUDENT_COURSE
TEACHER_N
STUD_NO O COURSE_NO
1 005 C001
2 056 C005
It may be worth noting that, unlike the 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 the STUDENT_COURSE relation is not unique. It has
been repeated for the first and third tuples. However, the STUD_NO
in STUDENT relation is a primary key and it needs to be always
unique, and it cannot be null.
Composite Key
Sometimes, a table might not have a single column/attribute that
uniquely identifies all the records of a table. To uniquely identify
rows of a table, a combination of two or more columns/attributes
can be used. It still can give duplicate values in rare cases. So, we
need to find the optimal set of attributes that can uniquely identify
rows in a table.
It acts as a primary key if there is no primary key in a table
Two or more attributes are used together to make a composite
key.
Different combinations of attributes may give different accuracy
in terms of identifying the rows uniquely.
Example:
FULLNAME + DOB can be combined
together to access the details of a student.
1. One-to-one (1:1) – When one entity in each entity set takes part at most
once in the relationship, the cardinality is one-to-one.
2. One-to-many (1: N) – If entities in the first entity set take part in the
relationship set at most once and entities in the second entity set take
part many times (at least twice), the cardinality is said to be one-to-many.
3. Many-to-one (N:1) – If entities in the first entity set take part in the
relationship set many times (at least twice), while entities in the second
entity set take part at most once, the cardinality is said to be many-to-one.
4. Many-to-many (N: N) – The cardinality is said to be many to many if
entities in both the entity sets take part many times (at least twice) in the
relationship set.
Participation Constraints : Participation Constraints tell us that the
participation in a relationship can either be total or partial.
When
each entity in an entity set participates in a relation, it is called Total
Participation. However, when all entities in the given entity set do not
participate in a relation, it is called Partial Participation. Structural
Constraints : Structural Constraints are also called Structural properties of a
database management system (DBMS). Cardinality Ratios and Participation
Constraints taken together are called Structural Constraints. The name
constraints refer to the fact that such limitations must be imposed on the
data, for the DBMS system to be consistent with the requirements.
The Structural
constraints are represented by Min-Max notation. This is a pair of
numbers(m, n) that appear on the connecting line between the entities and
their relationships. The minimum number of times an entity can appear in a
relation is represented by m whereas, the maximum time it is available is
denoted by n. If m is 0 it signifies that the entity is participating in the relation
partially, whereas, if m is either greater than or equal to 1, it denotes total
participation of the entity. Note – Number of times an entity participates in a
relationship is same as the number appearance of the entity in the tuples.
A weak entity type in a database is an entity that cannot be uniquely identified by its
own attributes alone. Instead, it depends on a "strong" or "owner" entity type to provide some or
all of its key attributes. Here’s a detailed look at weak entity types:
ER Diagram Representation
Components of an ER Diagram
1. Entities:
o Definition: An entity represents a real-world object or concept that can be distinctly
identified. Entities typically correspond to tables in a relational database.
o Representation: Entities are depicted as rectangles.
o Example: Employee, Department, Customer, Product.
2. Attributes:
o Definition: Attributes describe the properties or characteristics of an entity.
o Representation: Attributes are represented by ovals connected to their corresponding
entity.
o Types:
Simple Attribute: Cannot be divided further (e.g., FirstName).
Composite Attribute: Can be divided into sub-parts (e.g., FullName can be split
into FirstName and LastName).
Derived Attribute: Calculated from other attributes (e.g., Age from
BirthDate).
Multi-valued Attribute: Can have multiple values (e.g., PhoneNumbers).
o Example: Employee might have attributes like EmployeeID, Name, Address, Phone.
3. Relationships:
o Definition: Relationships illustrate how two or more entities are related to each other.
o Representation: Relationships are represented by diamonds, connected to the entities
they relate to.
o Types:
One-to-One (1:1): An entity in set A is related to one and only one entity in set
B, and vice versa.
One-to-Many (1
Many-to-Many (M
): Entities in set A can be related to multiple entities in set B, and entities in set B
can be related to multiple entities in set A.
4. Primary Key:
o Definition: A primary key is a unique attribute or combination of attributes that uniquely
identifies each instance of an entity.
o Representation: In an ER diagram, the primary key attribute is usually underlined.
5. Foreign Key:
o Definition: A foreign key is an attribute in one entity that links to the primary key of
another entity, establishing a relationship between the two.
o Representation: There is no specific symbol for a foreign key in standard ER diagrams,
but it is implied by the relationship lines.
6. Weak Entity:
o Definition: A weak entity depends on another entity (a strong entity) for its existence
and does not have a primary key of its own.
o Representation: Represented by a double rectangle, and its relationship with the strong
entity is shown by a double diamond.
Example ER Diagram
Entities:
o Student with attributes StudentID, Name, DOB.
o Course with attributes CourseID, CourseName.
o Enrollment (weak entity) with attributes EnrollmentID, Grade.
Relationships:
o Student "enrolls in" Course (One-to-Many relationship from Student to
Enrollment and Course to Enrollment).
o Course "is taught by" Professor (Many-to-Many relationship).
1. Case Sensitivity:
o Choose a case style and stick with it consistently. Common styles include:
snake_case: employee_name
camelCase: employeeName
PascalCase: EmployeeName
2. Descriptive Names:
o Use names that clearly describe the entity, attribute, or object’s purpose.
o Avoid vague abbreviations unless they are widely recognized.
1. Tables:
o Singular vs. Plural: Choose either singular or plural names and be consistent.
Example: Customers (plural) or Customer (singular).
o Descriptive Names: Names should clearly reflect the table’s content.
Example: Employees, Orders, Products.
2. Columns:
o Descriptive Names: Use descriptive names that clearly indicate the data stored in the
column.
Example: FirstName, OrderDate, ProductPrice.
o Prefixing:
Primary Keys: Often named as ID or [TableName]ID.
Example: EmployeeID, OrderID.
Foreign Keys: Name foreign keys to include the name of the referenced table.
Example: CustomerID, OrderID_FK.
3. Primary Keys:
o Naming Pattern: Use ID or [TableName]ID to indicate primary keys.
Example: CustomerID, OrderID.
4. Foreign Keys:
o Naming Pattern: Include the name of the table being referenced, often with a suffix like
_id or _fk.
Example: CustomerID, OrderID_FK.
o Indicates Relationship: This convention helps in understanding relationships between
tables.
5. Indexes:
o Naming Pattern: Start with IDX_ followed by the table name and the column(s)
involved.
Example: IDX_Employee_LastName, IDX_OrderDate.
6. Constraints:
o Naming Pattern: Use prefixes to indicate the type of constraint:
Primary Key: PK_ (e.g., PK_Employees)
Foreign Key: FK_ (e.g., FK_Orders_Customers)
Unique Constraint: UN_ (e.g., UN_Username)
Check Constraint: CK_ (e.g., CK_Price_Positive)
o This helps in quickly identifying the role of the constraint.
8. Views:
o Naming Pattern: Use vw_ or view_ as a prefix to indicate that the object is a view.
Example: vw_CustomerOrders, view_SalesSummary.
9. Stored Procedures:
o Naming Pattern: Use a verb or action word followed by the object’s name.
Example: sp_GetCustomerOrders, sp_UpdateOrderStatus.
10. Triggers:
o Naming Pattern: Use a prefix like trg_ and include the table name and the action.
Example: trg_BeforeInsert_Orders, trg_AfterUpdate_Inventory.