DBMS & SQL
DBMS & SQL
1. Database
A Database is a collection of related data organised in a way that data can be
easily accessed, managed and updated. Databases can be software based or
hardware based, with one sole purpose, storing data.
2. DBMS
A DBMS is a software that allows creation, definition and manipulation of
databases, allowing users to store, process and analyse data easily. DBMS
provides us with an interface or a tool, to perform various operations like
creating a database, storing data in it, updating data, creating tables in the
database.DBMS also provides protection and security to the databases. It
also maintains data consistency in case of multiple users.
4. Keys
Super Key - The set of attributes which can uniquely identify a tuple is known
as super key.
Candidate Key - The minimal set of attributes which can uniquely identify a
tuple is known as candidate key.
Primary Key - There can be more than one candidate key in relation, out of
which one can be chosen as primary key.
Alternate Key - The candidate key other than primary key is known as
alternate key.
5. Transaction
A transaction can be defined as a group of tasks. It is a very small unit of a
program and it may contain several low level tasks. A transaction must
maintain ACID properties in order to ensure accuracy, completeness and data
integrity.
6. States of Transaction
● Active
● Partially committed
● Failed
● Aborted
● Committed
7. Normalization
It is a technique of organizing the data in the database. Systematic approach
of decomposing tables to eliminate data redundancy and undesirable
characteristics like insertion,update and deletion anomalies.
8. Normalization rule
Normalization rules are divided into the following normal forms
● First Normal Form (1NF)
● Second Normal Form (2NF)
● Third Normal Form (3NF)
● Boyce and Codd Normal Form (BCNF)
● Fourth Normal Form (4NF)
● Fifth Normal Form (5NF)
Multi-valued dependency
For a dependency A->B, if for a single value of A, multiple values of B exist,
then the table may have multi-valued dependency. Table should have at
least 3 columns and for a relation R(A,B,C) if there is a multi-valued
dependency between A and B, then B and C should be independent of each
other.
View Level: The View Level is the highest level of abstraction and it describes
only a part of the entire database.
Unique Key can have a NULL value The primary key cannot have a
NULL value
Each table can have more than one Each table can have only one
unique key primary key
Procedure Function
RETURN will simply exit the control RETURN will exit the control from
from sub program sub program and also returns the
value
Return data type will not be specified Return data type is mandatory at the
at the time of creation time of creation
22. Indexing
Indexing is a data structure technique to efficiently retrieve records from the
database files based on some attributes on which the indexing has been
done.
Types of indexing
Primary Index − Primary index is defined on an ordered data file. The data
file is ordered on a key field. The key field is generally the primary key of the
relation.
Secondary Index − Secondary index may be generated from a field which is
a candidate key and has a unique value in every record, or a non-key with
duplicate values.
Clustering Index − Clustering index is defined on an ordered data file. The
data file is ordered on a non-key field.
23. Hashing
Hashing is an effective technique to calculate the direct location of a data
record on the disk without using index structure.Hashing uses hash functions
with search keys as parameters to generate the address of a data record.
Types of hashing
Static Hashing - In static hashing, when a search-key value is provided, the
hash function always computes the same address.
Dynamic Hashing - The problem with static hashing is that it does not
expand or shrink dynamically as the size of the database grows or shrinks.
Dynamic hashing provides a mechanism in which data buckets are added and
removed dynamically and on-demand. Dynamic hashing is also known as
extended hashing.
24. Deadlock
Deadlock is a situation which occurs when two transactions wait on a
resource which is locked or other transaction holds. Deadlocks can be
prevented by making all the transactions acquire all the locks at the same
instance of time. So, once a deadlock occurs, the only way to cure it is to
abort one of the transactions and remove the partially completed work.
An exclusive lock is a lock on a data A shared lock allows more than one
item when a transaction is about to transaction to read the data items.
perform the write operation.
SQL
1. SQL
Structured Query Language is the core of the relational database which is
used for accessing and managing the databases. This language is used to
manipulate and retrieve data from a structured data format in the form of
tables and holds relationships between those tables. So, SQL is used to
communicate with the database.
2. Sub-queries
A subquery is a query inside another query where a query is defined to
retrieve data or information back from the database. In a subquery, the outer
query is called the main query whereas the inner query is called subquery.
Subqueries are always executed first and the result of the subquery is passed
on to the main query. It can be nested inside a SELECT, UPDATE or any
other query. A subquery can also use any comparison operators such as >,<
or =.
3. Data Definition Language(DDL)
DDL changes the structure of the table like creating a table, deleting a table,
altering a table.All the commands of DDL are auto-committed that means it
permanently saves all the changes in the database.
DDL Commands
1. Create - It is used to create a new database or table.
CREATE TABLE TABLE_NAME (COLUMN1 DATATYPE, COLUMN2 DATATYPE,...);
3.Truncate - It is used to delete all the rows from the table and free the space
containing the table.
TRUNCATE TABLE TABLE_NAME;
4. Drop - It is used to delete both the structure and record stored in the table.
DROP TABLE TABLE_NAME;
DML Commands
1. Insert - It is used to insert data into the row of a table.
INSERT INTO TABLE_NAME VALUES(VALUE1,VALUE2,VALUE3,...);
TCL Commands
1. Commit - Commit command is used to save all the transactions to the
database.
COMMIT;
DCL Commands
1. Grant - It is used to give user access privileges to a database.
2. Revoke - It is used to take back permissions from the user.
DQL Command
1. Select - It is used to select the attribute based on the condition described
by WHERE clause.
SELECT COLUMN1,COLUMN2 FROM TABLE_NAME;
WHERE HAVING
14. Distinct
The distinct keyword is used with the SELECT statement to retrieve unique
values from the table. Distinct removes all the duplicate records while
retrieving records from any table in the database.
SELECT DISTINCT column-name FROM table-name;
Natural Join joins two tables based Inner join joins two tables on the
on the same attribute name and basis of the column which is
datatypes. explicitly specified in the ON clause.
The resulting table will contain all the The resulting table will contain all the
attributes of both the tables but keep attributes of both the tables including
only one copy of each common duplicate columns also.
column.
When attributes are not common It does not depend upon the
then it will return nothing. common attributes. If the attribute is
blank then NULL is placed there.
Join Union
Combines data from many tables Combines the result set of two or
based on a matched condition more select statements.
between them.
It combines data into new columns. It combines data into new rows.
25. View
A VIEW in SQL is a logical subset of data from one or more tables. View is
used to restrict data access.
CREATE or REPLACE VIEW view_name
AS
SELECT column_name(s)
FROM table_name
WHERE condition
Types of views
● Simple
● Complex
26. SQL Alias
Alias is used to give an alias name to a table or a column, which can be a
resultset table too. This is quite useful in case of large or complex queries.
Alias is mainly used for giving a short alias name for a column or a table with
complex names.
SELECT column-name FROM table-name AS alias-name
Constraints are used to make sure that the integrity of data is maintained in
the database. Following are the most used constraints that can be applied to a
table.
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK
6. DEFAULT