DBMS Unit - 1 and Unit-2 Notes
DBMS Unit - 1 and Unit-2 Notes
Functions of DBMS
● Provides data Independence
● Concurrency Control
● Provides Recovery services
● Provides Utility services
● Provides a clear and logical view of the process that manipulates data.
Advantages of DBMS
● Segregation of applicaion program.
● Minimal data duplicacy.
● Easy retrieval of data.
● Reduced development time and maintainance need.
Disadvantages of DBMS
● Complexity
● Costly
● Large in size
Comparison Chart
BASIS FOR
DATA INFORMATION
COMPARISON
Specific No Yes
DEFINITION OF DBMS
The typical file processing system is supported by the operating systems. Files
are created and manipulated by writing programs so the permanent records are
stored in various files. Before the advent of DBMS, organizations typically stored
the information using such systems.
Ex: Using COBOL we can maintain several files (collection of records) to access
those files we have to go through the application programs which have written for
creating files, updating file, inserting the records
The problems in file processing system are
View of data:
The man purpose of DBMS is to provide users with an abstract view of the data.
Physical level: How the data are actually stored that means what data structures are
used to store data on Hard disk
Instance:
Logical schema
Physical schema
Data independence:
The ability to modify schema definition in one level with affecting a schema definition in
the next higher level is called data independence.
Physical independence
Logical independence
Data models:
Underlying
the structure
of a data base
is the data
model. The
collection
of conceptual
tools for describing
data, data
These are used to describe the data at logical level, view level. It is divided
In contrast to object based model they are used both to specify the overall logical
structure of data base and to pride a higher level description of the
implementation.
Relation model
Network model
Hierarchical model
Database languages:
DML command
Data Manipulation Language (DML) statements are used for managing data in
database. DML commands are not auto-committed. It means changes made by DML
command are not permanent to database, it can be rolled back.
1) 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.
S_id S_Name age
INSERT into Student values(101,'Adam',15);
The above command will insert a record into Student table.
S_id S_Name age
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')
2) 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;
S_id S_Name age
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
DELETE from Student where s_id=103;
The above command will delete the record where s_id is 103 from Student table.
S_id S_Name age
101 Adam 15
102 Alex 18
TCL command
Transaction Control Language(TCL) commands are used to manage transactions in
database.These are used to manage the changes made by DML statements. It also
allows statements to be grouped together into logical transactions.
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;
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,
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
7 bravo
Now rollback to savepoint B
rollback to B;
SELECT * from class;
The resultant table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
Now rollback to savepoint A
rollback to A;
SELECT * from class;
The result table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
DCL command
Data Control Language(DCL) is used to control privilege in Database. To perform any
operation in the database, such as for creating tables, sequences or views we need
privileges. Privileges are of two types,
● System : creating session, table etc are all types of system privilege.
● Object : any command or query to work on tables comes under object privilege.
DCL defines two commands,
WHERE clause
Where clause is used to specify condition while retriving data from table. Where clause
is used mostly with Select, Update and Delete query. If condititon specified
by where clause is true then only the result from table is returned.
Syntax for WHERE clause
SELECT column-name1,
column-name2,
column-name3,
column-nameN
from table-name WHERE [condition];
SELECT Query
Select query is used to retrieve data from a tables. It is the most used SQL query. We
can retrieve complete tables, or partial by mentioning conditions using WHERE clause.
101 Adam 15
102 Alex 18
103 Abhi 17
104 Ankit 22
Schema definition
Access methods definition
Schema and physical organization modification
Granting authorization for data
Integrity constraint specification
Structure
of a
DBMS
Figure 1.3
shows the
structure
of a typical
DBM
Database Design:
Requirements Analysis :
The very first step in designing a database application is to understand what data is
to be stored in the database, what applications must be built on top of it, and what
operations are most frequent and subject to performance requirements. In other
words, we must find out what the users want from the database.
We must choose a DBMS to implement our database design, and convert the
conceptual database design into a database schema in the data model of the
chosen DBMS.
Schema Refinement :
The fourth step in database design is to analyse the collection of relations in our
relational database schema to identify potential problems, and to refine it. In
contrast to the requirements analysis and conceptual design steps, which are
essentially subjective, schema refinement can be guided by some elegant and
powerful theory.
E-R
Diagram
ER-Diagram is a visual representation of data that describes how data is related to each
other.
Symbols and Notations
Components of E-R Diagram
The E-R diagram has three main components.
1) Entity
An Entity can be any object, place, person or class. In E-R Diagram, an entity is
represented using rectangles. Consider an example of an Organisation. Employee,
Manager, Department, Product and many more can be taken as entities from an
Organisation.
Weak Entity
Weak entity is an entity that depends on another entity. Weak entity doen't have key
attribute of their own. Double rectangle represents weak entity.
2) Attribute
An Attribute describes a property or characterstic of an entity. For example, Name,
Age, Address etc can be attributes of a Student. An attribute is represented using
eclipse.
Key Attribute
Key attribute represents the main characterstic of an Entity. It is used to represent
Primary key. Ellipse with underlying lines represent Key Attribute.
Composite Attribute
An attribute can also have their own attributes. These attributes are known
as Composite attribute.
3) Relationship
A Relationship describes relations between entities. Relationship is represented using
diamonds.
Binary Relationship
Binary Relationship means relation between two Entities. This is further divided into
three types.
1. One to One : This type of relationship is rarely seen in real world.
The above example describes that one student can enroll only for one course and a
course will also have only one Student. This is not what you will usually see in
relationship.
2. One to Many : It reflects business rule that one entity is associated with many
number of same entity. The example for this relation might sound a little weird, but
this menas that one student can enroll to many courses, but one course will have
one Student.
The arrows in the diagram describes that one student can enroll for only one course.
3. Many to One : It reflects business rule that many entities can be associated with
just one entity. For example, Student enrolls for only one Course but a Course can
have many Students.
4. Many to Many :
The above diagram represents that many students can enroll for more than one
courses.
Recursive Relationship
When an Entity is related with itself it is known as Recursive Relationship.
Ternary Relationship
Relationship of degree three is called Ternary relationship.
SQL Views
A view in SQL is a logical subset of data from one or more tables. View is used to restrict
data access.
Syntax for creating a View,
CREATE or REPLACE view view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
The data fetched from select statement will be stored in another object called sale_view.
We can use create seperately and replace too but using both together works better.
Example of Displaying a View
Syntax of displaying a view is similar to fetching data from table using Select statement.
SELECT * from sale_view;
unit ii
The main
construct
for
representing data in the relational model is a relation. A relation consists of a relation
schema and a relation instance. The relation instance is a table, and the relation
schema describes the column heads for the table. We first describe the relation schema
and then the relation instance. The schema specifies the relation’s name, the name of
each field (or column, or attribute), and the domain of each field. A domain is referred to
in a relation schema by the domain name and has a set of associated values.
Eg:
Students(sid: string, name: string, login: string, age: integer, gpa: real)
This says, for instance, that the field named sid has a domain named string. The set of
values associated with domain string is the set of all character strings.
An instance of a relation is a set of tuples, also called records, in which each tuple
has the same number of fields as the relation schema. A relation instance can be
thought of as a table in which each tuple is a row, and all rows have the same number
of fields.
RDBMS Concepts
A Relational Database management System(RDBMS) is a database management
system based on relational model introduced by E.F Codd. In relational model, data is
represented in terms of tuples(rows).
RDBMS is used to manage Relational database. Relational database is a collection of
organized set of tables from which data can be accessed easily. Relational Database is
most commonly used database. It consists of number of tables and each table has its
own primary key.
What is Table ?
In Relational database, a table is a collection of data elements organised in terms of
rows and columns. A table is also considered as convenient representation of relations.
But a table can have duplicate tuples while a true relation cannot have duplicate tuples.
Table is the most simplest form of data storage. Below is an example of Employee
table.
ID Name Age Salary
1 Adam 34 13000
2 Alex 28 15000
3 Stuart 20 18000
4 Ross 42 19020
What is a Record ?
A single entry in a table is called a Record or Row. A Record in a table represents set
of related data. For example, the above Employee table has 4 records. Following is an
example of single record.
1 Adam 34 13000
What is Field ?
A table consists of several records(row), each record can be broken into several smaller
entities known as Fields. The above Employee table consist of four
fields, ID, Name, Age and Salary.
What is a Column ?
In Relational table, a column is a set of value of a particular type. The term Attribute is
also used to represent a column. For example, in Employee table, Name is a column
that represent names of employee.
Name
Adam
Alex
Stuart
Ross
Database Keys
Keys are very important part of Relational database. They are used to establish and
identify relation between tables. They also ensure that each record within a table can be
uniquely identified by combination of one or more fields within a table.
Super Key
Super Key is defined as a set of attributes within a table that uniquely identifies each
record within a table. Super Key is a superset of Candidate key.
Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected.
It is an attribute or set of attribute that can act as a primary key for a table to uniquely
identify each record in that table.
Primary Key
Primary key is a candidate key that is most appropriate to become main key of the
table. It is a key that uniquely identify each record in a table.
Composite Key
Key that consist of two or more attributes that uniquely identify an entity occurance is
called Composite key. But any attribute that makes up the Composite key is not a
simple key in its own.
Integrity Constraints Over Relations
Key Constraints
Consider the Students relation and the constraint that no two students have the same
student id. This IC is an example of a key constraint. A key constraint is a statement
that a certain minimal subset of the fields of a relation is a unique identifier for a tuple. A
set of fields that uniquely identifies a tuple according to a key constraint is called a
candidate key for the relation; we often abbreviate this to just key.In the case of the
Students relation, the (set of fields containing just the) sid field is a candidate key.
The first part of the definition means that in any legal instance, the values in the key
fields uniquely identify a tuple in the instance
The second part of the definition means, for example, that the set of fields {sid, name} is
not a key for Students, because this set properly contains the key {sid}.Theset {sid,
name} is an example of a superkey, which is a set of fields that contains a key.
CREATE TABLE Students ( sid CHAR(20), name CHAR(30), login CHAR(20), age
INTEGER, gpa REAL, UNIQUE (name, age), CONSTRAINT StudentsKey PRIMARY
KEY (sid) )
This definition says that sid is the primary key and that the combination of name and
age is also a key. The definition of the primary key also illustrates how we can name a
constraint by preceding it with CONSTRAINT constraint-name. If the constraint is
violated, the constraint name is returned and can be used to identify the error.
A foreign key is a key used to link two tables together. This is sometimes
also called as a referencing key.
A Foreign Key is a column or a combination of columns whose values match
a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one
of the tables with a Foreign Key in the second table.
If a table has a primary key defined on any field(s), then you cannot have
two records having the same value of that field(s).
Example
Consider the structure of the following two tables.
CUSTOMERS table
);
ORDERS table
DATE DATETIME,
AMOUNT double,
);
If the ORDERS table has already been created and the foreign key has not
yet been set, the use the syntax for specifying a foreign key by altering a
table.
Syntax
The basic syntax of % and _ is as follows −
SELECT FROM table_name
WHERE column LIKE 'XXXX%'
or
or
or
or
Example
The following table has a few examples showing the WHERE part having
different LIKE clause with '%' and '_' operators −
Let us take a real example, consider the CUSTOMERS table having the
records as shown below.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would display all the records from the
CUSTOMERS table, where the SALARY starts with 200.
Syntax
The basic syntax of the ORDER BY clause is as follows −
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
You can use more than one column in the ORDER BY clause. Make sure
whatever column you are using to sort that column should be in the
column-list.
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The following code block has an example, which would sort the result in an
ascending order by the NAME and the SALARY −
Syntax
The basic syntax of a GROUP BY clause is shown in the following code block.
The GROUP BY clause must follow the conditions in the WHERE clause and
must precede the ORDER BY clause if one is used.
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2
Example
Consider the CUSTOMERS table is having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
If you want to know the total amount of the salary on each customer, then
the GROUP BY query would be as follows.
GROUP BY NAME;
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
Now again, if you want to know the total amount of salary on each
customer, then the GROUP BY query would be as follows −
GROUP BY NAME;
This would produce the following result −
+---------+-------------+
| NAME | SUM(SALARY) |
+---------+-------------+
| Hardik | 8500.00 |
| kaushik | 8500.00 |
| Komal | 4500.00 |
| Muffy | 10000.00 |
| Ramesh | 3500.00 |
+---------+-------------+
Syntax
The basic syntax of DISTINCT keyword to eliminate the duplicate records is
as follows −
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
First, let us see how the following SELECT query returns the duplicate salary
records.
ORDER BY SALARY;
This would produce the following result, where the salary (2000) is coming
twice which is a duplicate record from the original table.
+----------+
| SALARY |
+----------+
| 1500.00 |
| 2000.00 |
| 2000.00 |
| 4500.00 |
| 6500.00 |
| 8500.00 |
| 10000.00 |
+----------+
Now, let us use the DISTINCT keyword with the above SELECT query and
then see the result.
ORDER BY SALARY;
This would produce the following result where we do not have any duplicate
entry.
+----------+
| SALARY |
+----------+
| 1500.00 |
| 2000.00 |
| 4500.00 |
| 6500.00 |
| 8500.00 |
| 10000.00 |
+----------+
Relational Algebra
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses
operators to perform queries. An operator can be either unary or binary.
They accept relations as their input and yield relations as their output.
Relational algebra is performed recursively on a relation and intermediate
results are also considered relations.
The fundamental operations of relational algebra are as follows −
● Select
● Project
● Union
● Set different
● Cartesian product
● Rename
We will discuss all these operations in the following sections.
Select Operation (σ)
It selects tuples that satisfy the given predicate from a relation.
Notation − σp(r)
Where σ stands for selection predicate and r stands for relation. p is
prepositional logic formula which may use connectors like and, or, and not.
These terms may use relational operators like − =, ≠, ≥, < , >, ≤.
For example −
σsubject = "database"(Books)
● Assignment
● Natural join
Relational Calculus
In contrast to Relational Algebra, Relational Calculus is a non-procedural
query language, that is, it tells what to do but never explains how to do it.
Relational calculus exists in two forms −
Tuple Relational Calculus (TRC)
Filtering variable ranges over tuples
Notation − {T | Condition}
Returns all tuples T that satisfies a condition.
For example −
{< article, page, subject > | ∈ TutorialsPoint ∧ subject = 'database'}
Output − Yields Article, Page, and Subject from the relation TutorialsPoint,
where subject is database.
Just like TRC, DRC can also be written using existential and universal
quantifiers. DRC also involves relational operators.
The expression power of Tuple Relation Calculus and Domain Relation
Calculus is equivalent to Relational Algebra.
Joins in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single
set of data. SQL Join is used for combining column from two or more tables by using values
common to both tables. Join Keyword is used in SQL queries for joining two or more tables.
Minimum required condition for joining table, is (n-1) where n, is number of tables. A table
can also join to itself known as, Self Join.
Types of Join
The following are the types of JOIN that we can use in SQL.
● Inner
● Outer
● Left
● Right
from table-name1
CROSS JOIN
table-name2;
ID NAME
1 abhi
2 adam
4 alex
1 DELHI
2 MUMBAI
3 CHENNAI
from class,
ID NAME ID Address
1 abhi 1 DELHI
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
from table-name1
INNER JOIN
table-name2
ID NAME
1 abhi
2 adam
3 alex
4 anu
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
Natural Join Syntax is,
SELECT *
from table-name1
NATURAL JOIN
table-name2;
Example of Natural JOIN
The class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
In the above example, both the tables being joined have ID column(same name and same
datatype), hence the records for which value of ID matches in both the tables will be the
result of Natural Join of these two tables.
Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further
into,
● Left Outer Join
● Right Outer Join
● Full Outer Join
from table-name1
table-name2
on table-name1.column-name = table-name2.column-name;
from table-name1,
table-name2
on table-name1.column-name = table-name2.column-name(+);
Example of Left Outer Join
The class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
from table-name1
table-name2
on table-name1.column-name = table-name2.column-name;
from table-name1,
table-name2
on table-name1.column-name(+) = table-name2.column-name;
Example of Right Outer Join
The class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
from table-name1
table-name2
on table-name1.column-name = table-name2.column-name;
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI