DBMS & SQL Notes
DBMS & SQL Notes
DBMS & SQL Notes
4. Integrity problems
5. Atomicity of updates
7. Security problems
ER diagram:
Entity Set:
o A strong entity set is an entity set that contains sufficient attributes to uniquely
identify all its entities.
o A weak entity set is an entity set that does not contain sufficient attributes to
uniquely identify its entities.
o In other words, a primary key does not exist for a weak entity set.
o However, it contains a partial key called a discriminator.
Relationship:
● Unary Relationship Set – Unary relationship set is a relationship set where only
one entity set participates in a relationship set.
● N-ary Relationship Set – N-ary relationship set is a relationship set where ‘n’
entity sets participate in a relationship set.
Cardinality Constraint:
Attributes:
Attributes are the descriptive properties that are owned by each entity of an Entity
Set. Types of Attributes:
● Simple Attributes – Simple attributes are those attributes that cannot be divided
further. Ex. Age
● Derived Attributes – Derived attributes are those attributes that can be derived
from other attributes. Ex. Age can be derived from DOB.
● Key Attributes – Key attributes are those attributes that can identify an entity
uniquely in an entity set. Ex. Roll No.
Constraints:
Relational constraints are the restrictions imposed on the database contents and
operations. They ensure the correctness of data in the database.
● Domain Constraint – Domain constraint defines the domain or set of values for
an attribute. It specifies that the value taken by the attribute must be the atomic
value from its domain.
● Key Constraint – All the values of the primary key must be unique. The value
of the primary key must not be null.
● Referential Integrity Constraint – It specifies that all the values taken by the
foreign key must either be available with the primary key or be null.
The set of all those attributes which can be functionally determined from an
attribute set is called the closure of that attribute set.
Keys:
A key is a set of attributes that can identify each tuple uniquely in the given
relation. Types of Keys:
● Super Key – A superkey is a set of attributes that can identify each tuple
uniquely in the given relation. A super key may consist of any number of
attributes.
● Candidate Key – A set of the minimal attribute(s) that can identify each tuple
uniquely in the given relation is called a candidate key.
● Primary Key – A primary key is a candidate key that the database designer
selects while designing the database. Primary Keys are unique and NOT NULL.
● Alternate Key – Candidate keys that are left unimplemented or unused after
implementing the primary key are called alternate keys.
● Foreign Key – An attribute ‘X’ is called a foreign key to some other attribute
‘Y’ when its values are dependent on the values of attribute ‘Y’. The relation in
which attribute ‘Y’ is present is called the referenced relation. The relation in
which attribute ‘X’ is present is called the referencing relation.
● Composite Key – A primary key composed of multiple attributes and not just a
single attribute is called a composite key.
● Unique Key – It is unique for all the records of the table. Once assigned, its
value cannot be changed i.e. it is non-updatable. It may have a NULL value.
Functional Dependency:
In any relation, a functional dependency α → β holds if- Two tuples having the
same value of attribute α also have the same value for attribute β.
Decomposition of a Relation:
The process of breaking up or dividing a single relation into two or more sub
relations is called the decomposition of a relation.
Properties of Decomposition:
o None of the functional dependencies that hold on the original relation are lost. o
The sub relations still hold or satisfy the functional dependencies of the original
relation.
Types of Decomposition:
o, Consider there is a relation R which is decomposed into sub relations R1, R2,
…., Rn.
o This decomposition is called lossless join decomposition when the join of the sub
relations results in the same relation R that was decomposed.
o, Consider there is a relation R which is decomposed into sub relations R1, R2,
…., Rn.
o This decomposition is called lossy join decomposition when the join of the sub
relations does not result in the same relation R that was decomposed.
Normalization:
Normal Forms:
● First Normal Form (1NF) – A given relation is called in First Normal Form
(1NF) if each cell of the table contains only an atomic value i.e. if the attribute of
every tuple is either single-valued or a null value.
A → B is called a transitive dependency if and only if- A is not a super key and B
is a non-prime attribute.
Transaction:
● Read Operation – Read(A) instruction will read the value of ‘A’ from the
database and will store it in the buffer in the main memory.
● Write Operation – Write(A) will write the updated value of ‘A’ from the buffer
to the database.
Transaction States:
● Active State –
o All the changes made by the transaction now are stored in the buffer in the main
memory.
o After the last instruction of the transaction has been executed, it enters into a
partially committed state.
o It is not considered fully committed because all the changes made by the
transaction are still stored in the buffer in the main memory.
● Committed State –
o After all the changes made by the transaction have been successfully stored into
the database, it enters into a committed state.
● Aborted State –
o After the transaction has failed and entered into a failed state, all the changes
made by it have to be undone.
o To undo the changes made by the transaction, it becomes necessary to roll back
the transaction.
o After the transaction has rolled back completely, it enters into an aborted state. ●
Terminated State –
o After entering the committed state or aborted state, the transaction finally enters
into a terminated state where its life cycle finally comes to an end.
ACID Properties:
To ensure the consistency of the database, certain properties are followed by all the
transactions occurring in the system. These properties are called ACID Properties
of a transaction.
● Atomicity –
o This property ensures that either the transaction occurs completely or it does not
occur at all.
o In other words, it ensures that the database remains consistent before and after
the transaction.
● Isolation –
o The resultant state of the system after executing all the transactions is the same as
the state that would be achieved if the transactions were executed serially one after
the other.
● Durability –
o This property ensures that all the changes made by a transaction after its
successful execution are written successfully to the disk.
o It also ensures that these changes exist permanently and are never lost even if
there occurs a failure of any kind.
Schedules:
The order in which the operations of multiple transactions appear for execution is
called a schedule.
● Serial Schedules –
● Non-Serial Schedules –
Serializability –
● Serializable Schedules –
o If a given non-serial schedule of ‘n’ transactions is equivalent to some serial
schedule of ‘n’ transactions, then it is called a serializable schedule.
Types of Serializability –
Non-Serializable Schedules –
● Irrecoverable Schedules –
If in a schedule,
● Recoverable Schedules –
If in a schedule,
o A transaction performs a dirty read operation from an uncommitted transaction o
And its commit operation is delayed till the uncommitted transaction either
commits or rollbacks
Relational Algebra:
R1-R2 returns those tuples which are in R1 but not in R2. Max
−(Minus)
no. of rows returned = m and Min no. of rows returned = min
Extended
Semantic
Operator
Returns those tuples which are in both R1 and R2. Max no. of
∩ (Intersection)
rows returned = min(m,n) and Min no. of rows returned = 0
File Structures:
● Primary Index: A primary index is an ordered file, records of fixed length with
two fields. The first field is the same as the primary key as a data file and the
second field is a pointer to the data block, where the key is available. The average
number of block accesses using index = log2 Bi + 1, where Bi = number of index
blocks.
● Clustering Index: A clustering index is created on a data file whose records are
physically ordered on a non-key field (called the Clustering field).
B Trees
At every level, we have Key and Data Pointer and data pointer points to either
block or record.
Properties of B-Trees:
The root of the B-tree can have children between 2 and P, where P is the Order of
the tree. Order of tree – Maximum number of children a node can have.
B+ Trees
In B+ trees, the structure of leaf and non-leaf are different, so their order is. Order
of non-leaf will be higher as compared to leaf nodes.
Searching time will be less in B+ trees since it doesn’t have record pointers in
non-leaf because of which depth will decrease.
SQL
DDL:
DDL is the short name of 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)
● TRUNCATE – remove all records from a table, including all spaces allocated for
the records are removed
DML:
DML is the short name of 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.
DCL is the short name of Data Control Language which includes commands such
as GRANT and is mostly concerned with rights, permissions, and other controls of
the database system.
TCL:
TCL is the short name of Transaction Control Language which deals with a
transaction within a database.
● SAVEPOINT – to roll back the transaction making points within groups SQL:
SELECT:
Syntax –
FROM table_name;
● Here, column1, column2, … are the field names of the table you want to select
data from. If you want to select all the fields available in the table, use the
following syntax: ● SELECT * FROM table_name;
Ex –
SELECT DISTINCT:
FROM table_name;
Ex –
WHERE:
Syntax –
FROM table_name
WHERE condition;
Ex –
● SELECT * FROM Customers
WHERE Country=’Mexico’;
Operator Description
= Equal
Not equal. Note: In some versions of SQL this operator may be written
<>
as !=
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
● The AND operator displays a record if all the conditions separated by AND are
TRUE. ● The OR operator displays a record if any of the conditions separated by
OR is TRUE.
Syntax –
FROM table_name
FROM table_name
FROM table_name
Ex –
ORDER BY:
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
Syntax –
FROM table_name
Ex –
ORDER BY Country;
INSERT INTO:
Syntax –
● INSERT INTO table_name (column1, column2, column3, …)
*In the second syntax, make sure the order of the values is in the same order as the
columns in the table.
Ex –
NULL Value:
It is not possible to test for NULL values with comparison operators, such as =, <,
or <>. We will have to use the IS NULL and IS NOT NULL operators instead.
Syntax –
● SELECT column_names
FROM table_name
● SELECT column_names
FROM table_name
Ex –
FROM Customers
UPDATE:
The UPDATE statement is used to modify the existing records in a table. Syntax –
● UPDATE table_name
WHERE condition;
Ex –
● UPDATE Customers
WHERE CustomerID = 1;
DELETE:
In 2ndsyntax, all rows are deleted. The table structure, attributes, and indexes will
be intact Ex –
SELECT TOP:
The SELECT TOP clause is used to specify the number of records to return.
Syntax –
FROM table_name
WHERE condition;
● SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
● SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
● SELECT column_name(s)
FROM table_name
*In case the interviewer asks other than the TOP, rest are also correct. (Diff. DB
Systems) Ex –
LIMIT 3;
Aggregate Functions:
MIN():
The MIN() function returns the smallest value of the selected column. Syntax –
● SELECT MIN(column_name)
FROM table_name
WHERE condition;
Ex –
FROM Products;
MAX():
The MAX() function returns the largest value of the selected column. Syntax –
● SELECT MAX(column_name)
FROM table_name
WHERE condition;
Ex –
FROM Products;
COUNT():
The COUNT() function returns the number of rows that matches a specified
criterion. Syntax –
● SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Ex –
● SELECT COUNT(ProductID)
FROM Products;
AVG():
Syntax –
● SELECT AVG(column_name)
FROM table_name
WHERE condition;
Ex –
● SELECT AVG(Price)
FROM Products;
SUM():
Syntax –
● SELECT SUM(column_name)
FROM table_name
WHERE condition;
Ex –
● SELECT SUM(Quantity)
FROM OrderDetails;
LIKE Operator:
The LIKE operator is used in a WHERE clause to search for a specified pattern in
a column. There are two wildcards often used in conjunction with the LIKE
operator:
Syntax –
FROM table_name
WHERE CustomerName LIKE ‘a%’ Finds any values that start with “a”
WHERE CustomerName LIKE ‘%a’ Finds any values that end with “a”
WHERE CustomerName LIKE Finds any values that have “or” in any
‘%or%’ position
WHERE CustomerName Finds any values that have “r” in the second
LIKE ‘_r%’ position
WHERE CustomerName Finds any values that start with “a” and are at least
LIKE ‘a_%’ 2 characters in length
WHERE CustomerName Finds any values that start with “a” and are at least
LIKE ‘a__%’ 3 characters in length
WHERE ContactName LIKE Finds any values that start with “a” and ends with
‘a%o’ “o”
IN:
The IN operator allows you to specify multiple values in a WHERE clause. The IN
operator is a shorthand for multiple OR conditions.
Syntax –
● SELECT column_name(s)
FROM table_name
● SELECT column_name(s)
FROM table_name
Ex –
BETWEEN:
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
Syntax –
● SELECT column_name(s)
FROM table_name
Ex –
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
INNER JOIN:
The INNER JOIN keyword selects records that have matching values in both
tables. Syntax –
● SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
Ex –
FROM Orders
The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the
right side if there is no match.
Syntax –
● SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
Ex –
FROM Customers
ORDER BY Customers.CustomerName;
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matching records from the left table (table1). The result is 0 records from the left
side if there is no match.
Syntax –
● SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name;
Ex –
The FULL OUTER JOIN keyword returns all records when there is a match in the
left (table1) or right (table2) table records.
Syntax:
● SELECT column_name(s)
FROM table1
ON table1.column_name = table2.column_name
WHERE condition;
Ex –
FROM Customers
The UNION operator is used to combine the result-set of two or more SELECT
statements.
● Every SELECT statement within UNION must have the same number of
columns ● The columns must also have similar data types
● The columns in every SELECT statement must also be in the same order
The UNION operator selects only distinct values by default. To allow duplicate
values, use UNION ALL
Syntax –
UNION
UNION ALL
Ex –
UNION
GROUP BY:
The GROUP BY statement groups rows that have the same values into summary
rows, like “find the number of customers in each country”.
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more
columns. Syntax –
● SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Ex –
FROM Customers
GROUP BY Country
HAVING:
The HAVING clause was added to SQL because the WHERE keyword cannot be
used with aggregate functions.
Syntax –
● SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Ex –
FROM Customers
GROUP BY Country
CREATE DATABASE:
DROP DATABASE:
CREATE TABLE:
Syntax –
column1 datatype,
column2 datatype,
column3 datatype,
….
);
DROP TABLE:
The TRUNCATE TABLE statement is used to delete the data inside a table, but not
the table itself. Syntax –
ALTER TABLE:
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
Syntax –
Ex –