DBMS
DBMS
What is Database
The database is a collection of inter-related data which is used to retrieve, insert and delete the
data efficiently. It is also used to organize the data in the form of a table, schema, views, and
reports, etc.
For example: MySQL, Oracle
De
• Controls database redundancy
• Data sharing
• Backup
• Reduce time
5.Disadvantages of DBMS
e_
•
•
•
Size,
Complexity,
Higher impact of failure, cod
es
• Cost of Hardware and Software
6. Three schema Architecture
• Internal Level (or) Physical Schema - describes the physical storage structure
of the database.
• External Level (or) View Schema – a database contains several schemas that
sometimes called as subschema. The subschema is used to describe the
different view of the database.
7. ER (Entity Relationship) Diagram in DBMS
• 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.
• Entity - n entity may be any object,
class, person or place. In the ER diagram,
an entity can be represented as rectangles.
◦ Weak Entity - An entity that
depends on another entity called a
weak entity.
De
◦ Composite Attribute - composed of many other attributes is known as a composite
attribute.
◦ Multivalued Attribute - An attribute can have more than one value. The double oval is
e_
used to represent multivalued attribute.
◦ Derived Attribute - An attribute that can be derived from other attribute is known as a
derived attribute. represented by a dashed ellipse.
• Relationship
cod
◦ A relationship is used to describe the relation between entities. Diamond or rhombus is
used to represent the relationship.
es
◦ One-to-One Relationship - When only one instance of an entity is associated with the
relationship
◦ 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.
◦ 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.
◦ 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.
8. Notations of ER Diagrams .
De
e_
cod
9. Keys
es
1. Primary key - It is the first key used to identify one and only one instance of an entity uniquely
2. Candidate key - A candidate key is an attribute or set of attributes that can uniquely identify a
tuple. Except for the primary key, the remaining attributes are considered a candidate key.
3. Super key - Super key is an attribute set that can uniquely identify a tuple. A super key is a
superset of a candidate key.
4. Foreign keys - Foreign keys are the column of the table used to point to the primary key of
another table.
5. Alternate key - One key is chosen as the primary key from these candidate keys, and the
remaining candidate key, if it exists, is termed the alternate key. If there is only one candidate key in
a relation, it does not have an alternate key.
6. Composite key (or) Concatenated Key - Whenever a primary key consists of more than one
attribute
7. Artificial key - The key created using arbitrarily assigned data are known as artificial key ,
usually numbered in a serial order.
10. What is Normalization?
• Normalization is the process of organizing the data in the database.
• Normalization divides the larger table into smaller and links them using relationships.
• The normal form is used to reduce redundancy from the database table.
11. Why do we need Normalization?
The main reason for normalizing the relations is removing the anomalies. Failure to eliminate
anomalies leads to data redundancy and can cause data integrity and other problems as the database
grows.
De
12. Data modification anomalies can be categorized into three types:
• Insertion Anomaly: one cannot insert a new tuple into a relationship due to lack of data.
• Deletion Anomaly: the deletion of data results in the unintended loss of some other
e_
important data.
• Updatation Anomaly: ]when an update of a single data value requires multiple rows of data
to be updated.
cod
13. Types of Normal Forms:
Normal
Description
Form
1NF A relation is in 1NF if it contains an atomic value.
es
A relation will be in 2NF if it is in 1NF and all non-key attributes are fully functional
2NF
dependent on the primary key.
3NF A relation will be in 3NF if it is in 2NF and no transition dependency exists.
BCNF A stronger definition of 3NF is known as Boyce Codd's normal form.
A relation will be in 4NF if it is in Boyce Codd's normal form and has no multi-
4NF
valued dependency.
A relation is in 5NF. If it is in 4NF and does not contain any join dependency, joining
5NF
should be lossless.
De
• 3NF is used to reduce the data duplication.
19. Boyce Codd normal form (BCNF) (or) 3.5NF (A 3.5NF table should have the below
conditions)
e_
• BCNF is the advance version of 3NF.
• if every functional dependency X → Y, X is the super key of the table.
cod
20. Fourth Normal Form (or)4NF (A 4NF table should have the below conditions)
• A relation will be in 4NF if it is in Boyce Codd normal form and has no multi-valued
dependency.
• For a dependency A → B, if for a single value of A, multiple values of B exists, then the
es
relation will be a multi-valued dependency.
20. Fifth Normal Form (or)5NF (or) Project-join normal form (PJ/NF) (A 5NF table should
have the below conditions)
• A relation is in 5NF if it is in 4NF and not contains any join dependency and joining
should be lossless.
• satisfied when all the tables are broken into as many tables.
21. Deadlock in DBMS
A deadlock is a condition where two or more transactions are waiting indefinitely for one another to
give up locks.
22.Deadlock Avoidance
• When a database is stuck in a deadlock state, then it is better to avoid the database rather
than aborting or restating the database.
• Deadlock avoidance mechanism is used to detect any deadlock situation in advance.
23.Deadlock Detection
• When a transaction waits indefinitely to obtain a lock, then the DBMS should detect
whether the transaction is involved in a deadlock or not.
• To detect deadlock, “Wait-for-graph” is used.
24. Wait-for-graph
• A graph is created based on the transaction and their lock.
• If the created graph has a cycle or closed loop, then there is a deadlock.
25. Deadlock Prevention
• Deadlock prevention method is suitable for a large database.
• If the resources are allocated in such a way that deadlock never occurs, then the deadlock
De
can be prevented.
26. SQL
• SQL stands for Structured Query Language. It is used for storing and managing data in
e_
relational database management system
Example : MySQL, Informix, Oracle,
cod
27. Characteristics of SQL
• SQL is used to access data from relational database management systems.
• SQL can execute queries against the database.
es
• SQL is used to describe the data.
28. Advantages Of SQL
• Well defined standards – followed by the guidelines of ISO and ANSI formats.
• Portability - used in laptop, PCs, server and even some mobile phones.
29. ACID Properties
29. Types of SQL Commands
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
De
e_
30. Data Definition Language (DDL)
cod
•
etc.
es
DDL changes the structure of the table like creating a table, deleting a table, altering a table,
• All the command of DDL are auto-committed that means it permanently save all the changes
in the database.
a. CREATE It is used to create a new table in the database.
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
b. DROP: It is used to delete both the structure and record stored in the table.
DROP TABLE table_name;
c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
ALTER TABLE table_name ADD column_name COLUMN-definition;
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
TRUNCATE TABLE table_name;
31. Data Manipulation Language (DML)
• DML commands are used to modify the database. It is responsible for all form of changes in
the database.
• a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2,
value3, .... valueN);
• b. UPDATE: This command is used to update or modify the value of a column in the table.
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE
CONDITION]
• c. DELETE: It is used to remove one or more row from a table.
De
DELETE FROM table_name [WHERE condition];
32. Data Control Language (DCL)
e_
• DCL commands are used to grant and take back authority from any database user.
• a. Grant: It is used to give user access privileges to a database.
cod
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
• b. Revoke: It is used to take back permissions from the user.
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
33. Transaction Control Language
•
•
es
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.
a. Commit: Commit command is used to save all the transactions to the database.
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
ROLLBACK;
• c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back
the entire transaction.
SAVEPOINT SAVEPOINT_NAME;
34. Data Query Language
• DQL is used to fetch the data from the database.
• a. SELECT: It is used to select the attribute based on the condition described by WHERE
clause.
SELECT expressions FROM TABLES WHERE conditions;
35. 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.
• CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name
WHERE condition;
36. Types of SQL JOIN
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
De
37. INNER JOIN
INNER JOIN selects records that have matching values in both tables as long as the condition is
e_
satisfied.
SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 INNER JOIN
table2 ON table1.matching_column = table2.matching_column;
cod
38. LEFT JOIN
• The SQL left join returns all the values from left table and the matching values from the
right table.
es
• If there is no matching join value, it will return NULL.
SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 LEFT
JOIN table2 ON table1.matching_column = table2.matching_column;
39. RIGHT JOIN
• In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and
the matched values from the left table.
• If there is no matching in both tables, it will return NULL.
SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 RIGHT
JOIN table2 ON table1.matching_column = table2.matching_column;
40. FULL JOIN
• In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join
tables have all the records from both tables. It puts NULL on the place of matches not found.
SELECT table1.column1, table1.column2, table2.column1,.... FROM table1 FULL JOIN
table2 ON table1.matching_column = table2.matching_column;
41. Subquery in SQL
• A Subquery is a query within another SQL query and embedded within the WHERE clause.
• A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause,
HAVING clause.
• You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
42. SQL Aggregate functions
SQL aggregation function is used to perform the calculations on multiple rows of a single column
of a table. It returns a single value.
1. COUNT FUNCTION
De
• Used to Count the number of rows in a database table. Accepts both numeric and non-
numeric.
• COUNT(*) : Syntax
e_
• SELECT COUNT(*) FROM TABLE_NAME; : Example
2. SUM Function
cod
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
• SUM() : Syntax
• SELECT SUM() FROM TABLE_NAME; : Example
es
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns
the average of all non-Null values.
• AVG()
• SELECT AVG(COST) FROM PRODUCT_MAST;
4. MAX function
MAX function is used to find the maximum value of a certain column.
This function determines the largest value of all selected values of a column.
• MAX()
• SELECT MAX(RATE) FROM PRODUCT_MAST;
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines the
smallest value of all selected values of a column.
• MIN()
• SELECT MIN(RATE) FROM PRODUCT_MAST;