Professional Documents
Culture Documents
DBMS (Unit 1)
DBMS (Unit 1)
Database
A database is a place/container where all the data is stored or Collection of related data.
Data:
Data are real facts. In a database, even the smallest piece of information becomes data.
For example, Name of a student is data, course is data, and Color is data, height, weight,
food, everything is data.
Need for Data
• To perform various actions on them.
• We can store all the information in the database.
• We can fetch information from the database, if we need to change any information,
we can update/delete them. But all the data will be in one place – Database.
Type of data stored in database
• In a database, related data are grouped together and stored under one group name
called a table.
• This helps in identifying which data is stored where and under what name.
• It reduces the time to search for particular data in a whole database.
For example: Student, Employee, Department, etc form individual tables.
In earlier days, data was stored manually, using pen and paper but after computer was
discovered, the same task could be done by using files. A computer File is a resource which
uniquely records data, in a storage device in a computer. There are various formats in which
data can be stored. For eg., Text files can be stored in .txt format while pictures can be stored
in .png format etc. Each file is placed in relevant folders for the ease of access.
File Processing System (FPS) is a way of storing, retrieving and manipulating data which
is present in various files. All files are grouped based on their categories. The file names are
much related to each other and arranged properly to easily access the files. In file processing
system, if one needs to insert, delete, modify, store or update data, one must know the entire
hierarchy of the files.
Student files for each class were bundled inside different folders to identify it quickly.
• The file processing system is good when there is only a limited number of files and
data are very less.
• As the data and files in the system grow, handling them becomes difficult.
Advantages of File Processing System:
• Cost friendly –
There is a very minimal set up and usage fee for File Processing System.
• Easy to use –
File systems require very basic learning and understanding, hence, can be easily used.
• High scalability –
One can very easily switch from smaller to larger files as per his needs.
3. Data Isolation: Because data are scattered in various files, and files may be in different
formats, writing new application programs to retrieve the appropriate data is difficult.
4. Atomicity Problem: Atomicity of a transaction refers to “All or nothing”, which means
either all the operations in a transaction executes or none.
For example: Let’s say Steve transfers 100$ to Negan’s account. This transaction
consists multiple operations such as debit 100$ from Steve’s account, credit 100$ to
Negan’s account. Like any other device, a computer system can fail. let’s say it fails after
first operation then in that case Steve’s account would have been debited by 100$ but
the amount was not credited to Negan’s account, in such case the rollback of operation
should occur to maintain the atomicity of transaction. It is difficult to achieve atomicity
in file processing systems.
5. Integrity Problem: If we need to check for certain insertion criteria while entering the
data into the file it is not possible directly. We can do it by writing programs. Say, if we
have to restrict the students above age 18, then it is by means of the program alone. There
is no direct checking facility in the file system. Hence these kinds of integrity checks are
not easy in the file systems.
6. Concurrent Access Anomalies: Multiple users accessing the same data from the same
file at a time is called concurrent access. In the file system, concurrent access leads to
incorrect data.
• For example, a student wants to borrow a book from the library. He searches for the
book in the library file and sees that only one copy is available. At the same time,
another student also wants to borrow the same book and checks that one copy available.
The first student opt for borrow and gets the book. But it is still not updated to zero-
copy in the file and the second student also opt for borrow. But there are no books
available. This is the problem of concurrent access to the file system.
7. Data Security: Data should be secured from unauthorised access, for example a student
in a college should not be able to see the payroll details of the teachers, such kind of
security constraints are difficult to apply in file processing systems.
8. Slow access time: Direct access of files is very difficult and one needs to know the
entire hierarchy of folders to get to a specific file. This involves a lot of time.
9. Unauthorized Access: Anyone who gets access to the file can read or modify the
data.
Note –
Database Management System (DBMS) is used to eliminate the disadvantages of the FPS.
• In our example above, DBMS commits the transaction before transfer. If there is any
crash or shutdown of the system, before committing the transfer, transaction will be
rolled back to the original state. Hence it makes sure the atomicity of the transaction is
achieved.
9. Concurrent Access: DBMS provides access to multiple users to access the database
at the same time. It has its own mechanism to have concurrency accesses and hence
avoid any incorrect data in the system.
View of Data in DBMS – Three Level Architecture of a
Database
Abstraction is one of the main features of database systems.
For example: When you are booking a train ticket, you are not concerned how data is
processing at the back end when you click “book ticket”, what processes are happening when
you are doing online payments. You are just concerned about the message that pops up
when your ticket is successfully booked. This doesn’t mean that the process happening at the
back end is not relevant, it just means that you as a user are not concerned what is happening
in the database.
For example: An employee table in database exists with the following attributes:
DBMS Instance
Definition of Instance: The data stored in database at a particular moment of time is called
instance of database. Database schema defines the attributes in tables that belong to a particular
database. The value of these attributes at a moment of time is called the instance of that
database.
For example, we have seen the schema of table “employee” above. Let’s see the table with the
data now. At this moment the table contains two rows (records). This is the current instance of
the table “employee” because this is the data that is stored in this table at this particular moment
of time.
EMP_NAME EMP_ID EMP_ADDRESS EMP_CONTACT
------- ------ ----------- -----------
Chaitanya 101 Noida 95********
Ajeet 102 Delhi 99********
Views of Data
This is the lowest level of data abstraction. It describes how data is actually stored in
database. This level is also known as physical level. This level is also responsible for
allocating space to the data. This is the lowest level of the architecture.
2. Conceptual level / Logical level:
This is the middle level of 3-level data abstraction architecture. It describes what data is
stored in database.
It is also called logical level. The whole design of the database such as relationship among
data, schema of data etc. are described in this level.
Highest level of data abstraction. This level describes the user interaction with database
system. It is also called view level. The reason this level is called “view” is because users can
view their desired data from this level which is internally fetched from database with the help
of conceptual and internal level mapping.
The user doesn’t need to know the database schema details such as data structure, table
definition etc. user is only concerned about data which is returned back to the view level after
it has been fetched from database.
External level is the “top level” of the Three Level DBMS Architecture.
Example: Let’s say we are storing customer information in a customer table. At physical
level these records can be described as blocks of storage (bytes, gigabytes, terabytes etc.) in
memory. These details are often hidden from the programmers.
At the logical level these records can be described as fields and attributes along with their data
types, their relationship among each other can be logically implemented. The programmers
generally work at this level because they are aware of such things about database systems.
At view level, user just interact with system with the help of GUI and enter the details at the
screen, they are not aware of how the data is stored and what data is stored; such details are
hidden from them.
Data Independence
o Logical data independence refers the ability to change the conceptual schema without
having to change the external schema.
o Logical data independence is used to separate the external level from the conceptual
view.
o If we do any changes in the conceptual view of the data, then the user view of the data
would not be affected.
o Logical data independence occurs at the user interface level.
o Physical data independence can be defined as the ability to change the internal schema
without having to change the conceptual schema.
o If we do any changes in the storage size of the database system server, then the
Conceptual structure of the database will not be affected.
o Physical data independence is used to separate conceptual levels from the internal
levels.
o Physical data independence occurs at the logical interface level.
In this type of architecture, the database is readily available on the client machine, any request
made by client doesn’t require a network connection to perform the action on the database.
For example, lets say you want to fetch the records of employee from the database and the
database is available on your computer system, so the request to fetch employee details will be
done by your computer and the records will be fetched from the database by your computer as
well. This type of system is generally referred as local database system.
2. Two tier architecture
In two-tier architecture, the Database system is present at the server machine and the DBMS
application is present at the client machine, these two machines are connected with each other
through a reliable network as shown in the above diagram.
Whenever client machine makes a request to access the database present at server using a query
language like sql, the server perform the request on the database and returns the result back to
the client.
Two tier architecture provides added security to the DBMS as it is not exposed to the end-user
directly. It also provides direct and faster communication.
In the above 2 Tier client-server architecture of database management system, we can see that
one server is connected with clients 1, 2, and 3.
Data Models
A database model is a logical structure of a database, which contains the relationships and
constraints that determine about how data is stored and accessed. The individual database
models are designed based on the rules and concepts.
Types of database models
There are so many database models, but most used database models are −
1. Hierarchical database model
2. Relational model
3. Network model
1. Hierarchical Model
• In Hierarchical Model, a hierarchical relation forms a tree-like structure.
• The relationship can be defined in the form of parent child type.
Example
• The hierarchy shows an Employee can be an Intern, on Contract or Full- Time. Sub-
levels show that Full-Time Employee can be hired as a Writer, Senior Writer or
Editor.
Advantages
• The design of the hierarchical model is simple.
• Provides Data Integrity since it is based on parent/ child relationship
• Data sharing is feasible since the data is stored in a single database.
• Even for large volumes of data, this model works perfectly.
Disadvantages
• Implementation is complex.
• This model has to deal with anomalies like Insert, Update and Delete.
• Maintenance is difficult since changes done in the database may want you to do
changes in the entire database structure.
2. Network Model
• The Hierarchical Model creates hierarchical tree with parent/ child relationship,
whereas the Network Model has graph and links.
• The relationship can be defined in the form of links and it handles many-to-many
relations. This states that a record can have more than one parent.
Example
Advantages
• Easy to design the Network Model
• The model can handle one-one, one-to-many, many-to-many relationships.
3. Relational Model
• A relational model groups data into one or more tables. These tables are related to
each other using common records.
• The data is represented in the form of rows and columns i.e. tables:
Example
• Let us see an example of two relations <Employee> and <Department> linked to
each other, with DepartmentID, which is Foreign Key of <Employee> table and
Primary key of <Department> table.
Advantages
• The Relational Model does not have any issues that we saw in the previous two
models i.e. update, insert and delete anomalies have nothing to do in this model.
• Changes in the database do not require you to affect the complete database.
• Implementation of a Relational Model is easy.
• To maintain a Relational Model is not a tiresome task.
Disadvantages
• Database inefficiencies hide and arise when the model has large volumes of data.
• The overheads of using relational data model come with the cost of using powerful
hardware and devices.
Based on parent child relationship. A record can have many parents as Based on relational data structures.
well as many children.
Cannot insert the information of a Does not suffer from any insertion Doesn’t suffer from any insertion
child who does not have any anomaly. anomaly.
parent.
Multiple occurrences of child Free from update anomalies. Free from update anomalies.
records which lead to problems of
inconsistency during the update
operation.
Deletion of parent results in Free from delete anomalies. Free from delete anomalies.
deletion of child record.
This model lacks data There is partial data independence. It provides data independence.
independence.
Overview of SQL
SQL Commands
o SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
• Data Definition Language (DDL) : used to create, alter and delete database objects.
• Data Manipulation Language (DML) : used to insert, modify and delete the data in
the database.
• Data Query Language (DQL) : enables the users to query one or more tables to get
the information they want.
• Data Control Language (DCL) : controls the user access to the database objects.-
Grant and Revoke
• Transaction Control Language (TCL) : manage all the changes made by the DML
statements - commit, savepoint, rollback
SQL Data Types
Example: create table student (regno number(11), name varchar(25), addr varchar(25),
dept varchar2 (3));
Output:
Table created.
Syntax:
alter table <existing-table name> rename to <new-table name>;
Example:
alter table Student rename to Marksheet;
Output:
Table altered.
3. TRUNCATE TABLE COMMAND
• The truncate command deletes all rows from the table. Only the structure of the table
remains.
Syntax: Truncate table <table name>;
Example: Truncate table student;
Output:
Table truncated.
4. DESC COMMAND
• This command will display the structure of the table.
Syntax: Desc <table name>;
Output:
Name Null? Type
---------------------------------- -------- ----------------------------
REGNO NUMBER(11)
NAME VARCHAR2(30)
DEPT VARCHAR2(3)
COMMENTS LONG
Output:
Table dropped.
1. INSERT COMMAND
• Used to add one or more rows to a table.
• While using this command the values are separated by commas and the data types
char and date are enclosed in apostrophes.
• The values must be entered in the same order as they are defined in the table.
Syntax:
Example:
Example:
iii) Syntax:
SQL> Insert into <table_name> Values (&list of Column names,…..);
Example:
SQL> insert into biodata values(&rno,'&sname','&course','&dob','&address','&city',&phno);
2. SELECT COMMAND
• Used to perform a query. The query is a request for information from table in a database.
Syntax: Select [All/Distinct] column_name … from table_name …
[where conditions ]
[group by column_name …]
[having conditions ]
[order by column_name …] [DESC];
• To select only specific columns, specify the column names or use * to select all
columns from the table in the select command.
• The select command lists the items to be retrieved.
• The from clause specifies the table or tables from where the data has to be retrieved.
• The where clause specifies the search criteria.
• The group by clause will group the result set based on the given column.
• The having clause tells SQL to include only certain groups produced by the group
by clause in the query result set. It is used to specify the search criteria or search
condition when group by clause is specified.
• The order by clause sorts or orders the results based on the data in one or more
columns in the ascending or descending order.
3. UPDATE COMMAND
• Used to alter the column values in a table.
• Specific rows could be updated based on a specific condition.
Syntax: Update table_name set field = value, … [where condition];
• The ‘where’ clause and the ‘set’ clause can also include queries.
Example:
4. DELETE COMMAND
• Used to delete one or more rows to a table.
Syntax: Delete from <table_name> [where conditions];
Example:
1. Union
2. Union All
3. Intersect
4. Minus
1. Union
o The SQL Union operation is used to combine the result of two or more SQL SELECT
queries.
o In the union operation, all the number of datatype and columns must be same in both
the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
Example:
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing
duplication and sorting the data.
Syntax:
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
ID NAME
3 Jackson
4. Minus
o It combines the result of two SELECT statements. Minus operator is used to display
the rows which are present in the first query but absent in the second query.
o It has no duplicates and data arranged in ascending order by default.
Syntax:
Example
ID NAME
1 Jack
2 Harry
Data Control Language statements are used to grant privileges on tables, views, sequences,
procedures to other users.
a. System Privileges
b. Object Privileges
• System Privileges are granted by a DBA to users.
Examples of system privileges are CREATE TABLE, and CREATE USER etc.
• Object Privileges means privileges on objects such as tables, views, procedure.
These are granted by owner of the object.
Object privileges are ALTER, DELETE, INSERT, SELECT, and UPDATE etc.
Revoke succeeded.
These are used to manage the changes made to the data in a table by DML statements.
1. Commit
2. Rollback
3. Savepoint
1. COMMIT command
COMMIT command is used to permanently save any transaction into the database.
When we use any DML command like INSERT, UPDATE or DELETE, the changes made
by these commands are not permanent, until the current session is closed, the changes made
by these commands can be rolled back.
To avoid that, we use the COMMIT command to mark the changes as permanent.
COMMIT;
2. ROLLBACK command
This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing transaction.
If we have used the UPDATE command to make some changes into the database, and realise
that those changes were not required, then we can use the ROLLBACK command to rollback
those changes, if they were not commited using the COMMIT command.
ROLLBACK TO savepoint_name;
3. SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to
that point whenever required.
SAVEPOINT savepoint_name;
id name
1 Abhi
2 Adam
4 Alex
Example
COMMIT;
SAVEPOINT B;
SAVEPOINT C;
NOTE: SELECT statement is used to show the data stored in the table.
The resultant table will look like,
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
use the ROLLBACK command to roll back the state of data to the savepoint B.
ROLLBACK TO B;
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
Again use the ROLLBACK command to roll back the state of data to the savepoint A
ROLLBACK TO A;
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit