DBMS Unit 1 Complete Notes
DBMS Unit 1 Complete Notes
CONTENTS:
INTRODUCTION:
• Data: Data represents known facts or raw information in unorganized form (such as
alphabets or numbers or symbols ) .
or
RDBMS (relational database management system) applications store data in a tabular form and
relation exist among the tables. while DBMS applications stores data in tables but there will be
no “relation” between the tables, like in a RDBMS
Database Applications
1. Banking: all transactions
2. Airlines: reservations, schedules
3. Universities: registration, grades
4. Sales: customers, products, purchases
5. Online retailers: order tracking, customized recommendations
6. Manufacturing: production, inventory, orders, supply chain
7. Human resources: employee records, salaries, tax deductions
8. Databases touch all aspects of our lives
1
Database Management Systems
2
Database Management Systems
Program-Data independence
Data Independence
Efficient data access
Data integrity and security
Data administration
Concurrent access and crash recovery
Reduced application development time
Centralized control of data(data ownership)
Data redundancy eliminated
Data can be shared
Improved data consistency
Improved data integrity
Improved data accessibility and response
Reduced program maintenance
Improved security for data
Multiple access of data
Protection from failure
3
Database Management Systems
Disadvantages of DBMS
Higher cost -More sophisticated hardware and software, higher operating cost
Greater complexity
Backup/recovery procedures complex
DBMS occupy more memory space
Application need to process large amount of data
Database users
o Application Programmers – queries+C ,C++,java
o Sophisticated users-SQL select/create/update/insert etc
o Specialized users – complex programs
o Stand-alone users-
o Native Users -
4
Database Management Systems
Levels of Abstraction
VIEWS OF DATA
Major purpose of database system is to provide users with an abstract view of data
The system hide certain details of how the data are stored and maintained
Physical level
• Lowest level of abstraction
• About how data is stored
• Low level data structures , data storage
• Handled by DBMS
Logical level
• Describe what data is stored and what relationship exist among the data
• Logical data structure
• Independent of any particular technology
• Handled by DBA
View level
• Highest level of abstraction
• Describes only part of database
5
Database Management Systems
Data independence:
Data Independence – The ability to modify a schema definition in one level with out affecting a
schema definition in next higher level is called data independence.
1. Physical data independence: The ability to modify the physical schema without
changing logical schema
2. Logical data independence: The ability to modify the logical or conceptual schema
without changing application programs
In general, the interfaces between the various levels and components should be well defined so
that changes in some parts do not seriously influence others
6
Database Management Systems
Data models
The structure of a database is data model
Hierarchical Model
• Data represented by a collection of records and relationships among data represented by
links
• Records are organized as a collection of trees
• Some data redundancy can be avoided
• Complex data structures are need
• Can not be possible to represent many-to-many relationship
• Data represented in parent –child relationship
• One –to many relationship can easily represented
• Eg: IMS (information management system)
• RAMIS- rapid access management information system
Network model
• Data represented by collection of records and relationship among the data represented
by links
• Records are organized into a collection of graphs
• Data redundancy is minimum
• High degree of complexity
• Represented many –to – many & one- many relation ship
7
Database Management Systems
Relational models
Relational model proposed by Dr.E.F.Codd in 1976
The relational model uses a collection of tables.
Data represented in the form of tables(relations)
Tables are logically equivalent to files and consists of rows
Rows called tuples
Rows have fixed number of columns called attributes
Relational database
• Any database for which the logical organization is based on relational data models
• RDBMS: A DBMS that manages the relational database
• An RDBMS is a type of DBMS that stores data in the form of related tables
• The relational model expresses the data and relationship among the data in the form of
TABLES.
• Relational Data Model was first proposed by Ted Codd of IBM in the 1970s
• The database is a set of related relations (table of values).
• Each relation has a name which indicates what type of tuples the relation has. For
example, a relation named ‘Student’ indicates that it has student entities in it.
Each relation has a set of attributes (column names) which represents, what type of
information, the entities or tuples have?
• A tuple (row) in a relation, is a real-world entity, it has a set of values for corresponding
attributes.
Each data value in a row or tuple is called field.
8
Database Management Systems
A Database system is partitioned into modules that deal with each of the responsibilities of the
overall system.
Storage Manager
A 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. The
storage manager is responsible for the interaction with the file manager. The raw data are stored
on the disk using the file system, which is usually provided by a conventional operating system.
9
Database Management Systems
The storage manager translates the various DML statements into low level file system
commands. Thus, the storage manager is responsible for storing, retrieving and updating data in
the database.
The storage manager components include:
Authorization and integrity manager
Which tests for the satisfaction of integrity constraints and checks the authority of users to
access data.
Transaction manager
Which ensures that the database remains in a consistent correct) state despite system failures,
and that concurrent transaction executions proceed without conflicting.
File Manager
Which manages the allocation of space on disk storage and the data structures used to represent
information stored on disk.
Buffer manager
Which is responsible for fetching data from disk storage into main memory, and deciding what
data to cache in main memory. The buffer manager is a critical part of the database system,
since it enables the database to handle data sizes that are much larger than the size of main
memory.
The storage manager implements several data structures as part of the physical system
implementation:
Data files, which store the database itself.
Data dictionary, which stores metadata about the structure of the database, in particular
the schema of the database.
Indices, which provide fast access to data items that hold particular values.
A query can usually be translated into any of a number of alternative evaluation plans that all
give the same result. The DML compiler also performs query optimization, that is it picks the
lowest cost evaluation plan from among the alternatives.
o Query evaluation engine, which executes low level instructions generated by the
DML compiler.
10
Database Management Systems
11
Database Management Systems
Syntax :
Create table <table name > ( column_name1 type(size), Column_name2 type(size), …);
Example
COMMAND :ALTER
This command is used to add a column or to increase the size of the column
Syntax:
alter table <table name> add/modify (column data type(size));
Example
COMMAND: DROP
This command is used to delete the table from database.
12
Database Management Systems
Syntax:
drop table <tablename>; Example
COMMAND :TRUNCATE
This command is used delete the rows but structure remains
Syntax:
truncate table <table name>; example
COMMAND :INSERT
Inserting a single row into a table
Syntax:
insert into <table name> values (value list);
SQL> insert into s values(‘s3’,’sup3’,’blore’,10)
1 row created.
SQL> /
Enter value for empno: 24
Enter value for ename: bbb
Enter value for job: manager
Enter value for mgr: 46
Enter value for hirdate: 13-jan-89
Enter value for sal: 6000
13
Database Management Systems
1 row created.
OUTPUT:
emp table
EMPNO ENAME JOB MGR HIREDATE SAL COMM. DEPTNO
----------- ------------ ---------------- -------- ----------------------- ------- ------------ --------------
7369 SMITH CLERK 7902 17 – DEC – 80 800 20
7499 ALLEN SALESMAN 7698 20 – FEB – 81 1600 300 30
7521 WARD SALESMAN 7698 22 – FEB – 81 1250 500 30
7566 JONES MANAGER 7839 02 – APR – 81 2975 20
7654 MARTIN SALESMAN 7698 28 – SEP – 81 1250 1400 30
7698 BLAKE MANAGER 7839 01 – MAY – 81 2850 30
7782 CLARK MANAGER 7839 09 – JUN – 81 2450 10
7788 SCOTT ANALYST 7566 19 – APR – 87 3000 20
7839 KING PRESIDENT 17 – NOV – 81 5000 10
7844 TURNER SALESMAN 7698 08 – SEP – 81 1500 0 30
7876 ADAMS CLERK 7788 23 - MAY – 87 1100 20
7900 JAMES CLERK 7698 03 – DEC – 81 950 30
7902 FORD ANALYST 7566 03 – DEC – 81 3000 20
7934 MILLER CLERK 7782 23 – JAN – 82 1300 10
COMMAND : SELECT
To retrieve all rows from a table
Syntax:
SQL>Select * from table name;
INPUT:
SQL>Select * from emp;
OUTPUT:
EMPNO ENAME JOB MGR HIREDATE SAL COMM. DEPTNO
14
Database Management Systems
INPUT:
SQL> select ename, deptno, sal from emp;
OUTPUT:
ENAME DEPTNO SAL
---------- ------- ------- ------
adams 20 1100
allen 30 1600
blake 30 2850
clark 10 2450
ford 20 3000
james 30 950
jones 20 2975
KEYWORD : DISTINCT
Elimination of duplicates from the select clause: It prevents retrieving the duplicated values.
Distinct keyword is to be used.
Syntax:
SQL>Select DISTINCT col1, col2 from table name;
INPUT:
SQL>select distinct job from emp;
OUTPUT:
JOB
------
15
Database Management Systems
CLERK
SALESMAN
MANAGER
ANALYST
PRESIDENT
To select specific rows from a table we include ‘where’ clause in the select command. It can
appear only after the ‘from’ clause
Syntax:
Select column_name1, …..,column_name from table name where condition;
INPUT:
SQL> select ename, job from emp where job = 'manager';
OUTPUT:
ENAME JOB
---------- ---------
jones manager
blake manager
clark manager
COMMAND: UPDATE
To modify column or group of columns
Syntax:
update <tablename> set field=values where condition;
INPUT:
SQL>update std set branch='mca' where sno=03;
1 row updated.
sql> select * from std;
OUTPUT:
SNO SNAME BRANCH
---------- ---------- ----------
1 ram B.Tech
2 ravi B.Tech
3 krish mca
4 rfde B.Tech
COMMAND :DELETE
The command is used to delete the rows in a table
Syntax:
Delete from <table_name> where conditions;
16
Database Management Systems
INPUT:
SQL> delete from emp where empno=7369;
COMMAND : COMMIT
Syntax:
commit [work] [comment 'comment_text']
commit [work] [force 'force_text' [,int] ]
INPUT:
SQL>commit;
OUTPUT:
Commit complete;
COMMAND : ROLLBACK
The ROLLBACK command is the transactional control command to undo the transactions that
have not already been committed to the database. The ROLLBACK command can be issued to
undo the changes since the last COMMIT or ROLLBACK.
Syntax:
ROLLBACK [WORK] [TO [SAVEPOINT]'savepoint_text_identifier'];
ROLLBACK [WORK] [FORCE 'force_text'];
INPUT:
SQL>rollback;
OUTPUT:
Rollback complete;
17
Database Management Systems
INTEGRITY CONSTRAINTS
data integrity refers to maintaining and assuring the accuracy and consistency of data over its
entire life-cycle. Integrity constraints are used to ensure accuracy and consistency of data in a
relational database.
• Domain Integrity
• Key Constraints
Domain Integrity-
• Domain integrity means the definition of a valid set of values for an attribute.
18
Database Management Systems
• You define data type, length or size, It also allows null value. It is the value unique or not
for an attribute ,the default value, the range (values in between) and/or specific values
for the attribute
• This rule states that in any database relation value of attribute of a primary key can't be
null.
Key Constraints
• An attribute or set of attributes that uniquely identifies rows in a table is called as Key
• 1.column-level
The constraints can be specified immediately after the column definition. This is called
column-level definition.
• 2. Table-level
The constraints can be specified after all the columns are defined. This is called table-
level definition
Syntax :
Example:
19
Database Management Systems
);
Unique Key
A UNIQUE key integrity constraint requires that every value in a column or set of columns
(key) be unique--that is, no two rows of a table have duplicate values in a specified
column or set of columns
A column(s) can have a null value but the values cannot be duplicated
Column level:
Syntax
Table level:
Syntax
Example:
or
20
Database Management Systems
);
table level:
Primary key
No two rows of a table have duplicate values in the specified column or set of columns.
The primary key columns do not allow nulls. That is, a value must exist for the primary
key columns in each row.
Syntax:
Note: The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional
column level
);
21
Database Management Systems
Table level:
When multiple fields are used as a primary key, they are called a composite primary key.
Foreign key is the key i.e. attribute which refers to another table primary key.
22
Database Management Systems
Foreign keys help you create logical ties within the information in a database
check constraint
A check constraint can be defined in either an SQL CREATE TABLE statement or an SQL
ALTER TABLE statement.
Syntax:
23
Database Management Systems
CREATE TABLE table_name ( column1 datatype null/not null, column2 datatype null/not
null, ... CONSTRAINT constraint_name CHECK (column_name condition) [DISABLE] );
Example:
DROP Constraint
24
DATABASE DESIGN
Topics
Introduction to database design
Entities, Attributes And Entity Sets
Relationships And Relationship Sets
Additional features of the E-R model
Conceptual design with the E-R model
Conceptual design for large enterprises
1. DATABASE DESIGN
The database design process can be divided into six steps. The ER model is most relevant
to the first three steps. Those six steps are-
i. Requirement Analysis
ii. Conceptual Database Design
iii. Logical Database Design
iv. Schema Refinement
v. Physical Database Design
vi. Application and Security Design
i. Requirements Analysis:
Example: For customer database, data is customer name, customer city, and customer
no.
The information gathered in the requirements analysis step is used to develop a higher-
level description of the data.
The goal of conceptual database design is a complete understanding of the database
structure, meaning (semantics), inter-relationships and constraints. This step is often
carried out using the ER model, or a similar high-level data model.
iii. Logical Database Design:
Under this convert the conceptual database design into a database schema, we must
choose a DBMS to implement our database design.
Beyond ER Design
Once we have logical schema, we must consider performance criteria and design the
physical schema. Finally, we must address security issues and ensure that users are able
to access the data they need, but not data that we wish to hide from them.
iv. Schema Refinement:
Under this, we have to analyze the collection of relations (tables) in our relational
database schema to identify the potential problems and to refine (clear) it.
Physical database design is the process of choosing specific storage structures and
access paths for the database files to achieve good performance for the various database
applications.
This step involves building indexes on some tables and clustering some tables.
vi. Security Design:
In this step, we must identify different user groups and different roles played by various
users.
For each role, and user group, we must identify the parts of the database that they must
be able to access, which are as below.
The ER model is widely used to develop an initial data base design. ER model describes the
physical database model; it is basically useful in the design & communication of the logical
database model.
ENTITY SET:-
An entity set is a collection of similar entities (or) an entity set is a group of similar
objects.
Example: all customers, all account details
ATTRIBUTES
Characteristics of an entity are called as an attribute.
The properties of a particular entity are called as attributes of that specified entity.
Example:
Name, street address, city --- customer database.
Acc-no, balance --- account database.
TYPES OF ATTRIBUTES:
These can be classified into following types.
Simple Attributes.
Descriptive attribute
Composite Attributes.
Single Valued Attributes.
Mutivalued Attributes.
Derived Attributes.
a) Simple Attributes:
The attributes that are not divisible are called as 'simple or atomic attributes'.
Example: cust_name, acc_no
b) Composite Attributes:
The attributes that can be divided into smaller subparts, which represent more basic
attributes with independent meaning.
These are useful to model situations in which a user sometimes refers to the composite
attribute as unit but at other times refers specifically to its components.
Example: Street_address can be divided into 3 simple attributes as Number, Street and
Apartment_no.
Street_address
The attributes having a single value for a particular entity are called as 'Single Valued
Attributes'.
Example: 'adaar number ' is a single valued attribute of 'Person'.
The attributes, which are having a set of values for the same entity, are called as 'Multi
Valued Attributes'.
Example: A 'College Degree' attribute for a person.i.e, one person may not have a
college degree, another person may have one and a third person may have 2 or more
degrees.
e) Derived Attributes:
Descriptive attributes are used to record information about relationship, rather than about
any one of the participating entities.
Examples: since is an attribute in works in relationship capture the information about
participating entities employee & departments.
since
h) Null Attribute:-
It is an attribute that uses a null value when an entity does not have a value for an
attribute.
Relationship & Relationship Set
A relationship is an association among two or more entities
A relationship set is a set of relationships of the same type. A relationship set can be
thought of as a set of n- tuples
f(e1; : : : ; en) j e1 2 E1; : : : ; en 2 Eng
Each n-tuple denotes a relationship involving n entities e1 through en, where entity ei
is in entity set Ei. we show the relationship set Works
since
Rectangles Entities.
Ellipses Attributes.
Lines Links.
Diamonds Relationships.
Dno
Name Street Since Dname
City
Budget
2222 2/2/94 70
3333
3/3/96
1. Many to Many:
An employee is allowed to work in different departments and a department is allowed
to have several employees.
Street Dno
Name City Since Dname
Budget
Works_in Departments
employee
2. One to Many: 1 employee can be associated with many departments, where as each
department can be associated with at most 1 employee as its manager.
employee
Works_in Departments
3.Many to One: Each employee works in at most 1 department.i.e, many employees can work
in same department.
Dno
Name Street Since Dname
City
Budget
Dno
Name Street Since Dname
City
Budget
Role: We indicate roles in E-R diagrams by labeling the lines that connect diamonds to
rectangles.
Role indicator - If an entity set plays more than one role, role indicators describe the different
purpose in the relationship.
Here an example is a single Employee entity set with a relation Reports-To that relates
supervisors and subordinates. Here the emp1,emp2 are entities of employee .
3. ADDITIONAL FEATURES OF THE E-R MODEL
A key is a set of attributes for one entity set such that no two entities in this set agree
on all the attributes of the key.
Sometimes, an additional constraint exists for a given relationship set. For example,
consider a relationship set "Manages" which associates departments with employees. If
a department cannot have more than one manager, this is an example of a one-to-many
relationship set. This type of constraint is called a key constraint.
It is represented in the ER diagrams by drawing an arrow from an entity set E to a
relationship set R when each entity in an instance of E appears in at most one
relationship in (a corresponding instance of) R.
2.Partial.
3.2.1. Total: An entity set dependent on a relationship set and having one to many
relationships is said to be ‘total’. The participation of the entity set ‘departments’ in the
relationship set ‘manages’ is said to be total.
No
Name
Dname Budget
Partial Participation
Employees Manages
Department
Works_in