Unit II Dbms Notes
Unit II Dbms Notes
Example:
Different Types of Integrity Constraints that are enforced with SQL are listed below
1
1. Domain Constraint
Ensures that values in a column belong to a predefined domain (data type, range,
format).
Example:
Ensures that every table has a primary key and that key cannot be NULL.
Example:
Ensures foreign key values in a table refer to valid primary key values in another
table.
Example:
CHECK Constraint
2
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
Price DECIMAL(10,2) CHECK (Price > 0)
);
The Price must always be greater than 0.
DEFAULT Constraint
RELATIONAL KEYS.
The relational keys in DBMS are explained below.
Primary key
An attribute or combination of attributes that have unique
identification property is called primary key. It cannot contain null
values and repeated values.
Eg. STU_NUM in STUDENT Table is the primary key.
Composite primary key
Not every relation will have single-attribute primary key. There can
be a possibility that some combination of attributes have the
unique identification property. These attributes as a group is called
composite primary key.
Candidate key
In a relation, there can be more than one attribute combination
possessing the unique identification property. These combinations,
which can act as primary key, are called candidate keys.
3
1013 1056300 Larry 24
SQL commands are categorized into different types based on their functionality. The main
categories include:
DML commands are used to insert, update, delete, and retrieve data from database
tables.
4
Comman
Description Example
d
table.
Removes specific records from a
DELETE DELETE FROM Students WHERE ID = 1;
table.
DCL commands are used to control access to data within the database.
Logical database design is the process of organizing data into tables that reflect a business's
operations. It's a key step in creating efficient and organized databases
5
Step 1: Mapping of Regular Entity Types
Each regular (strong) entity type in the ER model is transformed into a relation that includes
all its simple attributes. A primary key is selected, and composite keys are represented as
multiple attributes. Example: The EMPLOYEE, DEPARTMENT, and PROJECT entities in the
COMPANY database are converted into corresponding relations with SSN, DNUMBER, and
PNUMBER as their primary keys.
Weak entities rely on strong entities and do not have sufficient attributes to form a primary
key. They are mapped into a relation with all their attributes and a foreign key referencing
the owner entity. The primary key is a combination of the foreign key and the partial key of
the weak entity. Example: The DEPENDENT entity is mapped, where ESSN (foreign key)
references the EMPLOYEE relation.
Foreign Key Approach: One entity's primary key is included as a foreign key in the
other.
Merged Relation Approach: The two entities and their relationship are merged into
a single relation if both participations are total.
Cross-Reference Table: A new relation is created to reference both entities.
Example: The MANAGES relationship is mapped by choosing DEPARTMENT as the
entity that stores the foreign key.
6
For a one-to-many (1:N) relationship, the relation representing the entity at the N-side
includes a foreign key referencing the other entity’s primary key. Simple attributes of the
relationship are added to the relation. Example: The WORKS_FOR relationship is mapped by
including DNUMBER as a foreign key (DNO) in the EMPLOYEE relation.
A many-to-many (M:N) relationship is mapped into a new relation that stores foreign keys
from the participating entities. The combination of these foreign keys serves as the primary
key. Example: The WORKS_ON relation is created with ESSN and PNO as foreign keys
referencing EMPLOYEE and PROJECT, respectively.
Multivalued attributes require a separate relation containing the attribute and a foreign key
referencing the entity. The combination of both forms the primary key. Example: The
DEPT_LOCATIONS relation is created with DLOCATION (multivalued attribute) and DNUMBER
(foreign key referencing DEPARTMENT) as its primary key.
For relationships involving more than two entities (n-ary, where n>2), a separate relation is
created. This relation includes foreign keys referencing all participating entities and any
attributes of the relationship. Example: The SUPPLY relationship is mapped into the SUPPLY
relation with SNAME, PARTNO, and PROJNAME as foreign keys forming the primary key.
Example for mapping n-ary relationships. (No. of entities are 3, so it is ternary relationship).
7
Summary of Mapping Constructs and Constraints
The following table highlights the correspondences between ER and relational models:
8
Views
A view is a virtual or derived relation: a relation that does not
necessarily exist in its own right, but may be dynamically derived from
one or more base relations. Thus, an external model can consist of both
base (conceptual- level) relations and views derived from the base
relations.
Base relation
A named relation corresponding to an entity in the conceptual schema,
whose tuples are physically stored in the database.
We can define a view in terms of base relations: The dynamic result of
one or more relational operations operating on the base relations to
produce another relation. A view is a virtual relation that does not
necessarily exist in the database but can be produced upon request by
a particular user, at the time of request.
A view is a relation that appears to the user to exist, can be
manipulated as if it were a base relation, but does not necessarily exist
in storage in the sense that the base relations do. The contents of a
view are defined as a query on one or more base relations. Any
operations on the view are automatically translated into operations on
the relations from which it is derived. Views are dynamic, meaning that
changes made to the base relations that affect the view are
immediately reflected in the view. When users make permitted changes
to the view, these changes are made to the underlying relations
Purpose of Views
The view mechanism is desirable for several reasons:
It provides a powerful and flexible security mechanism by hiding
parts of the database from certain users. Users are not aware of
the existence of any attributes or tuples that are missing from
the view.
It permits users to access data in a way that is customized to
their needs, so that the same data can be seen by different
users in different ways, at the same time.
It can simplify complex operations on the base relations. For
example, if a view is defined as a combination (join) of two
relations users may now perform more simple operations on the
view, which will be translated by the DBMS into equivalent
operations on the join.
A view should be designed to support the external model that the user
finds familiar. For example:
A user might need Branch tuples that contain the names of
managers as well as the other attributes already in Branch. This
view is created by combining the Branch relation with a restricted
form of the Staff relation where the staff position is “Manager.”
Some members of staff should see Staff tuples without the salary
attribute.
Attributes may be renamed or the order of attributes changed.
For example, the user accustomed to calling the branchNo
attribute of branches by the full name Branch Number may see
that column heading.
9
Some members of staff should see property records only for those
properties that they manage.
Updating Views
All updates to a base relation should be immediately reflected in all
views that reference that base relation. Similarly, if a view is updated, then
the underlying base relation should reflect the change. However, there are
restrictions on the types of modification that can be made through views.
We summarize here the conditions under which most systems
determine whether an update is allowed through a view:
Updates are allowed through a view defined using a simple query
involving a
Single base relation and containing either the primary key or a
candidate key of the base relation.
Updates are not allowed through views involving multiple base
relations. Updates are not allowed through views involving
aggregation or grouping operations.
In Relational Database Management Systems (RDBMS), you can destroy
(drop) or alter tables and views using SQL commands.
Destroying (Dropping) Tables and Views
DROP TABLE: Permanently deletes a table along with its structure and all data.
DROP VIEW: Permanently deletes a view but does not affect the underlying tables.
Rename a column:
10
Delete a column:
Altering Views
WHERE condition;
CREATE VIEW StudentView AS SELECT StudentID, StudentName FROM Students WHERE age
> 18;
Selection (σ)
Notation:
Description:
o Selects rows (tuples) that satisfy a given condition.
o Filters data from a relation (table).
Example:
Find students older than 18 from the Students table.
Projection (π)
Notation:
Description:
11
Example:
Notation: R×S
Description:
o Combines every tuple of relation R with every tuple of relation S.
o Produces a large result set.
Example:
Students×Courses
Combine Students and Courses tables.
SELECT * FROM Students, Courses;
Union (∪)
Notation: R∪S
Description:
Example:
Get a list of all student names from Students2023 and Students2024 tables.
Description:
Returns tuples from R that are not present in S.
Example:
Find students enrolled in 2023 but not in 2024.
Intersection (∩)
Notation: R∩S
Description:
o Returns tuples common to both relations.
12
Example:
Get students enrolled in both 2023 and 2024.
Description:
Renames a relation.
Example:
Rename the Students relation to Scholars.
13
14
Equivalent SQL query: SELECT * FROM Students WHERE Age > 18;
15
SELECT Name, Age FROM Students WHERE Age > 18;
SELECT Name FROM Students WHERE NOT EXISTS (SELECT * FROM Courses
WHERE CourseID NOT IN (SELECT CourseID
FROM Enrollment WHERE Enrollment.StudentID = Students.StudentID));
16