Introduction to Database Systems Overview
Introduction to Database Systems Overview
What Is a DBMS?
A Database Management System (DBMS) is a software package designed to interact with end-
users, other applications, store and manage databases. A general-purpose DBMS allows the
definition, creation, querying, update, and administration of databases.
DBMS contains information about a particular enterprise
Collection of interrelated data
Set of programs to access the data
An environment that is both convenient and efficient to use
Why Use a DBMS?
A database management system stores, organizes and manages a large amount of information
within a single software application. It manages data efficiently and allows users to perform
multiple tasks with ease.
In the early days, database applications were built directly on top of file systems. A DBMS
provides users with a systematic way to create, retrieve, update and manage data. It is a
middleware between the databases which store all the data and th e users or applications which
need to interact with that stored database. A DBMS can limit what data the end user sees, as
well as how that end user can view the data, providing many views of a single database
schema.
A file processing system is a collection of programs that store and manage files in computer
hard-disk. On the other hand, a database management system is collection of programs that
enables to create and maintain a database. File processing system has more data redundancy, less
data redundancy in dbms.
Application must s t a g e l a r g e d a t a s e t s b e t w e e n m a i n m e m o r y a n d
secondary
Storage (e.g., buffering, page-oriented access, 32-bit addressing, etc.)
Special code for different queries
Must protect data from inconsistency due to multiple concurrent users
Crash recovery
Security and access control
View of Data
A database system is a collection of interrelated data and a set of programs that allow users to
access and modify these data. The main task of database system is to provide abstract view of
data i.e hides certain details of storage to the users.
Data Abstraction:
Major purpose of dbms is to provide users with abstract view of data i.e. the system hides cert
-ain details of how the data are stored and maintained. Since database system users are not
computer trained, developers hide the complexity from users through 3 levels of abstraction, to
simplify user‘s interaction with the system.
Levels of Abstraction
Physical level of data abstraction: Describes how a record (e.g., customer) is stored. This
is the lowest level of abstraction which describes how data are actually stored.
Logical level of data abstraction: The next highest level of abstraction which hides what
data are actually stored in the database and what relations hip exists among them. Describes
data stored in database, and the relationships among the data.
View Level of data abstraction: The highest level of abstraction provides security
mechanism to prevent user from accessing certain parts of database. Application programs
hide details of data types. Views can also hide information (such as an employee‘s salary)
for security purposes and to simplify the interaction with the system.
Summary
Similar to types and variables in programming languages. Database changes over time when
information is inserted or deleted.
Instance – the actual content of the database at a particular point in time analogous to the value
of a variable is called an instance of the database.
Schema – the logical structure of the database called the database schema. Schema is of three
types: Physical schema, logical schema and view schema.
Example: The database consists of information about a set of customers and accounts
and the relationship between them) Analogous t o type information of a variable in a
program
Physical schema: Database design at the physical level is called physical schema. How the
data stored in blocks of storage is described at this level.
Logical schema: database design at the logical level Instances and schemas, programmers and
database administrators work at this level, at this level data can be described as certain types of
data records gets stored in data structures, however the internal details such as implementation
of data structure is hidden at this level.
View schema: Design of database at view level is called view schema. This generally describes
end user interaction with database systems.
Physical Data Independence – The ability to modify the physical schema without changing
the logical schema.
In general, the interfaces between the various levels and components should be well defined so
Conceptual schema:
Course_info(cid:string,enrollment:integer)
Data Independence:
1980s:
1990s:
2000s:
Data Models:
– Data
– Data relationships
– Data semantics
– Data constraints
– Relational model
– Entity-Relationship data model (mainly for database design)
– Object-based data models (Object-oriented and Object-relational)
– Semi structured data model (XML)
– Other older models:
Network model
Hierarchical model
Every relation has a schema, which describes the columns, or fields.
Relational model: The relational model uses a collection of tables to represent both data and
relationships among those data. Each table has multiple columns with unique name.
– It is example of record based model
– These models are structured is fixed-format of several types.
– Each table contains records of particular type
– Each record type defines fixed number of fields, or attributes.
– The columns of the table correspond to attributes of the record type.
The relational data model is the most widely used data model and majority of current database
systems are based on relational model.
Entity-relationship model: The E-R model is based on a perception of real world that
consists of basic objects called entities and relationships among these objects. An entity is a
‗thing‘ or ‗object‘ in the real world, E-R model is widely used in database design.
–What information about these entities and relationships should we store in the
database?
diagrams).
ER Model:
Entity: Real-world objects distinguishable from other objects. An entity is described (in
DB) using a set of attributes.
– All entities in an entity set have the same set of attributes. (Until we consider
ISA hierarchies, anyway!)
–An n-ary relationship set R relates n entity sets E1 ... En; each relationship in R
involves entities e1 E1, ..., en En
Same entity set could participate in different relationship sets, or in different ―roles‖ in
same set.
Modeling:
–a collection of entities,
An entity set is a set of entities of the same type that share the same properties.
Attributes:
Attribute types:
Composite Attributes
Express the number of entities to which another entity can be associated via a relationship
set.
For a binary relationship set the mapping cardinality must be one of the following
types:
–One to one
–One to many
–Many to one
–Many to many
Mapping Cardinalities:
Note: Some elements in A and B may not be mapped to any elements in the other set
Mapping Cardinalities
Note: Some elements in A and B may not be mapped to any elements in the other set
Relationships and Relationship Sets
entity sets
– Example:
For instance, the depositor relationship set between entity sets customer and account
Relationship sets that involve two entity sets are binary (or degree two). Generally,
most relationship sets in a database system are binary.
A weak entity can be identified uniquely only by considering the primary key of another
(owner) entity.
Owner entity set and weak entity set must participate in a one-to-many
Weak entity set must have total participation in this identifying relationship set.
An entity set that does not have a primary key is referred to as a weak entity set.
The existence of a weak entity set depends on the existence of a identifying entity set
it must relate to the identifying entity set via a total, one-to-many relationship
The discriminator (or partial key) of a weak entity set is the set of attributes that
The primary key of a weak entity set is formed by the primary key of the strong entity
set on which the weak entity set is existence dependent, plus the weak entity set‘s
discriminator.
depict a weak entity set by double rectangles.
Note: the primary key of the strong entity set is not explicitly stored with the weak
If loan_number were explicitly stored, payment could be made a strong entity, but then
the relationship between payment and loan would be duplicated by an implicit
relationship defined by the attribute loan_number common to payment and loan
More Weak Entity Set Examples
weak entity
attribute.
Then the relationship with course would be implicit in the course_number attribute
Aggregation
However, some works_on relationships may not correspond to any manages relationships.
So we can‘t discard the works_on relationship
Design choices:
Depends upon the use we want to make of address information, and the semantics of the
data:
If we have several addresses per employee, address must be an entity (since attributes cannot
be set-valued).
If the structure (city, street, etc.) is important, e.g., we want to retrieve employees in a given
city, address must be modeled as an entity (since attribute values are atomic).
An example in the other direction: a ternary relation Contracts relates entity sets Parts,
Departments and Suppliers, and has descriptive at tribute qty. No combination of binary
. relationships is an adequate substitute:
S ―can-supply‖ P, D ―needs‖ P,and D ―deals-with‖ S does not imply that D
has agreed to buy P from S.
–How do we record qty?
arity.
– Schema : specifies name of relation, plus name and type of each column. E.G.
Students (sid: string, name: string, login: string, age: integer, gpa: real).
Can think of a relation as a set of rows or tuples (i.e., all rows are distinct).
Example Instance of Students Relation
Relational Query Languages A major strength of the relational model: supports simple,
powerful querying of data.
Queries can be written intuitively, and the DBMS is responsible for efficient evaluation.
– Allows the optimizer to extensively re-order operations, and still ensure that the answer does
not change.
Creating Relations in SQL
Creates the Students relation. Observe that the type of each field is specified, and enforced
by the DBMS whenever tuples are added or modified.
As another example, the Enrolled table holds information about courses that students take.
IC: condition that must be true for any instance of the database; e.g., domain constraints.
If the DBMS checks ICs, stored data is more faithful to real -world meaning.
No two distinct tuples can have same values in all key fields, and
– If there‘s >1 key for a relation, one of the keys is chosen (by DBA) to be the
primary key.
E.g., sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey.
Possibly many candidate keys (specified using UNIQUE), one of which is chosen as the
primary key.
Foreign key : Set of fields in one relation that is used to `refer‘ to a tuple in another
relation. (Must correspond to primary key of the second relation.) Like a `logical
pointer‘.
E.g. sid is a foreign key referring to Students:
Foreign Keys in SQL
Only students listed in the Students relation should be allowed to enroll for courses.
Consider Students and Enrolled; sid in Enrolled is a foreign key that references
Students.
(Reject it!)
– (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null,
– SET NULL / SET DEFAULT (sets foreign key value of referencing tuple)
ICs are based upon the semantics of the real-world enterprise that is being described in the
database relations.
We can check a database instance to see if an IC is violated, but we can NEVER infer
that an IC is true by looking at an instance.
– From example, we know name is not a key, but the assertion that sid is a key is
given to us.
Key and foreign key ICs are the most common; more general ICs supported too.
Data Control Language (DCL): It is used to control privilege in database. To perform any
operations like creating tables, view and modifying we need privileges which are of two types.
CONNECTING TO ORACLE:
Provide roles:
Provide privileges:
All DDL commands are auto-committed. That means it saves all the changes permanently in
the database.
Command Description
CREATE command:
create is a DDL command used to create a table or a database.
Creating a database
To create a database in RDBMS, create command is uses. Following is the Syntax,
Create database database-name;
Example for creating database
Create database Test;
The above command will create a database named Test.
Creating a table
create command is also used to create a table. We can specify names and datatypes of various
columns along. Following is the Syntax,
create table table-name
{
Column-name1 datatype1,
Column-name2 datatype2,
Column-name1 datatype3,
Column-name2 datatype4
};
Create table command will tell the database system to create a new table with given table
name and column information.
Example for creating table
Create table Student(id int, name varchar, age int);
The above command will create a new table Student in database system with 3 columns,
namely id, name and age.
ALTER command
alter command is used for alteration of table structures. There are various uses
of alter command, such as,
to add a column to existing table
to rename any existing column
to change datatype of any column or to modify its size.
Alter is also used to drop a column.
To add column to existing table
Using alter command we can add a column to an existing table. Following is the Syntax,
Alter table table-name add(column-name datatype);
alter command can add a new column to an existing table with default values. Following is the
Syntax,
alter table table_name add (column_name datatype default data);
To rename a column
Using alter command you can rename an existing column. Following is the Syntax,
alter table table-name rename old-column-name to new-column-name;
Here is an Example for this,
alter table Student rename address to Location;
The above command will rename address column to Location.
To drop a column
alter command is also used to drop columns also. Following is the Syntax,
alter table table-name drop(column-name);
Here is an Example for this,
alter table Student drop(address);
The above command will drop address column from the Student table
truncate command is different from delete command. delete command will delete all the rows
from a table whereas truncate command re-initializes a table(like a newly created table).
eg. If you have a table with 10 rows and an auto_increment primary key, if you use delete
command to delete all the rows, it will delete all the rows, but will not initialize the primary
key, hence if you will insert any row after using delete command, the auto_increment primary
key will start from 11. But in case of truncatecommand, primary key is re-initialized.
drop command
drop query completely removes a table from database. This command will also destroy the
table structure. Following is its Syntax,
drop table table-name;
Here is an Example explaining it.
drop table Student;
The above query will delete the Student table completely. It can also be used on Databases.
For Example, to drop a database,
drop database Test;
The above query will drop a database named Test from the system.
rename query
rename command is used to rename a table. Following is its Syntax,
rename table old-table-name to new-table-name
Here is an Example explaining it.
rename table Student to Student-record;
The above query will rename Student table to Student-record.
DML COMMANDS:
INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
insert into table_name values(data1,data2,…….);
Lets see an example,
Consider a table Student with following fields.
101 Adam 15
101 Adam 15
102 Alex
101 Adam 15
102 Alex
103 chris 14
Suppose the age column of student table has default value of 14.
Also, if you run the below query, it will insert default value into the age column, whatever the
default value may be.
INSERT into Student values(103,'Chris');
UPDATE command
Update command is used to update a row of a table. Following is its general syntax,
UPDATE table-name set column-name = value where condition;
Lets see an example,
update Student set age=18 where s_id=102;
101 Adam 15
102 Alex 18
103 chris 14
101 Adam 15
102 Alex 18
103 Abhi 17
3) Delete command
Delete command is used to delete data from a table. Delete command can also be used with
condition to delete a particular row. Following is its general syntax, DELETE from table-
name;
Example to Delete all Records from a Table
DELETE from Student;
The above command will delete all the records from Student table.
101 Adam 15
102 Alex 18
103 Abhi 17
Adam 15
101
102 Alex 18
Commit command
Commit command is used to permanently save any transaaction into database.
Following is Commit command's syntax,
commit;
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction. Following is Rollback command's
syntax,
rollback to savepoint-name;
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to
that point whenever necessary.
Following is savepoint command's syntax,
savepoint savepoint-name;
ID NAME
1 abhi
2 adam
4 alex
Lets use some SQL queries on the above table and see the results.
INSERT into class values(5,'Rahul');
commit;
UPDATE class set name='abhijit' where
id='5'; savepoint A;
INSERT into class values(6,'Chris');
savepoint B;
INSERT into class values(7,'Bravo');
savepoint C;
SELECT * from class;
The resultant table will look like,
Now rollback to savepoint B
rollback to B;
SELECT * from class;
The resultant table will look like
Now rollback to savepoint A
rollback to A;
SELECT * from class;
The result table will look like
DCL command
be sent to a database.
Customer:
Example: Find the name of the customer with customer -id 192-83-7465
SQL>select customer.customer_name
Example: Find the balances of all accounts held by the customer with customer -Id 192-83-
7465.
SQL>select [Link]
from depositor,account
where depositor.customer_id=‗192-83-7465‘and
depositor.account_number = account.account_number;
Database Architecture:
The architecture of a database systems is greatly influenced by the underlying com puter system
on which the database is running:
Centralized
Client-server
Distributed
Overall System Structure
A database system is partitioned into modules that deal with each of the responsibilities of the
overall system. The functional components of the database system are
– Storage management
– Query processing
– Transaction processing
Storage Management
Storage manager is a program module that provides the interface between the low -level
data stored in the database and the application programs and queries submitted to the
system.
– Storage access
– File organization
– Indexing and hashing
Query Processing
– Equivalent expressions
Cost difference between a good and a bad way of evaluating a query can be enormous
– Depends critically on statistical information about relations which the database must
maintain
– Need to estimate statistics for intermediate results to compute cost of complex
expressions
Database Users
Users are differentiated by the way they expect to interact with the system
Specialized users – write specialized database applications that do not fit into the
Naïve users – invoke one of the permanent application programs that have been written
previously
– Examples, people accessing database over the web, bank tellers, clerical staff
Database Administrator
–Backing up data
–Database tuning.