Database Management System (DBMS)
Database Management System (DBMS)
Database types
1. Relational Database
2. NoSQL Database(Non-Relational Database)
3. Cloud Database
Database Management System (DBMS) is a software that is used to define, create, and maintain a
database and provides controlled access to the data.
1. Relational Database
The name comes from the way that data is stored in multiple, related tables. Within the tables, data is
stored in rows and columns. The relational database management system (RDBMS) is a program that
allows you to create, update, and administer a relational database. Structured Query Language (SQL) is
the most common language for reading, creating, updating, and deleting data. Column represents the
attribute while row represents the value. They are compliant with ACID (Atomicity, Consistency,
Isolation, and Durability), which is a standard set of properties for reliable database transactions.
Relational databases work well with structured data.
Examples: Microsoft SQL Server, Oracle Database, MySQL, PostgreSQL, and IBM Db2
2. NoSQL Database
NoSQL databases (aka "not only SQL") are non-tabular databases and store data differently than
relational tables. NoSQL databases come in a variety of types based on their data model. The main types
are document, key-value, wide-column, and graph. They provide flexible schemas and scale easily with
large amounts of data and high user loads, increasing data size won’t affect the performance. It is great
for organizations seeking to store unstructured or semi-structured data.
A cloud database refers to any database that’s designed to run on the cloud. Like other cloud-based
applications, cloud databases offer flexibility and scalability, along with high availability. Cloud databases
are also often low-maintenance since many are offered via a SaaS model.
Examples: Microsoft Azure SQL Database, Amazon Relational Database Service, Oracle Autonomous
Database.
Structured Query Language (SQL)
SQL is a language used for interacting with Relational Database Management Systems (RDBMS).
You can use SQL to get the RDBMS to do things for you :
o Create, retrieve, update & delete data
o Create & manage databases
o Design & create database tables
o Perform administration tasks (security, user management, import/export, etc)
SQL implementations vary between systems
o Not all RDBMS' follow the SQL standard to a 'T'
o The concepts are the same but the implementation may vary
Here are some dialects of the SQL language:
II. Data Query Language (DQL) Used to query the database for information. Get information that is
already stored there. The purely read-only SELECT query statement is classed with the DQL and
so is considered by the standard to be outside of DML. The SELECT ... INTO form is considered to
be DML because it manipulates (i.e. modifies) data. In common practice though, this distinction
is not made and SELECT is widely considered to be part of DML.
III. Data Manipulation Language (DML) Used for inserting, updating and deleting data from the
database i.e. manipulate them.
IV. Data Control Language (DCL) Used for controlling access to the data in the database. User &
permissions management.
This includes:
GRANT – gives the user or group permissions to perform certain operations on the object;
REVOKE – revokes the granted permissions;
DENY – sets a ban that has priority over the permission.
TCL operator group is designed just for the implementation and management of transactions. It
can be referred to here:
Database query: request made to the RDBMS to perform certain operation like fetching some
information/data. It is a set of instruction given to RDBMS (usually written in SQL) that tells RDBMS what
information you want it to retrieve for you.
Data Definition Language (DDL)
Create Table
The DROP TABLE operation removes the table definition and data as well as the indexes, constraints, and triggers
related to the table.
This command frees the memory space. No triggers are fired when executing DROP TABLE.
This operation cannot be rolled back in MySQL, but it can in Oracle, SQL Server, and PostgreSQL.
In SQL Server, DROP TABLE requires ALTER permission in the schema to which the table belongs; MySQL requires
the DROP privilege; Oracle the requires the DROP ANY TABLE privilege. In PostgreSQL, users can drop their own
tables.
TRUNCATE TABLE
The TRUNCATE TABLE command deletes the data inside a table, but not the table itself.
Be careful using this command as TRUNCATE transactions can be rolled back in database engines like SQL Server
and PostgreSQL, but not in MySQL and Oracle.
TRUNCATE is faster than DELETE, as it doesn't scan every record before removing it. TRUNCATE TABLE locks the
whole table to remove data from a table; thus, this command also uses less transaction space than DELETE.
Unlike DELETE, TRUNCATE does not return the number of rows deleted from the table. It also resets the table auto-
increment value to the starting value (usually 1). If you add a record after truncating the table, it will have
ID=1. Note: In PostgreSQL, you can choose to restart or continue the auto-increment value. Oracle uses a
sequence to increment values, which is not reset by TRUNCATE.
Of course, you need permission to use TRUNCATE TABLE. In PostgreSQL, you need the privilege TRUNCATE; in SQL
Server, the minimum permission is ALTER table; in MySQL, you need the DROP privilege. Finally, Oracle requires
the DROP ANY TABLE system privilege to use this command.
alter Table
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. It is also used to add
and drop various constraints on an existing table.
To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):
o To change the data type of a column in a table, use the following syntax:
MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY FK_PersonOrder;
CHANGE
MODIFY
A word of caution:
you need to specify the full column definition again when using a MODIFY query. If your column has, for
example, a DEFAULT value, or a column comment, you need to specify it in the MODIFY statement along
with the data type and the NOT NULL, or it will be lost.
The safest practice to guard against such mishaps is to copy the column definition from the output of
a SHOW CREATE TABLE YourTable query, modify it to include the NOT NULL constraint, and paste it into
your ALTER TABLE... MODIFY... query.
EMPLOYEE
DEPARTMENT
DEPARTMENT_I DEPARTMENT_NAM
D E BU_ID
101 IT 1
102 HR 1
103 ACCOUNTS 1
104 OPERATIONS 1
105 IT 2
106 HR 2
BUSINESS_UNIT
BU_ID BU_NAME
1 PAYMENTS
2 RETAIL BANKING
CORPORATE
3 BANKING
SELECT
The SELECT statement is used to select data from a database. The data returned is stored in a result table,
called the result-set.
SELECT Syntax
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 DISTINCT
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often
contains many duplicate values; and sometimes you only want to list the different (distinct) values.
WHERE Clause
The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.
WHERE Syntax
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.
AND Syntax
NOT Syntax
ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order,
use the DESC keyword.
ORDER BY Syntax
NULL Values
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.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The SELECT TOP clause is used to specify the number of records to return.The SELECT TOP clause is useful on
large tables with thousands of records. Returning a large number of records can impact performance.
MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
Oracle 12 Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
COUNT(), AVG() and SUM() Functions
The COUNT() function returns the number of rows that match a specified criterion.
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
//CONDITIONS INSIDE COUNT()
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
A wildcard character is used to substitute one or more characters in a string. There are two wildcards often used
in conjunction with the LIKE operator:
LIKE Syntax
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 '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
IN Operator
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
BETWEEN Operator
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.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Aliases
SQL aliases are used to give a table , or a column in a table, a temporary name.Aliases are often used to make
column names more readable.
An alias only exists for the duration of that query.An alias is created with the AS keyword.
SELECT column_name(s)
FROM table_name AS alias_name;
INSERT INTO Statement
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL
query. However, make sure the order of the values is in the same order as the columns in the table. Here,
the INSERT INTO syntax would be as follows:
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE Statement
DELETE Syntax
It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes,
and indexes will be intact:
If you don’t want to remove table structure or you’re only deleting specific rows, use the DELETE command. It can
remove one, some, or all rows in a table. DELETE returns the number of rows removed from the table.
However, DELETE uses a row lock during execution and can be rolled back. Every deleted row is locked, so it will
require a lot of locks if you’re working in a large table.
DELETE also keeps the auto-increment ID in the table. If you remove the last record in the table with ID=20 and
then add a new record, this record will have ID=21 – even though the record immediately before it will be ID=19.
DELETE can be executed by triggers. A trigger can be called before, after, or instead of the DELETE operation. It can
be executed for any row change or when all rows are removed. Removing rows in another table can also
trigger DELETE.
Of course, to use the DELETE command you need DELETE permission for that table.
MySQL: No MySQL: No
Oracle: No Oracle: Yes
Rollback Yes
PostgreSQL: Yes PostgreSQL: Yes
SQL Server: Yes SQL Server : Yes
Works with No
Yes No
indexed views
Space
More space Less space More space
requirements
SQL JOIN
A JOIN clause is used to combine rows multiple tables, based on a related column between them.
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left
table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
SELF JOIN: A self join is a regular join, but the table is joined with itself.
INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
LEFT JOIN
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.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT A.EMPLOYEE_ID,B.VENDOR_NAME,B.AMOUNT,B.CLAIMED_DATE
WHERE A.EMPLOYEE_ID=B.EMPLOYEE_ID(+);
Oracle outer join operator (+) allows you to perform outer joins on two or more tables
RIGHT JOIN
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.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
SELECT A.EMPLOYEE_ID,B.VENDOR_NAME,B.AMOUNT,B.CLAIMED_DATE
WHERE B.EMPLOYEE_ID=A.EMPLOYEE_ID(+);
Tip: FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Self Join
A self join is a regular join, but the table is joined with itself.
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
UNION Operator
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
UNION Syntax
WHERE condition
UNION
WHERE condition;
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement.
WHERE condition
UNION ALL
WHERE condition;
ANOTHER 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".
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group
the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
EXISTS Operator
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other
values.
ANY Operator
ANY means that the condition will be true if the operation is true for any of the values in the range.
ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
ALL Operator
ALL means that the condition will be true only if the operation is true for all values in the range.
The SELECT INTO statement copies data from one table into a new table.
SELECT INTO Syntax
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;
The new table will be created with the column-names and types as defined in the old table. You can create new
column names using the AS clause.
The INSERT INTO SELECT statement copies data from one table and inserts it into another table.
The INSERT INTO SELECT statement requires that the data types in source and target tables match.
Copy only some columns from one table into another table:
CASE Statement
The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else
statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns
the value in the ELSE clause.
CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
MySQL
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL:
Oracle
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of
the data in the table. If there is any violation between the constraint and the data action, the action is aborted. The
following constraints are commonly used in SQL:
FIRST_NAME VARCHAR(20),
LAST_NAME VARCHAR(20),
DESIGNATION VARCHAR(30),
SALARY INT ,
);
To create a NOT NULL constraint on the "Age" column when the "Persons" table is already created, use the
following SQL:
UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different. A PRIMARY KEY constraint
automatically has a UNIQUE constraint
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a column or set of
columns. However, you can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
To create a UNIQUE constraint on the "ID" column when the table is already created, use the following SQL:
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following
SQL syntax:
MySQL:
ALTER TABLE Persons
DROP INDEX UC_Person;
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column
The following SQL creates a CHECK constraint on the "Age"( must be 18, or older) column when the
"Persons" table is created.
MySQL:
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the
following SQL syntax:
To create a CHECK constraint on the "Age" column when the table is already created, use the following
SQL:
To allow naming of a CHECK constraint, and for defining a CHECK constraint on multiple columns, use the
following SQL syntax:
MySQL:
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for a column.The default value will be added to all
new records, if no other value is specified.
The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is created:
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
To create a DEFAULT constraint on the "City" column when the table is already created, use the following
SQL:
MySQL:
ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';
SQL Server:
MS Access:
Oracle:
MySQL:
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table
(Often this is the primary key).
The following SQL statement defines the "Personid" column to be an auto-increment primary key field in the
"Persons" table:
By default, the starting value for AUTO_INCREMENT is 1,to let the AUTO_INCREMENT sequence start with another
value, use the following SQL statement:
To insert a new record into the "Persons" table, we will have to use the nextval function (this function retrieves the
next value from seq_person sequence):
SUB-QUERY
A subquery (inner query) is a SQL query nested inside a larger query(outer query).
The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another
subquery.
A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the
comparison operators, such as >, <, or =. The comparison operator can also be a multiple-row operator,
ON DELETE
1. CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in
the child table.
2. SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the child
table to NULL.
Query Visualization:
SARGABLE (Search ARGument ABLE) queries:
Queries that can use indexing for faster execution
Essential for optimizing DB performance
Example
A primary key is a column or set of columns of a table that helps to identify every record present in that
table uniquely. There can be only one primary Key in a table.
The PRIMARY KEY (PK) constraint put on a column or set of columns will not allow them to have any null
values or any duplicates
Foreign Key: It is a primary key of another related table. It helps us to define relationship between two
related tables.
Super Key
Super Key is the set of all the keys that help to identify rows in a table uniquely. This means that all
those columns of a table that are capable of identifying the other columns of that table uniquely will all
be considered super keys.
Super Key is the superset of a candidate key (explained below). The Primary Key of a table is picked from
the super key set to be made the table’s identity attribute.
Max no. of possible super keys= (2n-1), where n is the number of columns (depends on the attributes)
A B C D
1 1 5 1
2 1 5 1
3 1 7 1
4 2 7 1
5 2 5 1
No of Super keys in the above table=8 i.e. {A}, {AB}, {AC}, {AD}, {ABC}, {ACD}, {ABD}, {ABCD}
Candidate Key
Candidate keys are those attributes that uniquely identify rows of a table. The Primary Key of a table is
selected from one of the candidate keys. So, candidate keys have the same properties as the primary
keys explained above. There can be more than one candidate keys in a table.
Candidate key is a super key whose proper subset is not a super key
Example:
A B C
1 1 1
2 1 2
3 2 1
4 2 2
S1= {1, 2, 3}
S2= {1, 2}
In our example, A, AB, AC, ABC, BC are the super keys and A, BC are the candidate keys.
Foreign Key
Foreign Key is used to establish relationships between two tables. A foreign key will require each value
in a column or set of columns to match the Primary Key of the referential table. Foreign keys help to
maintain data and referential integrity
Composite Key
Composite Key is a set of two or more attributes that help identify each tuple in a table uniquely. The
attributes in the set may not be unique when considered separately. However, when taken all together,
they will ensure uniqueness.
Alternate Key
As stated above, a table can have multiple choices for a primary key; however, it can choose only one.
So, all the keys which did not become the primary Key are called alternate keys
Unique Key
Unique Key is a column or set of columns that uniquely identify each record in a table. All values will
have to be unique in this Key. A unique Key differs from a primary key because it can have only one null
value, whereas a primary Key cannot have any null values.
Surrogate Key
An artificial key which aims to uniquely identify each record is called a surrogate key. These kind of key
are unique because they are created when you don’t have any natural primary key.
Trigger
cascade
Views
indexes
ACID