Unit 1
Unit 1
2
What is a DBMS?
Data isolation
-Multiple files and formats
Integrity problems
-Values in the database must satisfy constraints
-Hard to add new constraints or change existing ones 5
Atomicity of updates
-Failures may leave database in an inconsistent state with
partial updates carried out
-Example: Transfer of funds from one account to another
should either complete or not happen at all (atomic)
Security problems
-Hard to provide user access to some, but not all, data
6
Database systems offer solutions to all the above problems
View of Data
Data Abstraction
Looking at the necessary information
Logical level
Physical level
8
Physical level
Logical level
View level
10
Example:
struct customer
{
customer_id: string;
customer_name: string;
customer_street: string;
customer_city: string;
};
11
At the logical level, each such record is described by a
structure definition, as shown above
12
The views also provide a security mechanism to prevent
users from accessing certain parts of the database
13
The overall design of the database is called the database
schema
Data Models
16
Types of Data Models
Relational Model
Network model
Hierarchical model
17
Relational Model
Example
18
The relational model is an example of a record-based
model
20
For example, the attributes account-number and
balance may describe one particular account in a bank,
and they form attributes of the account entity set
21
The set of all entities of the same type is called an entity
set
Figure 1.3 23
Object-based data models (Object-oriented and Object-
relational)
In Object-oriented data model data is represented in the
form of objects (OOP principles can be applied among
the data)
Object-relational data model is essentially a relational
model with object-oriented features
-A field may also contain an object with attributes and
operations
-Complex objects can be stored in relational tables
Network model
25
22 Dustin 7 45.0 101 10/10/98
Hierarchical model
26
22 Dustin 7 45. 31 Lubber 8 55.
0 5
101 10/10/98 102 11/10/98
Database Languages
-Data Definition Language (DDL)
-Data Manipulation Language (DML)
-Data Control Language (DCL)
-Data Transaction Language (DTL)
29
DDL Commands
-CREATE
-ALTER
-DROP
-TRUNCATE
CREATE command
Syntax:
CREATE TABLE table-name (Fieldname1 data_type,
Fieldname2 data_type , …. ….. ….)
30
Example
CREATE TABLE Sailors (sid NUMBER(2), sname
VARCHAR2(20), rating NUMBER(2))
ALTER command
31
Modifying an existing field
Example
ALTER TABLE Sailors MODIFY (age NUMBER(3,1))
Deleting an existing field
32
DROP command
TRUNCATE command
33
Data-Manipulation Language
A data-manipulation language (DML) is a language
that enables users to access or manipulate data
Data manipulation is
--The retrieval of information stored in the database
--The insertion of new information into the database
--The deletion of information from the database
--The modification of information stored in the database
DML Commands
-INSERT
-DELETE
-UPDATE
-SELECT
35
INSERT command
36
Inserting records from another table
Syntax: INSERT INTO table_name1 SELECT * FROM
table_name2
UPDATE command
For modifying attribute values of (some) tuples in a table
Syntax: UPDATE tablename SET column1=value1,…,
columnn=valuen WHERE condition
Example: UPDATE Sailors SET age=34.5 WHERE sid=22
DELETE command
Removing specified rows from a table
Syntax: DELETE FROM tablename WHERE condition
Example: DELETE FROM Sailors WHERE sid=22
37
Removing all rows from a table
Syntax: DELETE FROM tablename
Example: DELETE FROM Sailors
Data-Control Language
38
DCL Commands
-GRANT
-REVOKE
GRANT Command
Syntax:
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC}
39
privilege_name is the access right or privilege granted
to the user. Some of the access rights are ALL,
EXECUTE, and SELECT
40
Example:
REVOKE Command:
41
Syntax:
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC}
Example:
REVOKE SELECT ON employee FROM user1;
42
Data-Transaction Language
DTL Commands
-COMMIT
-ROLLBACK
COMMIT Command
COMMIT;
Example:
44
DELETE FROM CUSTOMERS WHERE AGE = 25;
COMMIT;
Thus, two rows from the table would be deleted and the
SELECT statement would produce the following result.
45
ROLLBACK Command
Syntax:
ROLLBACK;
Example:
46
DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
Thus, the delete operation would not impact the table and
the SELECT statement would produce the following
result.
47
Database Access for Application Programs
49
--Usually, a special character prefaces DML calls, and
a preprocessor, called the DML precompiler, converts the
DML statements to normal procedure calls in the host
language
50
Database Users and User Interfaces
52
2.Application programmers: computer professionals
who write application programs
A person who has central control over both the data and
the programs that access the data is called a database
administrator (DBA)
57
-Routine maintenance.
60
Isolation is the responsibility of concurrency-control
manager to control the interaction among the concurrent
transactions
61
Database
System
Structure
63
The storage manager components include:
67
Database Design
We must find out what the users want from the database
69
(2) Conceptual Database Design:
71
Schema can be refined using normalization
72
This step may simply involve building indexes on some
tables and clustering some tables, or
73
For each role and user group, assign the privileges
74
ENTITIES, ATTRIBUTES, AND ENTITY SETS
75
An entity is described using a set of attributes
76
Ex: Domain values for attribute rating consists of
integers 1 through 10
77
The Employees entity set with attributes ssn, name, and
lot is shown in Figure 2.1
78
RELATIONSHIPS AND RELATIONSHIP SETS
81
Descriptive attributes are used to record information about
the relationship, rather than about any one of the
participating entities
82
In the Works_In relationship set, for example, each
Works_In relationship must be uniquely identified by the
combination of employee ssn and department did
83
84
As another example of an ER diagram, suppose that each
department has offices in several locations and we want to
record the locations at which each employee works
85
86
For example, consider the Reports_To relationship set that
is shown in Figure 2.5
87
If an entity set plays more than one role, the role indicator
concatenated with an attribute name from the entity set
gives us a unique name for each attribute in the relationship
set 88
For example, the Reports_To relationship set has attributes
corresponding to the ssn of the supervisor and the ssn of the
subordinate, and the names of these attributes are
supervisor_ssn and subordinate_ssn
89
ADDITIONAL FEATURES OF THE ER MODEL
Key Constraints:
91
92
A relationship set like Manages is sometimes said to be
one-to-many
93
Key Constraints for Ternary Relationships
94
95
An instance of the Works_In3 relationship set is shown in
Figure 2.9
96
97
Participation Constraints
98
In the Works_In relationship set (Fig 2.3), the participation
of both Employees and Departments is total
99
100
Weak Entities
Entity set which does not have a key is called weak entity
set
101
The following restrictions must hold:
-The owner entity set and the weak entity set must
participate in a one-to-many relationship set. This
relationship set is called the identifying relationship set of
the weak entity set
102
For example, a Dependents entity can be identified
uniquely by key ssn of Employees entity set and the
attribute pname of the Dependents entity set
104
To indicate that pname is a partial key for Dependents, we
underline it using a broken line
Class Hierarchies
105
We might have attributes hours_worked and hourly_wages
defined for Hourly_Emps and an attribute contractid defined
for Contract_Emps
108
Overlap constraints determine whether two subclasses are
allowed to contain the same entity
Aggregation
Use aggregation when we need to express a relationship
among relationships