0% found this document useful (0 votes)
21 views

DBMS

Uploaded by

ashokgehlot2100
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

DBMS

Uploaded by

ashokgehlot2100
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 83

DBMS

DATA and INFORMATION


• Data: It is a raw facts. Data can be
represented in form of numbers , words,
images, sounds, multimedia and animated
data.
• Information: Information is data that has been
converted into a more useful or intelligible
form.
DBMS

• A Database Management System (DBMS) is a


software system that is designed to manage
and organize data in a structured manner. It
allows users to create, modify, and query a
database, as well as manage the security and
access controls for that database.
• DBMS provides an environment to store and
retrieve the data in co-invent and efficient
manner.
Key concepts:
• Data modeling: A DBMS provides tools for creating and modifying data
models, which define the structure and relationships of the data in a
database.
• Data storage and retrieval: A DBMS is responsible for storing and
retrieving data from the database, and can provide various methods for
searching and querying the data.
• Concurrency control: A DBMS provides mechanisms for controlling
concurrent access to the database, to ensure that multiple users can
access the data without conflicting with each other.
• Data integrity and security: A DBMS provides tools for enforcing data
integrity and security constraints, such as constraints on the values of
data and access controls that restrict who can access the data.
• Backup and recovery: A DBMS provides mechanisms for backing up and
recovering the data in the event of a system failure.
DBMS can be classified into two types
Relational Database Management System (RDBMS)
and Non-Relational Database Management
System (NoSQL or Non-SQL)
• RDBMS: Data is organized in the form of tables
and each table has a set of rows and columns.
The data are related to each other through
primary and foreign keys.
• NoSQL: Data is organized in the form of key-
value pairs, documents, graphs, or column-based.
These are designed to handle large-scale, high-
performance scenarios.
Database Languages
• Data Definition Language
• Data Manipulation Language
• Data Control Language
• Transactional Control Language
Data Definition Language

• DDL is the short name for Data Definition


Language, which deals with database schemas
and descriptions, of how the data should
reside in the database.
• CREATE: to create a database and its objects like
(table, index, views, store procedure, function,
and triggers)
• ALTER: alters the structure of the existing
database
• DROP: delete objects from the database
• TRUNCATE: remove all records from a table,
including all spaces allocated for the records are
removed
• COMMENT: add comments to the data dictionary
• RENAME: rename an object
Data Manipulation Language
• DML is the short name for Data Manipulation
Language which deals with data manipulation
and includes most common SQL statements
such SELECT, INSERT, UPDATE, DELETE, etc.,
and it is used to store, modify, retrieve, delete
and update data in a database.
• SELECT: retrieve data from a database
• INSERT: insert data into a table
• UPDATE: updates existing data within a table
• DELETE: Delete all records from a database
table
• MERGE: UPSERT operation (insert or update)
Data Control Language
• DCL is short for Data Control Language which acts
as an access specifier to the database.(basically
to grant and revoke permissions to users in the
database
• GRANT: grant permissions to the user for running
DML(SELECT, INSERT, DELETE,…) commands on
the table
• REVOKE: revoke permissions to the user for
running DML(SELECT, INSERT, DELETE,…)
command on the specified table
Transactional Control Language
• TCL is short for Transactional Control Language
which acts as an manager for all types of
transactional data and all transactions. Some of
the command of TCL are
• Roll Back: Used to cancel or Undo changes made
in the database
• Commit: It is used to apply or save changes in the
database
• Save Point: It is used to save the data on the
temporary basis in the database
Applications of DBMS:
• Enterprise Information: Sales, accounting, human
resources, Manufacturing, online retailers.
• Banking and Finance Sector: Banks maintaining the
customer details, accounts, loans, banking transactions,
credit card transactions. Finance: Storing the information
about sales and holdings, purchasing of financial stocks and
bonds.
• University: Maintaining the information about student
course enrolled information, student grades, staff roles.
• Airlines: Reservations and schedules.
• Telecommunications: Prepaid, postpaid bills maintance.
E-R Model:
• Entity: An entity is referred to as a real-world object.
It can be a name, place, object, class, etc. These are
represented by a rectangle in an ER Diagram.
• Attributes: An attribute can be defined as the
description of the entity. These are represented by
Eclipse in an ER Diagram. It can be Age, Roll Number,
or Marks for a Student.
• Relationship: Relationships are used to define
relations among different entities. Diamonds and
Rhombus are used to show Relationships.
An attribute can be of many types, here are different types of
attributes defined in ER database model:
• Simple attribute: The attributes with values that are atomic
and cannot be broken down further are simple attributes. For
example, student's age.
• Composite attribute: A composite attribute is made up of
more than one simple attribute. For example,
student's address will contain, house no., street
name, pincode etc.
• Derived attribute: These are the attributes which are not
present in the whole database management system, but are
derived using other attributes. For example, average age of
students in a class.
• Single-valued attribute: As the name suggests, they have a
single value.
• Multi-valued attribute: And, they can have multiple values.
Relation Database
• A relational database collects different types
of data sets that use tables, records, and
columns. It is used to create a well-defined
relationship between database tables so that
relational databases can be easily stored. For
example of relational databases such as
Microsoft SQL Server, Oracle Database,
MYSQL, etc
• A table has records (rows) and fields
(columns).
• Degree refers to the number of different
attributes or columns in a table, while
Cardinality is used to count the total number
of tuples or rows in a table.
Different Types of Keys
• Primary Key
• Candidate Key
• Super Key
• Alternate Key
• Foreign Key
• Composite Key
• Primary Key: The primary key refers to a
column or a set of columns of a table that
helps us identify all the records uniquely
present in that table. A table can consist of
just one primary key. All the values of a
primary key have to be different, and there
should be no repetitions. The PK (PRIMARY
KEY) constraint that we put on a column/set
of columns won’t allow these to have a null
value or a duplicate.
• Super key: A super keys are those attributes of
a relation, Which have the properties of
uniqueness. The attributes of the super key can
have NULL values.
+
• Composite Key can be defined as a
combination of multiple columns, and these
columns are used to identify all the rows that
are involved uniquely. Even though a single
column can't identify any row uniquely, a
combination of over one column can uniquely
identify any record.
• A foreign key is a column or columns of data
in one table that refers to the unique data
values -- often the primary key data -- in
another table. Foreign keys link together two
or more tables in a relational database.
Relational Integrity Rules
• Entity Integrity Rule: It states that value of
attribute of a primary key cannot be null.
• Referential Integrity Rule: The Rule defines
that a foreign key have a matching primary
key. Reference from a table to another table
should be valid.
Relational Algebra and Calculus
• Relational Algebra:
• Relational Algebra is a procedural language. In
Relational Algebra, The order is specified in
which the operations have to be performed.
The basic operation included in relational
algebra are:
• 1. Select (σ) 2. Project (Π) 3. Union (U) 4. Set
Difference (-) 5. Cartesian product (X) 6.
Rename (ρ)
1. Selection(σ): It is used to select required tuples of
the relations.
3. Union(U): Union operation in relational algebra is
the same as union operation in set theory. Let there
are two tables: FRENCH and German
Student_Name Roll_Number Student_Name Roll_Number

Ram 01 Vivek 13

Mohan 02 Geeta 17

Vivek 13 Shyam 21

Geeta 17 Rohan 25
Student_Name
π(Student_Name)FRENCH U π(Student_Name)GERMAN Ram

Mohan

Vivek

Geeta

Shyam

Rohan
• 6. Rename(ρ): Rename is a unary operation
used for renaming attributes of a relation.
• ρ(a/b)R will rename the attribute 'b' of the
relation by 'a'.
• 7. Cross Product(X): Cross-product between
two relations. Let’s say A and B, so the cross
product between A X B will result in all the
attributes of A followed by each attribute of B.
Each record of A will pair with every record of
B.
• A B
Name Age Sex ID Course
Ram 14 M
1 DS
Sona 15 F

2 DBMS
Kim 20 M

AXB Name Age Sex ID Course

Ram 14 M 1 DS

Ram 14 M 2 DBMS

Sona 15 F 1 DS

Sona 15 F 2 DBMS

Kim 20 M 1 DS

Kim 20 M 2 DBMS
Types of Join operations
1. Natural Join
2. Outer Join
3. Equi Join
A natural join is the set of tuples of all
combinations in R and S that are equal on their
common attribute names. It is denoted by ⋈.
The outer join operation is an extension of the join
operation. It is used to deal with missing information.
Left outer join:
• Left outer join contains the set of tuples of all
combinations in R and S that are equal on their
common attribute names.
• In the left outer join, tuples in R have no
matching tuples in S.
• It is denoted by ⟕.
• Example: Using the above EMPLOYEE table and
FACT_WORKERS table
• EMPLOYEE ⟕ FACT_WORKERS
EMPLOYEE ⟖ FACT_WORKERS
• 3. Equi join:
• It is also known as an inner join. It is the most
common join. It is based on matched data as
per the equality condition. The equi join uses
the comparison operator(=).
Data Definition Language
• Create: Syntax: CREATE table table_name
(
Column1 datatype (size),
column2 datatype (size),

columnN datatype(size)
);
Here table_name is name of the table, column is the name of
column
• CREATE TABLE Customer( CustomerID INT
PRIMARY KEY, CustomerName VARCHAR(50),
LastName VARCHAR(50), Country
VARCHAR(50), Age int(2), Phone int(10) );
Output:

Insert Data into Table
• Insert into Table_name(Column1, Column2,
Column3)
Values (Value1, value2, value3);
• Example: INSERT INTO Customer (CustomerID,
CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
(2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'), (3,
'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'), (4,
'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'), (5, 'Nishant.
Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');
• DROP is used to delete a table.
• DROP TABLE table_name;
• TRUNCATE statement is a Data Definition
Language (DDL) operation that is used to mark
the extent of a table for deallocation (empty
for reuse). The result of this operation quickly
removes all data from a table.
• TRUNCATE TABLE table_name;
Alter command:
• ADD is used to add columns to the existing
table.
• ALTER TABLE table_name ADD
(Columnname_1 datatype, Columnname_2
datatype, …Columnname_n datatype);
• Example:
• Alter table customer add email varchar(255);
• DROP COLUMN is used to drop columns in a
table. Deleting the unwanted columns from
the table.
• Syntax: ALTER TABLE table_name
DROP COLUMN column_name;

• Example: Alter table customer drop column


email;
Comments
• Comments can be written in the following
three formats:
• Single-line comments
• Multi-line comments
• In-line comments
• Single-line comments
Example: SELECT * FROM customers;
-- This is a comment.
Multi-Line Comments
/*
This is a multi-line comment that explains
the purpose of the query below.
*/
SELECT * FROM orders WHERE YEAR(order_date) =
2022;
• In-Line Comments
In line comment ->
SELECT * FROM Students;
SELECT * FROM /* STUDENT_DETAILS;
SELECT * FROM Orders;
SELECT * FROM */ Articles;
Rename
• To rename table.
• Syntax: ALTER TABLE table_name
RENAME TO new_table_name;

• Columns can also be given a new name with the


use of ALTER TABLE.
• Syntax(MySQL, Oracle)
• ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
Data Manipulation language
• Insert
• Delete
• Update
• select
delete
• DELETE can be used to selectively remove records from a database
table based on certain conditions. This SQL DELETE operation is
important for database size management, data accuracy, and
integrity.
• Syntax:
• DELETE FROM table_name WHERE some_condition;
• Example: DELETE FROM GFG_Employees WHERE NAME = 'Rithvik';
• Delete All of the Records
• To remove all the entries from the table, Query
• DELETE FROM GFG_EMPLOyees;
Or DELETE * FROM GFG_EMPLOyees;
Update
• We can update single columns as well as
multiple columns using the UPDATE statement
as per our requirement.
• Syntax: UPDATE table_name SET column1 =
value1, column2 = value2,…
• Example: UPDATE Customer SET
CustomerName = 'Nitin' WHERE Age = 22;
Select
• The SELECT Statement in SQL is used to retrieve or fetch data.
We can fetch either the entire table or according to some
specified rules. The data returned is stored in a result table.
• To fetch any column in the table.
• Syntax:
• SELECT column1,column2 FROM table_name
• To fetch the entire table or all the fields in the table:
• Syntax:
• SELECT * FROM table_name;
• Suppose we want to see table values with
specific conditions then Where Clause is used
with select statement.
• Query:
• SELECT CustomerName FROM Customer
where Age = '21';
Data control language
• Grant
• Revoke
• Grant: To given privileges to a user account,
the GRANT statement is used.
Syntax:
• GRANT privileges_names ON object TO user;
• REVOKE privileges ON object FROM user;
Transaction Control Language
• This command is used to save all the
transactions in the DB.
• Syntax:
• COMMIT;
• For Example,
• UPDATE Student SET DOB=’2005-03-27’
WHERE Stu_Name=’Joey’;
• COMMIT;
• ROLLBACK Command:
• The “rollback” term refers to the method of undoing
changes. Thus, this command could only be used in
order to reverse transactions that occurred since
the last ROLLBACK or COMMIT command. All the
modifications must be cancelled in case any SQL
grouped statements produce a certain error.
• Syntax:
• ROLLBACK;
• SAVEPOINT Command:
• It is used to roll back a certain transaction to a
certain point rather than the entire
transaction.
• Syntax:
• SAVEPOINT Name_of_Savepoint;
Example: Savepoint s1;
Normalization
• Normalization is the process of organizing the data
in the database.
• Normalization is used to minimize the redundancy
from a relation or set of relations. It is also used to
eliminate undesirable characteristics like Insertion,
Update, and Deletion Anomalies.
• Normalization divides the larger table into smaller
and links them using relationships.
• The normal form is used to reduce redundancy from
the database table.
• Data modification anomalies can be
categorized into three types:
• Insertion Anomaly: Insertion Anomaly refers to
when one cannot insert a new tuple into a
relationship due to lack of data.
• Deletion Anomaly: The delete anomaly refers
to the situation where the deletion of data
results in the unintended loss of some other
important data.
• Updation Anomaly: The update anomaly is
when an update of a single data value requires
multiple rows of data to be updated.
1NF
• A relation will be 1NF if it contains an atomic
value.
• It states that an attribute of a table cannot
hold multiple values. It must hold only single-
valued attribute.
2NF
• To be in second normal form, a relation must be in
first normal form and relation must not contain
any partial dependency. A relation is in 2NF if it
has No Partial Dependency, i.e., no non-prime
attribute (attributes which are not part of any
candidate key) is dependent on any proper subset
of any candidate key of the table. Partial
Dependency – If the proper subset of candidate
key determines non-prime attribute, it is called
partial dependency.
3NF
• It is in the second normal form. A relation is in
3NF if at least one of the following conditions
holds in every non-trivial function dependency
X –> Y.
• X is a super key.
• Y is a prime attribute (each element of Y is part
of some candidate key).
• If A->B and B->C are two FDs then A->C is called
transitive dependency.
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,
STUD_AGE)
STATE_COUNTRY (STATE, COUNTRY)
BCNF
• BCNF is the advance version of 3NF. It is
stricter than 3NF.
• A table is in BCNF if every functional
dependency X → Y, X is the super key of the
table.
4 NF
th

• For a dependency A -> B, if for a single value


of A, multiple values of B exist, then the table
may have a multi-valued dependency. The
table should have at least 3 attributes and B
and C should be independent for A ->> B
multivalued dependency.
5 NF
th

• Properties
• A relation R is in 5NF if and only if it satisfies
the following conditions:
• 1. R should be already in 4NF.
2. It cannot be further non loss decomposed
(join dependency).
Transaction Management

• Transaction Management is a crucial aspect of


database systems that ensures data
consistency, integrity, and reliability. A
transaction is a sequence of operations (such
as reads and writes) performed on a database
that forms a single logical unit of work. The
ACID properties (Atomicity, Consistency,
Isolation, Durability) are fundamental
principles of transaction management:
• Atomicity: A transaction is atomic, meaning that it either completes
successfully and commits all its changes to the database or fails and
leaves the database unchanged. There are no partial or incomplete
transactions.
• Consistency: Transactions preserve the consistency of the database
by ensuring that it transitions from one consistent state to another
consistent state. In other words, transactions maintain data integrity
and adhere to all integrity constraints.
• Isolation: Transactions execute in isolation from each other, meaning
that the intermediate state of one transaction is not visible to other
transactions until it is committed. This prevents interference and
ensures that transactions produce consistent results.
• Durability: Once a transaction is committed, its changes are
permanently saved to the database and are not lost, even in the
event of a system failure. This ensures that committed transactions
survive system crashes and maintain data durability.

You might also like