Databasenotesclass
Databasenotesclass
Databasenotesclass
The database is a collection of inter-related data which is used to retrieve, insert and
delete the data efficiently. It is also used to organize the data in the form of a table,
schema, views, and reports, etc.
For example: The college Database organizes the data about the admin, staff,
students and faculty etc.
Using the database, you can easily retrieve, insert, and delete the information.`
Characteristics of DBMS
o It uses a digital repository established on a server to store and manage the
information.
o It can provide a clear and logical view of the process that manipulates data.
o DBMS contains automatic backup and recovery procedures.
o It contains ACID properties which maintain data in a healthy state in case of
failure.
o It can reduce the complex relationship between data.
o It is used to support manipulation and processing of data.
o It is used to provide security of data.
o It can view the database from different viewpoints according to the
requirements of the user.
Advantages of DBMS
o Controls database redundancy: It can control data redundancy because it
stores all the data in one single database file and that recorded data is placed
in the database.
o Data sharing: In DBMS, the authorized users of an organization can share the
data among multiple users.
o Easily Maintenance: It can be easily maintainable due to the centralized
nature of the database system.
o Reduce time: It reduces development time and maintenance need.
o Backup: It provides backup and recovery subsystems which create automatic
backup of data from hardware and software failures and restores the data if
required.
o multiple user interface: It provides different types of user interfaces like
graphical user interfaces, application program interfaces
Disadvantages of DBMS
o Cost of Hardware and Software: It requires a high speed of data processor
and large memory size to run DBMS software.
o Size: It occupies a large space of disks and large memory to run them
efficiently.
o Complexity: Database system creates additional complexity and
requirements.
o Higher impact of failure: Failure is highly impacted the database because in
most of the organization, all the data stored in a single database and if the
database is damaged due to electric failure or database corruption then the
data may be lost forever.
Data Independence
o Data independence can be explained using the three-schema architecture.
o Data independence refers characteristic of being able to modify the schema at one
level of the database system without altering the schema at the next higher level.
The logical
Pointers are used to
A linked list is used to representation is used
establish relationships
4. establish a relationship with rows and columns
among records
among records physically. to depict relationship
physically.
among records.
Difficult to design a
database and manipulate It is easy to
Difficult to design a
a database because of its comprehend due to
11. database because of its
complexity. Hence, it concealed physical level
complexity.
imposes a burden on the details from end-users.
programmer.
It is flexible as
It is flexible as compared
12. It is less flexible. compared to the
to the hierarchical model.
hierarchical model.
VAX-DBMS, DMS-1100 of
It is mostly used in real
&XML and XAML use UNIVAC and
13. world applications.
this model. SUPRADBMS’s use this
Oracle, SQL.
model.
For example, Suppose we design a school database. In this database, the student
will be an entity with attributes like address, name, id, age, etc. The address can be
another entity with attributes like city, street name, pin code, etc and there will be a
relationship between them.
Component of ER Diagram
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an entity can
be represented as rectangles.
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak entity
doesn't contain any key attribute of its own. The weak entity is represented by a
double rectangle.
2. Attribute
The attribute is used to describe the property of an entity. Eclipse is used to
represent an attribute.
For example, id, age, contact number, name, etc. can be attributes of a student.
a. Key Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as a
multivalued attribute. The double oval is used to represent multivalued attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
An attribute that can be derived from other attribute is known as a derived attribute.
It can be represented by a dashed ellipse.
For example, A person's age changes over time and can be derived from another
attribute like Date of birth.
3. Relationship
A relationship is used to describe the relation between entities. Diamond or rhombus
is used to represent the relationship.
Types of relationship are as follows:
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship, then it is
known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one instance of an
entity on the right associates with the relationship then this is known as a one-to-
many relationship.
For example, Scientist can invent many inventions, but the invention is done by the
only specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one instance of an
entity on the right associates with the relationship then it is known as a many-to-one
relationship.
For example, Student enrolls for only one course, but a course can have many
students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than one instance
of an entity on the right associates with the relationship then it is known as a many-
to-many relationship.
For example, Employee can assign by many projects and project can have many
employees.
Cardinality
Cardinality means how the entities are arranged to each other or what is the
relationship structure between entities in a relationship set. In a Database
Management System, Cardinality represents a number that denotes how many times
an entity is participating with another entity in a relationship set. The Cardinality of
DBMS is a very important attribute in representing the structure of a Database. In a
table, the number of rows or tuples represents the Cardinality.
Cardinality Ratio
Cardinality ratio is also called Cardinality Mapping, which represents the mapping
of one entity set to another entity set in a relationship set. We generally take the
example of a binary relationship set where two entities are mapped to each other.
1. One to one
2. Many to one
3. One to many
4. Many to many
One to One
One to one cardinality is represented by a 1:1 symbol. In this, there is at most one
relationship from one entity to another entity. There are a lot of examples of one-to-
one cardinality in real life databases.
For example, one student can have only one student id, and one student id can
belong to only one student. So, the relationship mapping between student and
student id will be one to one cardinality mapping.
Another example is the relationship between the director of the school and the
school because one school can have a maximum of one director, and one director
can belong to only one school.
Note: it is not necessary that there would be a mapping for all entities in an entity
set in one-to-one cardinality. Some entities cannot participate in the mapping.
One to one Cardinality is the subset of Many to one Cardinality. It can be represented
by M:1.
For example, there are multiple patients in a hospital who are served by a single
doctor, so the relationship between patients and doctors can be represented by
Many to one Cardinality.
One to Many Cardinalities:
In One-to-many cardinality mapping, from set 1, there can be a maximum single set
that can make relationships with a single or more than one entity of set 2. Or we can
also describe it as from set 2, more than one entity can make a relationship with only
one entity of set 1.
It is represented by M: N or N: M.
One to one cardinality, One to many cardinalities, and Many to one cardinality is the
subset of the many to many cardinalities.
For Example, in a college, multiple students can work on a single project, and a
single student can also work on multiple projects. So, the relationship between the
project and the student can be represented by many to many cardinalities.
Appropriate Mapping Cardinality
Evidently, the real-world context in which the relation set is modeled determines the
Appropriate Mapping Cardinality for a specific relation set.
o We can combine relational tables with many involved tables if the Cardinality is
one-to-many or many-to-one.
o One entity can be combined with a relation table if it has a one-to-one
relationship and total participation, and two entities can be combined with
their relation to form a single table if both of them have total participation.
o We cannot mix any two tables if the Cardinality is many-to-many.
Keys
o Keys play an important role in the relational database.
o It is used to uniquely identify any record or row of data from the table. It is also used
to establish and identify relationships between tables.
For example, ID is used as a key in the Student table because it is unique for each
student. In the PERSON table, passport_number, license_number, SSN are keys since
they are unique for each person.
Types of keys:
1. Primary key
o It is the first key used to identify one and only one instance of an entity uniquely. An
entity can contain multiple keys, as we saw in the PERSON table. The key which is
most suitable from those lists becomes a primary key.
o In the EMPLOYEE table, ID can be the primary key since it is unique for each
employee. In the EMPLOYEE table, we can even select License_Number and
Passport_Number as primary keys since they are also unique.
o For each entity, the primary key selection is based on requirements and developers.
2. Candidate key
o A candidate key is an attribute or set of attributes that can uniquely identify a tuple.
o Except for the primary key, the remaining attributes are considered a candidate key.
The candidate keys are as strong as the primary key.
For example: In the EMPLOYEE table, id is best suited for the primary key. The rest of
the attributes, like SSN, Passport_Number, License_Number, etc., are considered a
candidate key.
3. Super Key
Super key is an attribute set that can uniquely identify a tuple. A super key is a
superset of a candidate key.
4. Foreign key
o Foreign keys are the column of the table used to point to the primary key of another
table.
o Every employee works in a specific department in a company, and employee and
department are two different entities. So we can't store the department's information
in the employee table. That's why we link these two tables through the primary key of
one table.
o We add the primary key of the DEPARTMENT table, Department_Id, as a new attribute
in the EMPLOYEE table.
o In the EMPLOYEE table, Department_Id is the foreign key, and both the tables are
related.
5. Alternate key
There may be one or more attributes or a combination of attributes that uniquely
identify each tuple in a relation. These attributes or combinations of the attributes
are called the candidate keys. One key is chosen as the primary key from these
candidate keys, and the remaining candidate key, if it exists, is termed the alternate
key. In other words, the total number of the alternate keys is the total number of
candidate keys minus the primary key. The alternate key may or may not exist. If
there is only one candidate key in a relation, it does not have an alternate key.
For example, employee relation has two attributes, Employee_Id and PAN_No, that
act as candidate keys. In this relation, Employee_Id is chosen as the primary key, so
the other candidate key, PAN_No, acts as the Alternate key.
6. Composite key
Whenever a primary key consists of more than one attribute, it is known as a
composite key. This key is also known as Concatenated Key.
For example, in employee relations, we assume that an employee may be assigned
multiple roles, and an employee may work on multiple projects simultaneously. So
the primary key will be composed of all three attributes, namely Emp_ID, Emp_role,
and Proj_ID in combination. So these attributes act as a composite key since the
primary key comprises more than one attribute.
7. Artificial key
The key created using arbitrarily assigned data are known as artificial keys. These
keys are created when a primary key is large and complex and has no relationship
with many other relations. The data values of the artificial keys are usually numbered
in a serial order.
For example, the primary key, which is composed of Emp_ID, Emp_role, and Proj_ID,
is large in employee relations. So it would be better to add a new virtual attribute to
identify each tuple in the relation uniquely.
Generalization
o Generalization is like a bottom-up approach in which two or more entities of lower
level combine to form a higher level entity if they have some attributes in common.
o In generalization, an entity of a higher level can also combine with the entities of the
lower level to form a further higher level entity.
o Generalization is more like subclass and superclass system, but the only difference is
the approach. Generalization uses the bottom-up approach.
o In generalization, entities are combined to form a more generalized entity, i.e.,
subclasses are combined to make a superclass.
For example, Faculty and Student entities can be generalized and create a higher
level entity Person.
Specialization
o Specialization is a top-down approach, and it is opposite to Generalization. In
specialization, one higher level entity can be broken down into two lower level
entities.
o Specialization is used to identify the subset of an entity set that shares some
distinguishing characteristics.
o Normally, the superclass is defined first, the subclass and its related attributes are
defined next, and relationship set are then added.
Aggregation
In aggregation, the relation between two entities is treated as a single entity. In
aggregation, relationship with its corresponding entities is aggregated into a higher
level entity.
For example: Center entity offers the Course entity act as a single entity in the
relationship which is in a relationship with another entity visitor. In the real world, if a
visitor visits a coaching center then he will never enquiry about the Course only or
just about the Center instead he will ask the enquiry about both.
In the database, every entity set or relationship set can be represented in tabular
form.
In the given ER diagram, LECTURE, STUDENT, SUBJECT and COURSE forms individual
tables.
In the STUDENT table, Age is the derived attribute. It can be calculated at any point
of time by calculating the difference between current date and Date of Birth.
Using these rules, you can convert the ER diagram to tables and columns and assign
the mapping between the tables. Table structure for the given ER diagram is as
below:
2. **Simplicity**: ER diagrams use simple and intuitive notations, such as boxes for
entities, lines for relationships, and diamonds for attributes, making them accessible
to both technical and non-technical audiences.
### Limitations:
Despite these limitations, ER diagrams remain a valuable tool for database designers
to conceptualize, communicate, and design database systems effectively. They are
often supplemented with other modeling techniques and documentation to address
their shortcomings and provide a more comprehensive understanding of the
database design.
Enhanced ER Model
ER Diagram
ER diagram stands for Entity Relationship diagram. When we draw the relationships
between entities using a diagram, then it is called an entity relationship diagram. The
ER diagram is just for understanding the purpose of the database administrator. We
cannot use the ER diagram directly on the computer. ER diagrams are converted into
the tabular form then they are inserted into the computer using any query language.
In ER diagrams, we use attributes, entities, and the relationships between entities. We
use an oval shape to represent the entity and a diamond shape to represent the
relationships between entities.
Enhanced ER Diagram
It is getting harder and harder to apply the conventional ER paradigm for database
modeling as data complexity rises today. The existing ER model needs to be
enhanced or improved in order for it to better handle the complicated application in
order to reduce the modeling complexity.
These are two normal kinds of relationships that were added to the normal ER model
for enhancement. These are inspired by the object-oriented paradigm, where we
divide the code into classes and objects, and in the same way, we have divided
entities into subclass and superclasses. Specialized classes are called subclasses, and
generalized classes are called superclasses or base classes. We can learn the concept
of subclass by 'IS-A' analysis. For example, 'Laptop IS-A computer.' Or 'Clerk IS-A
employee.'
We can draw the ER diagram for these relationships. Let's suppose we have a
superclass Employee and subclasses as a clerk, engineer, and lab assistant.
The Enhanced ER diagram of the above example will look like this:
In the above example, we have one superclass and three subclasses. Each subclass
inherits all the attributes from its superclass so that a lab assistant will have all its
attributes, like its name, salary, etc.
Constraints
There are two types of constraints on subclasses which are described below:
o Total or Partial:
A total subclass relationship is one where the union of all the subclasses is equal to
the superclass. It means if every superclass entity has some subclass entity, then it is
called a total subclass relationship. Let's suppose if the union of all the subclasses
( engineer, clerk, lab assistant) is equal to the total employee. Then the relationship is
total. In the above example, it is a total relationship.
If all the entities of a superclass are not associated with a subclass, then it is called a
partial subclass relationship.
o Overlapped or Disjoint:
If any entity from the superclass is associated with more than one subclass, then it is
known as overlapped subclassing, and if it is associated with zero or only one
subclass, then it is called disjoint subclassing.
Note: The above two constraints are independent of each other, and they follow
the transitive property.
Multiple Inheritance
When one subclass is associated with more than one superclass, then this
phenomenon is known as multiple inheritance. In multiple inheritance, the attributes
of the subclass will be the union of all the superclass attributes which are associated
with it. For example, a teacher is a subclass that can be associated with the superclass
of an employee and a superclass of faculty. In the same way, a monitor in the class
can be a subclass of a student superclass as well as an alumni superclass.
UNION
UNION is a different topic from subclassing. Let's suppose we have a vehicle
superclass, and we have two subclasses, car and bike. These two subclasses will
inherit the attributes from the vehicle superclass.
Now we have a UNION of those vehicles which are RTO registered, so we have a
UNION of cars and bikes, but they will inherit all the attributes from the vehicle
superclass.
Specialization:
Generalization:
1. **Inheritance**: Generalization represents the process of combining two or more
subclasses to form a superclass. In other words, it's the reverse of specialization.
Important Terminologies
These are some important terminologies that are used in terms of relation.
Attribute:Attributes are the properties that define a relation.
e.g.; ROLL_NO, NAME etc.
Tuple:Each row in the relation is known as tuple. The above relation
contains 4 tuples, one of which is shown as:
1 RAM DELHI 9455123451 18
Degree:The number of attributes in the relation is known as degree of the
relation. The STUDENT relation defined above has degree 5.
Cardinality:The number of tuples in a relation is known as cardinality.
The STUDENTrelation defined above has cardinality 4.
Column:Column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from relation STUDENT.
SQL, or Structured Query Language, is the foundation for interacting with relational
databases. While it can get complex, the basic structure of an SQL query is actually
quite straightforward. Here's a breakdown of the key components:
1. SELECT Clause: This clause tells the database what specific data you want to
retrieve. You can select individual columns by name, or use an asterisk (*) to select
all columns from a table.
2. FROM Clause: This clause specifies the table(s) that contain the data you're
interested in. You can select data from a single table or join multiple tables together
based on common fields.
3. WHERE Clause (Optional): This clause allows you to filter the results based on
specific conditions. You can use comparison operators (like =, >, <) and logical
operators (AND, OR, NOT) to narrow down the data you retrieve.
Here's an example of a basic SQL query that retrieves all the names (column
"name") from a table called "customers":
SQL
SELECT name
FROM customers;
This is a simple example, but it demonstrates the core structure of SELECT, FROM,
and WHERE clauses. As you get more comfortable with SQL, you can explore
additional functionalities like:
By understanding these fundamentals, you can start building powerful SQL queries
to extract and manipulate data from your relational databases.