Professional Documents
Culture Documents
DBMS Report Raj
DBMS Report Raj
DBMS Report Raj
DBMS REPORT
What is a database?
Database is a collection of related data organised in a way that can be easily
accessed, managed, and updated.
For example, a university database might contain information about the
following:
• Entities such as students, faculty, courses, and classrooms.
• Relationships between entities, such as students' enrollment in courses, faculty
teaching courses, and the use of rooms for courses.
What is a DBMS?
A database management system, or DBMS, is software designed to assist in
maintaining and utilizing large collections of data.
It is a collection of programs used for managing data and simultaneously it
supports different types of users to create, manage, retrieve, update and store
information.
Database-System Applications
Databases are widely used. Here are some representative applications:
Enterprise Information
◦ Sales: For customer, product, and purchase information.
◦ Accounting: For payments, receipts, account balances, assets and other
accounting information.
◦ Human resources: For information about employees, salaries, payroll taxes,
and benefits, and for generation of paychecks.
◦ Manufacturing: For management of the supply chain and for tracking
production of items in factories, inventories of items in warehouses and stores,
and orders for items.
◦ Online retailers: For sales data noted above plus online order tracking,
generation of recommendation lists, and maintenance of online product
evaluations.
• Banking and Finance
◦ Banking: For customer information, accounts, loans, and banking transactions.
◦ Credit card transactions: For purchases on credit cards and generation of
monthly statements.
◦ Finance: For storing information about holdings, sales, and purchases of
financial instruments such as stocks and bonds; also for storing real-time market
data to enable online trading by customers and automated trading by the firm.
• Universities: For student information, course registrations, and grades (in
addition to standard enterprise information such as human resources and
accounting).
• Airlines: For reservations and schedule information. Airlines were among the
first to use databases in a geographically distributed manner.
• Telecommunication: For keeping records of calls made, generating monthly
bills, maintaining balances on prepaid calling cards, and storing information
about the communication networks.
Purpose of Database Systems
The purpose of DBMS is to transform the following −
Data into information.
Information into knowledge.
Knowledge to the action.
The diagram given below explains the process as to how the transformation of
data to information to knowledge to action happens respectively in the DBMS –
DATA
INFORMATION
KNOWLEDGE ACTION
(Unprocessed
(Processed)
information
View of Data in DBMS
Abstraction is one of the main features of database systems.
Hiding irrelevant details from user and providing abstract view of data to
users, helps in easy and efficient user-database interaction.
The three level of DBMS architecture, The top level of that architecture is
―view level. The view level provides the ―view of data‖ to the users and hides
the irrelevant details such as data relationship, database schema, constraints,
security etc from the user.
DATA MODELS:
Data Model is the modelling of the data description, data semantics, and
consistency constraints of the data.
It provides the conceptual tools for describing the design of a database at each
level of data abstraction.
Therefore, there are following four data models used for understanding the
structure of the database:
Four Types of DBMS systems are:
Hierarchical database
Network database
Relational database
ER model database
Hierarchical DBMS
In a Hierarchical database, model data is organized in a tree-like structure. Data
is Stored Hierarchically (top down or bottom up) format. Data is represented
using a parent-child relationship. In Hierarchical DBMS parent may have many
children, but children have only one parent.
COLLEGE
DEPARTMENT INFRASTRUCTURE
Network Model
The network database model allows each child to have multiple parents. It helps
you to address the need to model more complex relationships like as the
orders/parts many-to-many relationship. In this model, entities are organized in
a graph which can be accessed through several paths.
DATABASE LANGUAGE
2.ALTER
1. add
2. modify
3. rename
4. drop
2.1. ADD
Syntax:
alter table table_name add (new_attribute_name datatype(size),…..,N);
Query:
alter table cseraj add(aad number(20), phone number(10));
Output:
SQL> desc cseraj;
Name Null? Type
………………………. ……………… ………………
SNO NUMBER(10)
NAME VARCHAR2(20)
MARKS NUMBER
ADD NUMBER(20)
PHONE NUMBER(10)
2.2. MODIFY
Syntax:
alter table table name modify (oldcolumnname datatype (new size));
Query:
alter table cseraj modify (name varchar2(15));
Output:
SQL> desc cseraj;
Name Null? Type
………………………. ……………… ………………
SNO NUMBER(10)
NAME VARCHAR2(15)
MARKS NUMBER
ADD NUMBER(20)
PHONE NUMBER(10)
2.3 RENAME
Syntax:
alter table tablename rename column oldcolumnname to
newcolumnname;
Query:
alter table cseraj rename column sno to no;
SQL> desc cseraj;
Name Null? Type
………………………. ……………… ………………
NO NUMBER(10)
NAME VARCHAR2(15)
MARKS NUMBER
ADD NUMBER(20)
PHONE NUMBER(10)
2.4 DROP
Syntax:
alter table tablename drop column oldcolumnname;
Query:
alter table cseraj drop column marks;
Output:
SQL> desc cseraj;
Name Null? Type
………………………. ……………… ………………
NO NUMBER(10)
NAME VARCHAR2(15)
ADD NUMBER(20)
PHONE NUMBER(10)
3.TRUNCATE
Syntax:
truncate table tablename;
Query:
truncate table cseraj;
Output:
SQL> desc cseraj;
Name Null? Type
………………………. ……………… ………………
NO NUMBER(10)
NAME VARCHAR2(15)
ADD NUMBER(20)
PHONE NUMBER(10)
4.DROP
Syntax:
drop table tablename;
Query:
drop table raj1;
Output:
SQL> desc cseraj;
ERROR:
ORA-04043: object cseraj does not exist.
2.SELECT
Syntax:
Select columnlists from table_name;
(OR)
Select * from table_name;
Query:
Select * from cseraj;
Output:
SNO SNAME MARKS
501 Raj 85
502 Deepak 90
503 Suraj 75
3.UPDATE
Syntax:
Update table_name set column_name=new_value where condition;
Query:
Update cseraj set marks=80 where sno=503;
Output:
1 row updated.
Select * from cseraj;
SNO SNAME MARKS
501 Raj 85
502 Deepak 90
503 Suraj 80
4.DELETE
Syntax:
Delete from table_name where condition;
Query:
Delete from cseraj where sno=503;
Output:
1 row deleted.
Select * from cseraj;
SNO SNAME MARKS
501 Raj 85
502 Deepak 90
Query:
Create user raj identified by 123;
Grant create session to raj;
Grant select on cseraj to raj;
SQL> Conn
Enter user-name: raj
Enter password: 123
SQL> select * from system.cseraj;
SNO SNAME MARKS
501 Raj 85
502 Deepak 90
REVOKE:
Syntax:
Revoke privileges on tablename from username;
Query:
Revoke select on cseraj from raj;
Output:
Privilege revoked.
DBMS ARCHITECTURE
Database management systems architecture will help us understand the
components of database system and the relation among them.
The architecture of DBMS depends on the computer system on which it runs.
the basic client/server architecture is used to deal with a large number of PCs,
web servers, database servers and other components that are connected with
networks.
The client/server architecture consists of many connected to the database to
get their request done.
TYPES OF DBMS ARCHITECTURE
There are three types of DBMS architecture:
1. Single tier architecture
2. Two tier architecture
3. Three tier architecture
1-Tier Architecture
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.
Any changes done here will directly be done on the database itself. It doesn't
provide a handy tool for end users.
The 1-Tier architecture is used for development of the local application, where
programmers can directly communicate with the database for the quick
response.
2-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.
back to the client. The application connection interface such as JDBC, ODBC
are used for the interaction between server and client.
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.
DATABASE server
SYSTEM
Application
client
User
3- Tier Architecture
In three-tier architecture, another layer is present between the client machine
and server machine.
In this architecture, the client application doesn‘t communicate directly with
the database systems present at the server machine, rather the client application.
Communicates with server application and the server application internally
communicates with the database system present at the server.
ER model
ER model stands for an Entity-Relationship model. It is a high-level data
model. This model is used to define the data elements and relationship
for a specified system.
It develops a conceptual design for the database. It also develops a very
simple and easy to design view of data.
In ER modelling, the database structure is portrayed as a diagram called
an entity-relationship diagram.
For example, Suppose we design a school database. In this database, the student
will be an entity with attributes like address, name, id, age, etc. The address can
be another entity with attributes like city, street name, pin code, etc and there
will be a relationship between them.
1. Entity:
An entity may be any object, class, person or place. In the ER diagram, an
entity can be represented as rectangles.
Consider an organization as an example- manager, product, employee,
department etc. can be taken as an entity.
a. Weak Entity
An entity that depends on another entity called a weak entity. The weak
entity doesn't contain any key attribute of its own. The weak entity is
represented by a double rectangle.
2. Attribute
b. Composite Attribute
c. Multivalued Attribute
An attribute can have more than one value. These attributes are known as
a multivalued attribute. The double oval is used to represent multivalued
attribute.
For example, a student can have more than one phone number.
d. Derived Attribute
An attribute that can be derived from other attribute is known as a derived
attribute. It can be represented by a dashed ellipse.
For example, A person's age changes over time and can be derived from
another attribute like Date of birth.
3. Relationship
A relationship is used to describe the relation between entities. Diamond
or rhombus is used to represent the relationship
Types of relationship are as follows:
a. One-to-One Relationship
When only one instance of an entity is associated with the relationship,
then it is known as one to one relationship.
For example, A female can marry to one male, and a male can marry to one
female.
b. One-to-many relationship
When only one instance of the entity on the left, and more than one
instance of an entity on the right associates with the relationship then this
is known as a one-to-many relationship.
For example, Scientist can invent many inventions, but the invention is
done by the only specific scientist.
c. Many-to-one relationship
When more than one instance of the entity on the left, and only one
instance of an entity on the right associates with the relationship then it is
known as a many-to-one relationship.
For example, Student enrolls for only one course, but a course can have
many students.
d. Many-to-many relationship
When more than one instance of the entity on the left, and more than
one instance of an entity on the right associates with the relationship
then it is known as a many-to-many relationship.
For example, Employee can assign by many projects and project can have
many employees.
Notation of ER diagram
Database can be represented using the notations. In ER diagram, many
notations are used to express the cardinality. These notations are as follows:
Relationship set in DBMS
The number of entity types which took part in the entity relationship is called
the degree of relationships.
There are three different types of degree of relationships, they are as follows −
• Unary relationship
• Binary relationship
• Ternary relationship
Unary relationship
It is the relationship between the instances of a single entity type. It is also
called a recursive relationship.
Example
A person is married is a one-to-one relationship between the instances of
Person entity type of unary relationship.
Binary relationship
It is the relationship between the instances of two different entity types. Two
entities will participate in the relationship.
For Example
Person and events are two different entity types which are related by using the
relationship called ―attend.
Ternary relationship
A simultaneous relationship between the instances of three entity types with
unique attributes is called a ternary relationship.
Example
Consultant, client and contract are three different entities with different
attributes.
These three entities are related with a single relationship called ―signs.
KEYS IN DBMS
Syntax
The syntax to create a primary key using the ALTER TABLE statement in
SQL is:
Integrity Constraints
o Integrity constraints are a set of rules. It is used to
maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and
other processes have to be performed in such a way that data
integrity is not affected.
o Thus, integrity constraint is used to guard against accidental
damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of
values for an attribute.
o The data type of domain includes string, character, integer, time,
date, currency, etc. The value of the attribute must be available in the
corresponding domain.
Example:
2.Entity integrity constraints
o This is because the primary key value is used to identify individual rows in
relation and if the primary key has a null value, then we can't identify those
rows.
o A table can contain a null value other than the primary key field.
4. Key constraints
a. Keys are the entity set that is used to identify an entity within its entity set
uniquely.
b. An entity set can have multiple keys, but out of which one key will be the
primary key.
A primary key can contain a unique and null value in the relational table.
UNIT-II
Relational Algebra and Calculus: Relational Algebra - Selection and
Projection, Set operations, Renaming, Joins.
Form of Basic SQL Query- Examples of Basic SQL Queries, Introduction to
Nested Queries, Correlated Nested Queries, Set - Comparison Operators,
Aggregate Operators, NULL values - Comparison using Null values,
Disallowing NULL values, Triggers and Active Data bases.
Relational Algebra
• Relational Algebra is procedural query language, which takes Relation as
input and generates relation as output. Relational algebra mainly provides
theoretical foundation for relational databases and SQL.
• Relational algebra is a procedural query language, it means that it tells
what data to be retrieved and how to be retrieved.
• Relational Algebra works on the whole table at once, so we do not have
to use loops etc to iterate over all the rows (tuples) of data one by one.
• All we have to do is specify the table name from which we need the data,
and in a single line of command, relational algebra will traverse the entire
given table to fetch data for you.
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union ( )
4. Intersect (∩)
5. Set Difference (-)
6. Cartesian product (X)
7. Rename (ρ)
Output:
ID NAME BRANCH MARKS
101 RAJ CIVIL 75
Query:
∏ ID, NAME, MARKS (STUDENT);
OUTPUT
ID NAME MARKS
101 RAJ 75
102 DEEPAK 85
103 SURAJ 65
104 PRITI 90
105 KRITI 95
Syntax:
ρ (newRelation, oldRelation);
Example:
We can use the rename operator to rename STUDENT relation to
STUDENT1.
Query:
ρ (STUDENT1, STUDENT)
Table R
CID CNAME
504 COA
505 DBMS
506 OOPS
Table S
CID CNAME
506 OOPS
507 OS
508 MEFA
Syntax:
Table1 U Table2;
Query:
RUS
Output:
CID CNAME
504 COA
505 DBMS
506 OOPS
507 OS
508 MEFA
Table COURSE
CID CNAME
504 COA
505 DBMS
Syntax:
Table1 × Table2
Query:
STUDENT × COURSE
Output:
SID SNAME CID CNAME
101 Raj 504 COA
101 Raj 505 DBMS
102 Deepak 504 COA
102 Deepak 505 DBMS
103 Suraj 504 COA
103 Suraj 505 DBMS
JOIN IN DBMS
• A JOIN clause is used to combine rows from two or more tables, based on
a related column between them.
• Join in DBMS is a binary operation which allows you to combine join
product and selection in one single statement.
• The goal of creating a join condition is that it helps you to combine the data
from two or more DBMS tables.
• The tables in DBMS are associated using the primary key and foreign keys.
• A Join can be broadly divided into two types :
• INNER JOIN
• OUTER JOIN
JOIN
Table BRANCH
BRANCH_ID BRANCH_NAME
101 CE
102 EEE
401 ECE
INNER JOIN
• Inner join is a join that can be used to return all the values that have
matching values in both the tables.
Equi Join
Equi Join is an inner join that uses the equivalence condition for fetching the
values of two tables.
Syntax:
Select tablename.columnlists from table1name inner join table2name on
condition.
Query:
Select student.sid, student.sname, branch.branch_name from student
INNER JOIN branch on student.branch_id = branch.branch_id;
Output:
SID SNAME BRANCH_NAME
2 Deepak EEE
3 Suraj CE
Natural Join
Natural Join is an inner join that returns the values of the two tables on
the basis of a common attribute that has the same name and domain.
It does not use any comparison operator.
It also removes the duplicate from the results.
Syntax:
Select * from table1name natural join table2name;
Query:
Select * from student natural join branch;
Output:
SID SNAME BRANCH_ID BRANCH_NAME
2 Deepak 201 EEE
3 Suraj 101 CE
OUTER JOIN
Outer join is a join that can be used to return in both the tables whether it has
matching record in both the tables or not.
The outer join can be further divided into three types:
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
Left Outer Join
The SQL left join returns all the values from left table and the matching
values from the right table. If there is no matching join value, it will return
NULL.
Syntax:
SELECT table1.column1, table1.column2 FROM
table1
LEFT OUTER JOIN
table2
ON table1.matching_column = table2.matching_column;
Query:
Select student.sid, student.sname, branch.branch_id FROM
STUDENT
LEFT OUTER JOIN
BRANCH
ON student.branch_id = branch.branch_id;
Output:
SID SNAME BRANCH_NAME
1 Raj NULL
2 Deepak EEE
3 Suraj CE
4 Rahul NULL
DIVISION OPERATOR
The division operator is used for queries which involve the ‘ALL’. It is not
supported by SQL.
R1÷R2 = tuples of R1 associated with all tuples of R2.
Consider tables R1 and R2.
Table R1
NAME COURSE
JAVA B.TECH
DBMS M.TECH
DBMS B.TECH
PYTHON B.TECH
Table R2
COURSE
B.TECH
M.TECH
Query:
Retrieves the name of the subject that is taught in all courses.
R1 ÷ R2
Output:
NAME
DBMS
Relational Calculus:
Syntax:
SELECT columnlists from tablename WHERE colunmname operator
(SELECT columnlists FROM tablename where condition);
Consider the CUSTOMER table having the following records:
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Puttur 2000.0
2 Rajesh 25 Tirupati 2500.0
3 Suresh 32 Patna 1500.0
4 Komal 40 Delhi 2800.0
5 Hardik 30 Mumbai 3000.0
Query:
SELECT * from customer where id in (SELECT id from customer
where salary > 2000.0);
Output:
ID NAME AGE ADDRESS SALARY
2 Rajesh 25 Tirupati 2500.0
4 Komal 40 Delhi 2800.0
5 Hardik 30 Mumbai 3000.0
Correlated Subquery:
Correlated subqueries are used for row-by-row processing. Each
subquery is executed once for every row of the outer query.
A correlated subquery is evaluated once for each row processed by the
parent statement. The parent statement can be a SELECT, UPDATE, or
DELETE statement.
A correlated subquery is one way of reading every row in a table and
comparing values in each row against related data.
Syntax:
SELECT column1, column2, ……,
FROM table1 outer
WHERE column1 operator
(SELECT column1, column2,
FROM table2
Query: WHERE expr1 = outer.expr2);
SELECT name, salary, id
FROM customer outer
WHERE salary >
(SELECT AVG(salary)
FROM customer
WHERE id =
outer.id group by id);
Output:
ID NAME AGE ADDRESS SALARY
2 Rajesh 25 Tirupati 2500.0
4 Komal 40 Delhi 2800.0
5 Hardik 30 Mumbai 3000.0
Syntax:
SELECT columnlists FROM table
WHERE expr1 <comparison operator> expr2;
AGGREGATE OPERATORS
SQL aggregation function is used to perform the calculations on
multiple rows of a single column of a table. It returns a single value.
It is also used to summarize the data.
SQL provides following aggregate functions:
Consider a student table:
SID NAME MARKS
101 A 85
102 B 80
103 C 75
104 D 65
1. COUNT ()
It returns the number of rows in a database table.
Syntax:
COUNT (attribute)
Query:
SELECT COUNT (sid) from student;
Output:
4
2. SUM ()
It returns the total sum of a numeric column.
Syntax:
SUM (attribute)
Query:
SELECT SUM (marks) from student;
Output:
SUM
305
3. AVG ()
It calculates the average of a set of values.
Syntax:
AVG (attribute)
Query:
SELECT AVG (marks) from student;
Output:
AVG
76
4. MIN ()
It returns the lowest value (minimum) in a set of non-null values.
Syntax:
MIN (attributes)
Query:
SELECT MIN (marks) from student;
Output:
MIN
65
5. MAX ()
It returns the highest value (maximum) in a set of non-null values.
Syntax:
MAX (attribute)
Query:
SELECT MAX (marks) from student;
Output:
MAX
85
Syntax:
SELECT * from table ORDER BY column ASC/DESC;
Query:
SELECT * from student ORDER BY sid ASC;
Output:
SID NAME MARKS
1 A 75
2 B 95
3 C 85
4 D 60
GROUP BY
The GROUP BY statement in SQL is used to arrange identical data into
groups with the help of some functions.
If a particular column has same values in different rows then it will
arrange these row in a group.
Consider STUDENT table.
SID SNAME MARKS
1 Harsh 85
2 Raj 75
3 Ashish 70
4 Harsh 60
5 Ashish 90
Syntax:
SELECT column1, function_name (column2) from tablename
WHERE condition GROUP BY column1, column2;
Query:
SELECT sname, sum (marks) from student GROUP BY sname;
Output:
SNAME MARKS
Harsh 145
Raj 75
Ashish 160
HAVING
The HAVING clause is used to apply a filter on the result of GROUP BY
based on the specified condition.
Syntax:
SELECT column1, function (column2) from tablename
WHERE condition GROUP BY column1, column2 HAVING
condition;
Query:
SELECT sname, sum (marks) as marks from student GROUP BY
sname
HAVING sum (marks)>100;
Output:
SNAME MARKS
Harsh 145
Ashish 160
NULL VALUES
SQL supports a specific value known as NULL which is used to represent
the value of attributes that may be unknown (or) not apply to a tuple.
It is important to know that a NULL value is different from ZERO value.
A NULL value is used to represent a missing value.
Each NULL value is considered to be different from every other NULL in
database.
Comparison Using NULL Values:
SELECT column-names
FROM table-name
SELECT column-names
FROM table-name
Views in SQL
Views in SQL are considered as a virtual table. A view also contains
rows and columns.
To create the view, we can select the fields from one or more tables
present in the database.
Table: Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Table: Student_Marks
STU_ID NAME MARKS AGE
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
A view can be created using the CREATE VIEW statement. We can
create a view from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS SELECT column1, column2.....
FROM table_name WHERE condition;
Query:
CREATE VIEW DetailsView AS SELECT NAME,
ADDRESS FROM Student_Details WHERE STU_ID
< 4;
Just like table query, we can query the view to view the data. SELECT *
FROM DetailsView;
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
Query:
CREATE VIEW MarksView AS
SELECT Student_Detail.NAME, Student_Detail.ADDRESS,
Student_Marks.MARKS FROM Student_Detail, Student_Mark
WHERE Student_Detail.NAME = Student_Marks.NAME;
Output:
2. Deleting View
Output:
VIEW dropped.
Uses of a View:
A good database should contain views due to the given reasons:
1. Restricting data access – Views provide an additional level
of table security by restricting access to a predetermined set of
rows and columns of a table.
2. Hiding data complexity – A view can hide the complexity that
exists in a multiple table join.
3. Simplify commands for the user –
Views allows the user to select information from multiple tables
without requiring the users to actually know how to perform a join.
4. Store complex queries –
Views can be used to store complex queries.
5. Rename Columns –
Views can also be used to rename the columns without affecting
the base tables provided the number of columns in view must
match the number of columns specified in select statement. Thus,
renaming helps to to hide the names of the columns of the base
tables.
6. Multiple view facility –
Different views can be created on the same table for different users.
Trigger:
Where condition
DECLARE
BEGIN
END;
Types of Triggers:
Query:
BEGIN
dbms_output.put_line(“updated successfully”);
END;
Output:
Trigger Created.
Updated Successfully.
1 row updated.
Query:
BEGIN
dbms_output.put_line(“Updated Successfully”);
END;
Output:
Trigger Created.
Updated successfully.
1 row updated.
1 row updated.
1 row updated.
Active Databases
UNIT-III
Introduction to Schema Refinement- Problems Caused by redundancy,
Functional Dependencies, Normal Forms - FIRST, SECOND, THIRD Normal
forms – BCNF.
Properties of Decompositions: Lossless join Decomposition, Dependency
preserving Decomposition - FOURTH Normal Form, FIFTH Normal form.
Redundancy:
Redundancy means having multiple copies of same data in the database. This
problem arises when a database is not normalized.
Suppose a table of student details having attributes Sid, sname, college, rank,
course.
SID SNAME CONTACT COLLEGE COURSE RANK
101 Raj 94712 SISTK B.TECH 1
102 Deepak 74939 SISTK B.TECH 1
103 Ravi 85078 SISTK B.TECH 1
104 Suraj 77695 SISTK B.TECH 1
As it can be observed that values of attribute college name, college rank, course,
is being repeated which can lead to problems.
2 B 20
3 C 20