Professional Documents
Culture Documents
RDBMS
RDBMS
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for
all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
Data is represented in terms of tuples (rows) in RDBMS. Relational database is most commonly
used database. It contains number of tables and each table has its own primary key. Due to a
collection of organized set of tables, data can be accessed easily in RDBMS.
2. Record or Tuple or Row - Each row of a table is known as record. It is also known as tuple. A
record is also called as a row of data is each individual entry that exists in a table.
3. Field or Column name or Attribute - A column is a vertical entity in a table that contains all
information associated with a specific field in a table. In general, an attribute is a characteristic. In
a database management system (DBMS), an attribute refers to a database component, such as a
table. It also may refer to a database field. Attributes describe the instances in the row of a
database.
4. Domain - A domain is a set of permitted values for an attribute in table. For example, a domain
of month-of-year can accept January, February,…December as values, a domain of dates can
accept all possible valid dates etc. We specify domain of attribute while creating a table.
An attribute cannot accept values that are outside of their domains. For example, In the table
“STUDENT”, the Student_Id field has integer domain so that field cannot accept values that are
not integers for example, Student_Id cannot has values like, “First”, 10.11 etc.
5. Instance - The data stored in database at a particular moment of time is called instance of
database.
6. Schema - A relation schema describes the structure of the relation, with the name of the
relation(name of table), its attributes and their names and type.
7. Keys - A relation key is an attribute which can uniquely identify a particular tuple(row) in a
relation(table).
DBMS Schema
A database schema is the skeleton structure that represents the logical view of the entire
database. It defines how the data is organized and how the relations among them are associated.
It formulates all the constraints that are to be applied on the data.
A database schema defines its entities and the relationship among them. It contains a descriptive
detail of the database, which can be depicted by means of schema diagrams. It’s the database
designers who design the schema to help programmers understand the database and make it
useful.
Schema is of three types:
Relational Algebra
Relational Algebra is procedural query language, which takes Relation as input and generate
relation as output. Relational algebra mainly provides theoretical foundation for relational
databases and SQL.
3. Union (U) - Union operation in relational algebra is same as union operation in set theory,
only constraint is for union of two relation both relation must have same set of Attributes.
4. Set Difference (-) - Set Difference in relational algebra is same set difference operation as in
set theory with the constraint that both relation should have same set of attributes.
5. Rename (ρ) - Rename is a unary operation used for renaming attributes of a relation.
6. Cross Product (X) - Cross product between two relations let say A and B, so cross product
between A X B will results all the attributes of A followed by each attribute of B. Each
record of A will pairs with every record of B.
7. Natural Join (⋈) - Natural join is a binary operator. Natural join between two or more
relations will result set of all combination of tuples where they have equal common
attribute.
8. Conditional Join - Conditional join works similar to natural join. In natural join, by default
condition is equal between common attribute while in conditional join we can specify the
any condition such as greater than, less than, not equal
Relational Calculus
Contrary to Relational Algebra which is a procedural query language to fetch data and which also
explains how it is done, Relational Calculus in non-procedural query language and has no
description about how the query will work or the data will b fetched. It only focusses on what to
do, and not on how to do it.
Relational Calculus exists in two forms:
1. Tupple Relational Calculus - The tuple relational calculus is specified to select the tuples in a
relation. In TRC, filtering variable uses the tuples of a relation.
In tuple relational calculus, we work on filtering tuples based on the given condition.
Syntax: { T | Condition }
In this form of relational calculus, we define a tuple variable, specify the table(relation) name in
which the tuple is to be searched for, along with a condition.
We can also specify column name using a . dot operator, with the tuple variable to only get a
certain attribute(column) in result.
A tuple variable is nothing but a name, can be anything, generally we use a single alphabet for
this, so let's say T is a tuple variable.
To specify the name of the relation(table) in which we want to look for data, we do the following:
Relation(T), where T is our tuple variable.
For example if our table is Student, we would put it as Student(T)
Then comes the condition part, to specify a condition applicable for a particluar
attribute(column), we can use the . dot variable with the tuple variable to specify it, like in
table Student, if we want to get data for students with age greater than 17, then, we can write it
as,
T.age > 17, where T is our tuple variable.
Putting it all together, if we want to use Tuple Relational Calculus to fetch names of students,
from table Student, with age greater than 17, then, for T being our tuple variable,
T.name | Student(T) AND T.age > 17
2. Domain Relational Calculus (DRC) - In domain relational calculus, filtering variable uses the
domain of attributes.
Domain relational calculus uses the same operators as tuple calculus. It uses logical connectives ∧
(and), ∨ (or) and ┓ (not).
It uses Existential (∃) and Universal Quantifiers (∀) to bind the variable.
Syntax: { c1, c2, c3, ..., cn | F(c1, c2, c3, ... ,cn)}
where, c1, c2... etc represents domain of attributes(columns) and F defines the formula including
the condition for fetching the data.
For example,
{< name, age > | ∈ Student ∧ age > 17}
Again, the above query will return the names and ages of the students in the table Student who
are older than 17.
Types of constraints
1. NOT NULL
2. UNIQUE
3. DEFAULT
4. CHECK
5. Key Constraints – Key constraint specifies that in any relation, all the values of the primary key
must be unique and no value must be null. PRIMARY KEY, FOREIGN KEY
Mapping constraints: One to One, One to Many, Many to One, Many to Many.
6. Domain constraint - Each table has certain set of columns and each column allows a same type
of data, based on its data type. The column does not accept values of any other data type. In
the DBMS the constraints have important role. Domain constraints are user defined data type.
We can describe
Domain Constraint = data type + Constraints (NOT NULL / UNIQUE / PRIMARY KEY / FOREIGN KEY
/ CHECK / DEFAULT)
In other words, foreign key is allowed to take only those values which have been already taken by
the primary key it refers to.
The relation which references the other relation is called as the referencing relation.
The relation which has been referenced is called as the referenced relation.
The referencing relation attribute which references the referenced relation’s primary key is called
as the referencing attribute.
The attribute of the referenced relation which has been referenced by the referencing relation is
called as the referenced attribute.
Example- Consider the following
two relations- ‘Student’ and
‘Department’.
Here, relation ‘Department’
references the relation ‘Student’.
Student Department
Here, the relation ‘Student’ does not satisfy the referential integrity constraint as in relation
‘Department’, the department no. 14 has not been defined as a primary key.
So, here the referential integrity constraint is violated.
Functional Dependency
Functional dependency is a relationship that exists when one attribute uniquely determines
another attribute.
If R is a relation with attributes X and Y, a functional dependency between the attributes is
represented as X->Y, which specifies Y is functionally dependent on X. Here X is a determinant set
and Y is a dependent attribute. Each value of X is associated with precisely one Y value.
For example: Suppose we have a student table with attributes: Stu_Id, Stu_Name, Stu_Age. Here
Stu_Id attribute uniquely identifies the Stu_Name attribute of student table because if we know
the student id we can tell the student name associated with it. This is known as functional
dependency and can be written as Stu_Id->Stu_Name or in words we can say Stu_Name is
functionally dependent on Stu_Id.
Types of Functional Dependencies
2. Non –Trivial Functional Dependency - It occurs when B is not a subset of A in: A ->B
Example - DeptId -> DeptName
The above is a non-trivial functional dependency since DeptName is a not a subset of DeptId.
3. Multivalued dependency - Multivalued dependency occurs when there are more than
one independent multivalued attributes in a table.
example: Consider a bike manufacture company, which produces two colors (Black and white) in
each model every year.
Here columns manuf_year and color are independent of each other and dependent on
bike_model. In this case these two columns are said to be multivalued dependent on
bike_model. These dependencies can be represented like this: bike_model ->> manuf_year
Note: A transitive dependency can only occur in a relation of three of more attributes.
NORMALIZATION
Normalization is the process of organizing data into a related table; it also eliminates redundancy
and increases the integrity which improves performance of the query. To normalize a database,
we divide the database into tables and establish relationships between the tables.
Normalization Avoids:
1. Duplication of Data - The same data is listed in multiple lines of the database
2. Insert Anomaly - A record about an entity cannot be inserted into the table without first
inserting information about another entity - Cannot enter a customer without a sales order
3. Delete Anomaly - A record cannot be deleted without deleting a record about a related entity.
Cannot delete a sales order without deleting all of the customer's information.
4. Update Anomaly - Cannot update information without changing information in many places. To
update customer information, it must be updated for each sales order the customer has placed
1. First Normal Form - First Normal Form is defined in the definition of relations (tables) itself. This
rule defines that all the attributes in a relation must have atomic domains. The values in an
atomic domain are indivisible units.
If a relation contain composite or multi-valued attribute, it violates first normal form. A relation is
in first normal form if every attribute in that relation is singled valued attribute.
Each attribute must contain only a single value from its pre-defined domain.
2. Second Normal Form - 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 proper subset of candidate key determines non-prime attribute, it is
called partial dependency.
3. Third Normal Form - For a relation to be in Third Normal Form, it must be in Second Normal form
and the following must satisfy −
4. Boyce-Codd Normal Form - BCNF is an extension of Third Normal Form on strict terms. BCNF
states that −
For any non-trivial functional dependency, X → A, X must be a super-key.
In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is the super-key
in the relation ZipCodes. So,
Stu_ID → Stu_Name, Zip And Zip → City
Which confirms that both the relations are in BCNF.
5. Fourth Normal Form (4th NF) - In 4th NF: An entity is in Fourth Normal Form (4NF) when it meets
the requirement of being in Third Normal Form (3NF) and additionally:
In 4NF states that no entity can have more than a single one-to-many relationship within an entity
if the one-to-many attributes are independent of each other.
In relational databases, many-to-many relationships are expressed through cross-reference
tables.
6. Fifth Normal Form (5th NF) - In 5th NF: A relation that has a join dependency cannot be
decomposed by a projection into other relations without spurious results. A relation is in 5NF
when its information content cannot be reconstructed from several smaller relations i.e. from
relations having fewer attributes than the original relation
RDBMS architecture
2. Process Manager – The process manager is responsible for providing a “thread of computation”
for each database request from a database client. It links the threads data and control output
to the appropriate communication manager client. The first decision to be made by the process
manager is to determine if enough system resources are available to execute the query or defer
the same until a later time.
3. Relational Query Processor – On receiving a request to process a query the Relation Query
Processor first checks if the user is authorised to run the query. It then compiles the query into
an interim query plan which is further optimised. The resulting plan is executed by the “plan
executor” which eventually makes use of the transaction and storage monitor.
4. Transaction and Storage Manager – Once a query is parsed it retrieves the requested data from
the Transaction and Storage Manager. It the gatekeeper to all data access and manipulation
calls. The transactional and storage manager also make sure that ACID properties of a
transaction are adhered too and thus the need for a lock and log manager.
5. Shared Components and Utilities – There are a number of shared components and utilities that
are essential for a database to run.
RDBMS KERNEL
A kernel is the core component of an operating system. Using interprocess communication and
system calls, it acts as a bridge between applications and the data processing performed at the
hardware level.
When an operating system is loaded into memory, the kernel loads first and remains in memory
until the operating system is shut down again. The kernel is responsible for low-level tasks such as
disk management, task management and memory management.
2. Data dictionary, which consists of the system-level data structures used by the kernel to
manage the database
Kernel - Core software, controls query processing, access paths to data, user access management,
storage management, indexing, transaction processing and read/update information
You might think of an RDBMS as an operating system (or set of subsystems), designed specifically
for controlling data access; its primary functions are storing, retrieving, and securing data. An
RDBMS maintains its own list of authorized users and their associated privileges; manages
memory caches and paging; controls locking for concurrent resource usage; dispatches and
schedules user requests; and manages space usage within its table-space structures
The System Global Area (SGA) is sometimes called the Shared Global Area. System Global Area
(SGA) is a key component of the relational database management system (RDMS). Developed by
Oracle Corporation, the SGA memory area is used by Oracle processes to hold shared database
instance information critical to proper database functioning, including required incoming data and
internal control data.
Oracle uses initialization parameters to control the amount of allocated SGA memory. In Oracle
Database 10g, the SGA is configured with the parameters "sga_target" and "sgs_max_size."
Oracle uses the automatic memory management feature to calculate and allocate memory to
different SGA areas. Initialization parameters may also be used to manually allocate memory to
individual SGA areas.
SGA components are as follows:
1. Dictionary cache: Holds data dictionary table information, such as information regarding
accounts, segments, data files, tables and privileges
2. Redo log buffer: Includes information about committed transactions that have not yet been
written to online redo log files
4. Shared pool: The shared pool caches information that is shared among users:
5. Java pool: The Java pool is an area of memory that is used for all session-specific Java code and
data within the Java Virtual Machine (JVM).
A process is a "thread of control" or a mechanism in an operating system that can run a series of
steps. Types of Processes - The processes in an Oracle system can be categorized into two major
groups:
1. Server Processes - Oracle creates server processes to handle the requests of user processes
connected to the instance.
1. Database Writer - Database writer process (DBWn) is a background process that writes buffers
in the database buffer cache to data files. Modified or new data is not necessarily written to a
datafile immediately. To reduce the amount of disk access and to increase performance, data is
pooled in memory and written to the appropriate data files all at once (in bulk mode), as
determined by the background process database writer process (DBWn).
The database writer (DBWn) process periodically writes cold, dirty buffers to disk. DBWn writes
buffers in the following circumstances:
A server process cannot find clean buffers for reading new blocks into the database buffer cache.
As buffers are dirtied, the number of free buffers decreases. If the number drops below an
internal threshold, and if clean buffers are required, then server processes signal DBWn to write.
The database uses the LRU(Least Recently Used) algorithm to determine which dirty buffers to
write. When dirty buffers reach the cold end of the LRU, the database moves them off the LRU to
a write queue. DBWn writes buffers in the queue to disk, using multiblock writes if possible. This
mechanism prevents the end of the LRU from becoming clogged with dirty buffers and allows
clean buffers to be found for reuse.
2. Log Writer (LGWR) - The Log Writer process is responsible for writing the contents of the redo log
buffer to the log file on disk, and for management of the log buffer. Redo log data is always
written first to a buffer in memory, then written to disk by the LGWR process when:
Writing commit records to the log file when a transaction is committed. Multiple commits (known
as group commits ) may be written in a single operation when database activity is high.
Recording a system change number (SCN) to each committed transaction, for use in recovery
operations when running Parallel Server.
3. Process Monitor - The process monitor (PMON) performs process recovery when a user process
fails. PMON is responsible for cleaning up the database buffer cache and freeing resources that
the user process was using. For example, it resets the status of the active transaction table,
releases locks, and removes the process ID from the list of active processes.
PMON periodically checks the status of dispatcher and server processes, and restarts any that
have stopped running (but not any that Oracle has terminated intentionally). PMON also registers
information about the instance and dispatcher processes with the network listener.
4. System Monitor Process (SMON) - The system monitor process (SMON) performs recovery, if
necessary, at instance startup. SMON is also responsible for cleaning up temporary segments
that are no longer in use and for coalescing contiguous free extents within dictionary managed
tablespaces. If any terminated transactions were skipped during instance recovery because of
file-read or offline errors, SMON recovers them when the tablespace or file is brought back
online. SMON checks regularly to see whether it is needed. Other processes can call SMON if they
detect a need for it.
5. Archiver Processes (ARCn) - The archiver process (ARCn) copies redo log files to a designated
storage device after a log switch has occurred. ARCn processes are present only when the
database is in ARCHIVELOG mode, and automatic archiving is enabled.
An Oracle instance can have up to 10 ARCn processes (ARC0 to ARC9). The LGWR process starts a
new ARCn process whenever the current number of ARCn processes is insufficient to handle the
workload. The alert log keeps a record of when LGWR starts a new ARCn process.
Oracle stores data logically in tablespaces and physically in datafiles associated with the
corresponding tablespace. The final components of the Oracle architecture are the physical
files where our information resided on disk. Oracle has several types for data files, each for
a different purpose:
1. Database datafiles - Database datafiles are physical files stored on disk. These files are used to
store data on disk. Database datafiles are only written to by the DBWR processes that we
introduced you to earlier (there is an exception or two to this statement, but for now, assume
that this point this true).
These database datafiles are associated with Oracle tablespaces, which are logical containers for
tables and indexes.
2. Control files - The Control File of the database is a binary file that contains a great deal of
database information. The control file contains the database name, data about the database log
files. Oracle cannot function without valid control files.
The control file of an Oracle database is created at the same time as the database. By default, at
least one copy of the control file is created during database creation. On some operating
systems the default is to create multiple copies. You should create two or more copies of the
control file during database creation. You might also need to create control files later, if you lose
control files or want to change particular settings in the control files.
3. Online redo logs concepts - Think of the online redo logs like a tape recorder that records every
change in the Oracle database. As changes occur, they are regularly recorded in the online redo
logs, just like you might record a movie on your VCR.
In the event that a disk crashes, you may have to replace the disk and restore the disk data from
a backup tape. If this backup tape was several days ago, you have lost a lot of data.
Fortunately, Oracle can ?replay? the saved transactions in the online redo logs, and re-apply lost
transactions back into the database.
At a minimum, Oracle requires that you have two online redo logs assigned to the database.
Oracle will write redo to the first log, and when the first log is full, Oracle will switch to the
second log and write the same redo. Each of these individual online redo logs is known as an
online redo log group.
Oracle Utilities
i. High-speed movement of data and metadata from one database to another using Data
Pump Export and Import
ii. Extract and manipulate complete representations of the metadata for database objects,
using the Metadata API
iii. Move all or part of the data and metadata for a site from one database to another, using
the Data Pump API
iv. Load data into Oracle tables from operating system files using SQL*Loader or from external
sources using external tables
v. Query redo log files through a SQL interface with LogMiner
vi. Perform physical data structure integrity checks on an offline (for example, backup)
database or datafile with DBVERIFY.
vii. Maintain the internal database identifier (DBID) and the database name (DBNAME) for an
operational database, using the DBNEWID utility
DBMS - Join
Joins can be simply defined as the combining or merging the related tuples from the two different
relations into a single type. It can be said that it is similar to cartesian product except the fact that
in cartesian product, we get all the possible combinations of relations while in join only those
combinations can be formed that satisfies some matching conditions. A cartesian product is
followed by a selection process results in joins. A Join operation pairs two tuples from different
relations, if and only if a given join condition is satisfied.
Consider the following two tables −
Table 1 − CUSTOMERS Table
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table 2 − ORDERS Table
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as shown below.
SQL> SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
Types of JOIN
1. INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the
condition satisfies. This keyword will create the result-set by combining all rows from both
the tables where the condition satisfies i.e value of the common field will be same.
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
i. Theta join - They have tuples from different relations if and only if they satisfy the theta
condition, here the comparison operators (≤, ≥, ˂, ˃, =, ̚ )come into picture.
ii. Equi join - the join uses only the equality operator then it is called as equi join.
iii. Natural join (⋈) - Natural join does not use any comparison operator. It does not
concatenate the way a Cartesian product does. We can perform a Natural Join only if there
is at least one common attribute that exists between two relations. In addition, the
attributes must have the same name and domain.
2. Outer Joins - An inner join includes only those tuples with matching attributes and the rest are
discarded in the resulting relation. Therefore, we need to use outer joins to include all the tuples
from the participating relations in the resulting relation. There are three kinds of outer joins
i. LEFT JOIN: This join returns all the rows of the table on the left side of
the join and matching rows for the table on the right side of join. The
rows for which there is no matching row on right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN
ii. RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the
rows of the table on the right side of the join and matching rows for the
table on the left side of join. The rows for which there is no matching row
on left side, the result-set will contain null. RIGHT JOIN is also known as
RIGHT OUTER JOIN.
iii. FULL JOIN: FULL JOIN creates the result-set by combining result of both
LEFT JOIN and RIGHT JOIN. The result-set will contain all the rows from
both the tables. The rows for which there is no matching, the result-set
will contain NULL values.
3.CARTESIAN JOIN: The CARTESIAN JOIN is also known as CROSS JOIN. In a CARTESIAN JOIN there
is a join for each row of one table to every row of another table. This usually happens when the
matching column or WHERE condition is not specified.
In the absence of a WHERE condition the CARTESIAN JOIN will behave like a CARTESIAN PRODUCT
. i.e., the number of rows in the result-set is the product of the number of rows of the two tables.
In the presence of WHERE condition this JOIN will function like a INNER JOIN.
Generally speaking, Cross join is similar to an inner join where the join-condition will always
evaluate to True
Syntax: SELECT table1.column1 , table1.column2, table2.column1...
FROM table1
CROSS JOIN table2;
4. SELF JOIN: As the name signifies, in SELF JOIN a table is joined to itself. That is, each row of the
table is joined with itself and all other rows depending on some conditions. In other words we can
say that it is a join between two copies of the same table. Syntax:
SELECT a.coulmn1 , b.column2
FROM table_name a, table_name b
WHERE some_condition;
SQL Functions
1. Aggregate Functions - Aggregate functions in DBMS take multiple rows from the table and
return a value according to the query. Aggregate functions perform a calculation on a set of
values and return a single value.
Aggregate functions ignore NULL values except COUNT.
It is used with the GROUP BY clause of the SELECT statement.
Syntax: SELECT <FUNCTION NAME> (<PARAMETER>) FROM <TABLE NAME>
Aggregate Description Syntax Example Output
Functions
2. String Functions - SQL string functions are used primarily for string manipulation.
ASCII(): This function is used to find the ASCII value of a character.
Syntax: SELECT ascii('t'); Output: 116
LEFT(): This function is used to SELECT a sub string from the left of given size or characters.
Syntax: SELECT LEFT('geeksforgeeks.org', 5); Output: geeks
LOCATE(): This function is used to find the nth position of the given word in a string.
Syntax: SELECT LOCATE('for', 'geeksforgeeks', 1); Output: 6
LOWER(): This function is used to convert the upper case string into lower case.
Syntax: SELECT LOWER('GEEKSFORGEEKS.ORG'); Output: geeksforgeeks.org
RIGHT(): This function is used to SELECT a sub string from the right end of the given size.
Syntax: SELECT RIGHT('geeksforgeeks.org', 4); Output: ‘.org’
SUBSTR(): This function is used to find a sub string from the a string from the given position.
Syntax:SUBSTR('geeksforgeeks', 1, 5); Output: ‘geeks’
TRIM(): This function is used to cut the given symbol from the string.
Syntax: TRIM(LEADING '0' FROM '000123'); Output: 123
MID(): This function is to find a word from the given position and of the given size.
Syntax: Mid ("geeksforgeeks", 6, 2); Output: for
3. Math functions - SQL numeric functions are used primarily for numeric manipulation and/or
mathematical calculations.
Functions Description
ABS() This SQL ABS() returns the absolute value of a number passed as an argument.
SELECT ABS(2); SELECT ABS(-2); Output-29
CEIL() This SQL CEIL() will rounded up any positive or negative decimal value within the
function upwards.
SELECT CEILING(3.46); Output-4
FLOOR() The SQL FLOOR() rounded up any positive or negative decimal value down to the next
least integer value.
SELECT FLOOR(7.55); output-7
EXP() The SQL EXP() returns e raised to the n-th power(n is the numeric expression), where
e is the base of natural algorithm and the value of e is approximately 2.71828183.
SELECT EXP(3);| output - 20.085537
MOD() This SQL MOD() function returns the remainder from a division.
SELECT MOD(29,3); output - 2
POW() This SQL POW() function returns the value of a number raised to another, where both
of the numbers are passed as arguments.
SELECT POWER(3,3); output-27
SQRT() The SQL SQRT() returns the square root of given value in the argument.
SELECT SQRT(49); output-7
LEAST() Returns the minimum-valued input when given two or more.
PI() Returns the value of pi
4. Date functions - Different database systems have different formats for date-type data, and
each RDBMS may employ different date functions, and there may also be differences in the
syntax for each RDBMS even when the function call is the same.
The GROUP BY clause is a SQL command that is used to group rows that have the same values.
The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical
data into groups. This GROUP BY clause follows the WHERE clause in a SELECT statement and
precedes the ORDER BY clause.
HAVING clause
We know that WHERE clause is used to place conditions on columns but what if we want to place
conditions on groups?
This is where HAVING clause comes into use. We can use HAVING clause to place conditions to
decide which group will be the part of final result-set. Also we can not use the aggregate functions
like SUM(), COUNT() etc. with WHERE clause. So we have to use HAVING clause if we want to use
any of these functions in the conditions.
Suppose we want to know all the release years for movie category id 8. We would use the
following script to achieve our results.
A field with a NULL value is a field with no value. It is very important to understand that a NULL
value is different than a zero value or a field that contains spaces.
IS NULL Syntax - IS NOT NULL Syntax -
A field with a NULL value is the one that has been left blank during the record creation.
The IS NULL/ IS NOT NULL Operator
The IS NULL operator is used to test for empty values (NULL values). The following SQL lists all
customers with a NULL value in the "Address" field:
Example
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
A subquery can have only one column in the SELECT clause, unless multiple columns are in
the main query for the subquery to compare its selected columns.
An ORDER BY command cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY command can be used to perform the same function as the
ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple value operators
such as the IN operator.
The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY,
CLOB, or NCLOB.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN operator
can be used within the subquery.
Syntax:SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
SQL – Views
A view is nothing more than a SQL statement that is stored in the database with an associated
name. A view is actually a composition of a table in the form of a predefined SQL query. A view
can contain all rows of a table or select rows from a table. A view can be created from one or
many tables which depends on the written SQL query to create a view. Views, which are a type of
virtual tables allow users to do the following −
Structure data in a way that users or classes of users find natural or intuitive.
Restrict access to the data in such a way that a user can see and (sometimes) modify
exactly what they need and no more.
Summarize data from various tables which can be used to generate reports.
Creating Views
Database views are created using the CREATE VIEW statement. Views can be created from a single
table, multiple tables or another view.
Syntax: CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];
Example - CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers WHERE Country = "Brazil";
We can query the view above as follows: SELECT * FROM [Brazil Customers];
If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
The following code block has an example of creating same view CUSTOMERS_VIEW with the WITH
CHECK OPTION.
CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS
WHERE age IS NOT NULL
WITH CHECK OPTION;
The WITH CHECK OPTION in this case should deny the entry of any NULL values in the view's AGE
column, because the view is defined by data that does not have a NULL value in the AGE column.
Updating a View - A view can be updated under certain conditions which are given below −
The SELECT clause may not contain the keyword DISTINCT.
All NOT NULL columns from the base table must be included in the view in order for the
INSERT query to function.
So, if a view satisfies all the above-mentioned rules then you can update that view. The following
code block has an example to update the age of Ramesh.
SQL > UPDATE CUSTOMERS_VIEW
SET AGE = 35 WHERE name = 'Ramesh';
This would ultimately update the base table CUSTOMERS and the same would reflect in the view
itself.
Inserting Rows into a View
Rows of data can be inserted into a view. The same rules that apply to the UPDATE command also
apply to the INSERT command.
Deleting Rows into a View
Rows of data can be deleted from a view. The same rules that apply to the UPDATE and INSERT
commands apply to the DELETE command.
Dropping Views
Obviously, where you have a view, you need a way to drop the view if it is no longer needed.
PL/SQL
PL/SQL is a block structured language that can have multiple blocks in it. The programs of PL/SQL
are logical blocks that can contain any number of nested sub-blocks. Pl/SQL stands for
"Procedural Language extension of SQL" that is used in Oracle.
PL/SQL includes procedural language elements like conditions and loops. It allows declaration of
constants and variables, procedures and functions, types and variable of those types and triggers.
It can support Array and handle exceptions (runtime errors).
You can create PL/SQL units like procedures, functions, packages, types and triggers, etc. which
are stored in the database for reuse by applications.With PL/SQL, you can use SQL statements to
manipulate Oracle data and flow of control statements to process the data.
The PL/SQL is known for its combination of data manipulating power of SQL with data processing
power of procedural languages. It inherits the robustness, security, and portability of the Oracle
Database.
PL/SQL is not case sensitive so you are free to use lower case letters or upper case letters except
within string and character literals. A line of PL/SQL text contains groups of characters known as
lexical units. It can be classified as follows:
The Basic Syntax of PL/SQL which is a block-structured language; this means that the PL/SQL
programs are divided and written in logical blocks of code. Each block consists of three sub-parts –
1. Declaration - This section starts with the keyword DECLARE. It is an optional section and defines
all variables, cursors, subprograms, and other elements to be used in the program.
2. Executable Commands - This section is enclosed between the keywords BEGIN and END and it is a
mandatory section. It consists of the executable PL/SQL statements of the program. It should
have at least one executable line of code, which may be just a NULL command to indicate that
nothing should be executed.
3. Exception Handling - This section starts with the keyword EXCEPTION. This optional section
contains exception(s) that handle errors in the program.
Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other
PL/SQL blocks using BEGIN and END. Following is the basic structure of a PL/SQL block −
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling>
END;
Example
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line(message);
END;
/
To run the code from the SQL command line, you may need to type / at the beginning of the first
blank line after the last line of the code.
1. Scalar data types - Single values with no internal components. It is like a linear data type. Scales
data type divides into four different types character, numeric, boolean or date/time type.
2. Composite data types - Composite data types have internal components to manipulate data
easily. Data items that have internal components that can be accessed individually. For example,
collections and records.
3. Reference data types - This data types works like a pointer to hold some value. Pointers to other
data items.
4. LOB data types -Pointers to large objects that are stored separately from other data items, such
as text, graphic images, video clips, and sound waveforms. Maximum size up to 4 Gigabytes.
BLOB Used to store large binary objects in the database. 8 to 128 terabytes (TB)
5. Unknown Column types - Identify columns when not know type of column.
6. User Define data types - Define your own data type that are inherit from predefined base data
type. PL/SQL gives you the control to create your own sub data type that are inherit from
predefined base type. Sub types can increase reliability and provide compatibility with ANSI/ISO
type. Several predefined subtypes are in STANDARD package.
7. NULLs in PL/SQL - PL/SQL NULL values represent missing or unknown data and they are not an
integer, a character, or any other specific data type. Note that NULL is not the same as an empty
data string or the null character value '\0'. A null can be assigned but it cannot be equated with
anything, including itself.
PL/SQL - Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulation. PL/SQL language is rich in built-in operators and provides the following types of
operators −
IF - THEN statement
IF(condition)
1 THEN
Statement;
END IF;
IF-THEN-ELSE statement
IF(condition)
THEN
2 [Statements to execute when condition is TRUE]
ELSE
[Statements to execute when condition is FALSE]
END IF;
IF-THEN-ELSIF statement
It allows you to choose between several alternatives.
IF(Condition1)
THEN
3 Statements to execute when condition1 is TRUE
ELSIF(condition2)
THEN
Statements to execute when condition2 is TRUE
END IF;
Case statement
The CASE statement selects one sequence of statements to execute.
CASE[expression]
WHEN condition1 THEN result1
4 WHEN condition2 THEN result2
…....................
WHEN condition_n THEN result_n
ELSE result
END;
nested IF-THEN-ELSE
5 You can use one IF-THEN or IF-THEN-ELSIF statement inside another IF-THEN or IF-THEN-
ELSIF statement(s).
S.No PL/SQL-Loop
PL/SQL Basic LOOP - Loops are iterative control statements. They are used to
repeat execution of one or more statements for defined number of times.
1 LOOP
Sequence of statements;
END LOOP;
PL/SQL FOR LOOP - It is used to execute a set of statements for a fixed number of
times. It is iterated between the start and end integer values.
3 FOR counter IN initial_value .. final_value LOOP
LOOP statements;
END LOOP;
EXIT statement - The Exit statement completes the loop and control passes to the
statement immediately after the END LOOP.
LOOP
1 Statements;
EXIT;
[or EXIT WHEN condition;]
END LOOP;
CONTINUE statement - Causes the loop to skip the remainder of its body and
2
immediately retest its condition prior to reiterating.
GOTO statement - Transfers control to the labeled statement. Though it is not advised
to use the GOTO statement in your program.
I) GOTO label_name;
PL/SQL – Transactions
Transactions are a means to break programming code into manageable units. Grouping
transactions into smaller elements is a standard practice that ensures an application will save
only correct data. Initially, any application will have to connect to the database to access the
data. A database transaction is an atomic unit of work that may consist of one or more related
SQL statements. It is called atomic because the database modifications brought about by the SQL
statements that constitute a transaction can collectively be either committed, i.e., made
permanent to the database or rolled back (undone) from the database.
Even if an SQL statement is executed successfully, unless the transaction containing the statement
is committed, it can be rolled back and all changes made by the statement(s) can be undone.
You can use the COMMIT, ROLLBACK, SAVEPOINT, and SET TRANSACTION command
to control the transaction.
1. COMMIT: COMMIT command to make changes permanent save to a database during the
current transaction. In case you need to save the current PL/SQL transaction into the database,
it is mandatory to issue a commit command after completing the transaction. The other users
of the same database would then be able to see the newly saved transaction. A commit
command is used to permanently save the transactions in a database. Example –
Commit;
3. ROLLBACK: ROLLBACK command execute at the end of current transaction and undo/undone
any changes made since the begin transaction. In case you have made a transaction and you
want to delete it or go to the previously saved state , a rollback command needs to be
executed. However, this will only work when you haven’t issued the commit command
otherwise giving rollback command wouldn’t be of any use. This command is basically used to
undo any changes made to the database. Example - Rollback;
4. SAVEPOINT: SAVEPOINT command save the current point with the unique name in the
processing of a transaction. Savepoint are used to mark the current position during processing a
transaction. It helps to split a lengthy transaction into smaller sub-units by issuing some
checkpoints at various locations within the program. In case you need to rollback a part of a
transaction you can refer to the checkpoints and rollback accordingly. Hence, savepoints lets
you rollback a particular part of a transaction than the whole transaction. Example - Savepoint
pointA;
5. SET TRANSACTION: PL/SQL SET TRANSACTION command set the transaction properties such as
read-write/read only access. A set transaction command sets the PL/SQL transaction properties
to read or write or a combination of both read or write. This command basically is used to
manipulate the accessing capability of a transaction. Example - Set Transaction Read Name
'TransactionA';
1. Atomicity - Atomicity means all or nothing. Transactions often contain multiple separate
actions. For example, a transaction may insert data into one table, delete from another table,
and update a third table. Atomicity ensures that either all of these actions occur or none at all.
2. Consistency - Consistency means that transactions always take the database from one
consistent state to another. So, if a transaction violates the databases consistency rules, then
the entire transaction will be rolled back.
3. Isolation - Isolation means that concurrent transactions, and the changes made within them,
are not visible to each other until they complete. This avoids many problems, including those
that could lead to violation of other properties. The implementation of isolation is quite
different in different DBMS’. This is also the property most often related to locking problems.
4. Durability - Durability means that committed transactions will not be lost, even in the event of
abnormal termination. That is, once a user or program has been notified that a transaction
was committed, they can be certain that the data will not be lost.
Database Locking
In Oracle PL/SQL, a LOCK is a mechanism that prevents destructive interaction between two
simultaneous transactions or sessions trying to access the same database object. A LOCK can
be achieved in two ways: Implicit locking or Explicit Locking. The session remains in a
waiting state until one of the sessions is either committed or rolled back.
Oracle server implicitly creates a deadlock situation if a transaction is done on the same
table in different sessions. This default locking mechanism is
called implicit or automatic locking.
A lock is held until the transaction is complete; this is referred to as data concurrency.
A key reason for locking is to ensure that all valid processes are always able to access the
original data as it was at the time the query was initiated. This is referred to as read
consistency.
With Explicit Locking, a table or partition can be locked using the LOCK TABLE statement in one of
the specified modes. The available lock modes are ROW EXCLUSIVE, SHARE
UPDATE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, NOWAIT and WAIT. Note that it is
preferable to do Explicit Locking rather than relying on the implicit locking done by default by the
Oracle server.
Types of Locks
1. Shared lock: Shared locks are placed on resources whenever a read operation (select) is
performed.
Multiple shared locks can be simultaneously set on a resource.
2. Exclusive lock: Exclusive locks are placed on resources whenever a write operation (INSERT,
UPDATE And DELETE) are performed. Only one exclusive lock can be placed on a resource at a
time.
Levels of Locks:
1. Table-level locking – In it the entire table is locked against any kind of update or insert actions
from another process.Once a given process has locked a table, that process is the only one that
can change rows in the table.
2. Row-level locking – In it any specified row or rows in a table can be locked (any unlocked rows
are still available for updates or deletes). The locked rows can be updated only by the process
that initiated the locking.
3. Page Level locking - If the WHERE clause evaluates to a set of data, a page level lock is used.
Example Syntax:
Deadlock: In a deadlock, two database operations wait for each other to release a lock.
A deadlock occurs when two users have a lock, each on a separate object, and, they want to
acquire a lock on each other's object.
When this happens, the first user has to wait for the second user to release the lock, but the
second user will not release it until the lock on the first user's object is freed. At this point,
both the users are at an impasse and cannot proceed with their business.
In such a case, Oracle detects the deadlock automatically and solves the problem by aborting
one of the two transactions.
PL/SQL Cursor
A cursor is a temporary work area created in the system memory when a SQL statement is
executed. A cursor contains information on a select statement and the rows of data accessed by
it. This temporary work area is used to store the data retrieved from the database, and
manipulate this data. A cursor can hold more than one row, but can process only one row at a
time. The set of rows the cursor holds is called the active set.
You can name a cursor so that it could be referred to in a program to fetch and process the rows
returned by the SQL statement, one at a time. There are two types of cursors −
1. Implicit Cursors - Implicit cursors are automatically created by Oracle whenever an SQL
statement is executed. Programmers cannot control the implicit cursors and the
information in it.
Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is
associated with this statement. For INSERT operations, the cursor holds the data that needs to be
inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would be
affected.
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always has
attributes such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.
S.No Attribute & Description
%ISOPEN - Always returns FALSE for implicit cursors, because Oracle closes the SQL
3
cursor automatically after executing its associated SQL statement.
An exception occurs when the PL/SQL engine encounters an instruction which it cannot execute
due to an error that occurs at run-time. These errors will not be captured at the time of
compilation and hence these needed to handle only at the run-time.
For example, if PL/SQL engine receives an instruction to divide any number by '0', then the PL/SQL
engine will throw it as an exception. The exception is only raised at the run-time by the PL/SQL
engine.
Exceptions will stop the program from executing further, so to avoid such condition, they need to
be captured and handled separately. This process is called as Exception-Handling, in which the
programmer handles the exception that can occur at the run time. SYNTAX –
BEGIN
<execution block>
EXCEPTION
WHEN <exceptionl_name>
THEN
<Exception handling code for the “exception 1 _name’' >
WHEN OTHERS
THEN
<Default exception handling code for all exceptions >
END;
Types of Exception
1. Predefined Exceptions - Oracle has predefined some common exception. These exceptions have
a unique exception name and error number. These exceptions are already defined in the
'STANDARD' package in Oracle. In code, we can directly use these predefined exception names
to handle them. Below are the few predefined exceptions
ROW_MISMATCH When cursor variable data type is incompatible with the actual cursor
return type
TOO_MANY_ROWS When a 'SELECT' statement with INTO clause returns more than one
row
PL/SQL - Procedures
A Procedure is a subprogram unit that consists of a group of PL/SQL statements. Each procedure
in Oracle has its own unique name by which it can be referred. A subprogram is a program
unit/module that performs a particular task. These subprograms are combined to form larger
programs. This is basically called the 'Modular design'. A subprogram can be invoked by another
subprogram or program which is called the calling program.
A subprogram can be created −
Declarative Part
It is an optional part. However, the declarative part for a subprogram does not start
1 with the DECLARE keyword. It contains declarations of types, cursors, constants,
variables, exceptions, and nested subprograms. These items are local to the
subprogram and cease to exist when the subprogram completes execution.
Executable Part
2
This is a mandatory part and contains statements that perform the designated action.
Exception-handling
3
This is again an optional part. It contains the code that handles run-time errors.
OUT - An OUT parameter returns a value to the calling program. Inside the subprogram,
2 an OUT parameter acts like a variable. You can change its value and reference the value
after assigning it. The actual parameter must be variable and it is passed by value.
PL/SQL – Functions
A function is same as a procedure except that it returns a value. Functions are a standalone block
that is mainly used for calculation purpose. Function use RETURN keyword to return the value,
and the datatype of this is defined at the time of creation. The values can be passed into the
function or fetched from the procedure through the parameters. These parameters should be
included in the calling statement. Function can also return the value through OUT parameters
other than using RETURN.
Procedure Function
Cannot call in SELECT statement A Function that contains no DML statements can be
called in SELECT statement
Use OUT parameter to return the value Use RETURN to return the value
RETURN will simply exit the control RETURN will exit the control from subprogram and
from subprogram. also returns the value
Return datatype will not be specified at Return datatype is mandatory at the time of
the time of creation creation
A PL/SQL program that is stored in a database in compiled form and can be called by name
is referred to as a stored procedure. A stored procedure can be shared by a number of
programs. Stored procedures are helpful in controlling access to data, preserving data
integrity ad improving productivity. The benefits of using stored procedures in SQL Server
rather than application code stored locally on client computers include:
Types of Triggers
1. Classification based on the timing
BEFORE Trigger: It fires before the specified event has occurred.
AFTER Trigger: It fires after the specified event has occurred.
INSTEAD OF Trigger: "INSTEAD OF trigger" is the special type of trigger. It is used only in
DML triggers. It is used when any DML event is going to occur on the complex view.
Classification based on the level
2.
STATEMENT level Trigger: It fires one time for the specified event statement.
ROW level Trigger: It fires for each record that got affected in the specified event. (only for DML)
Benefits of Triggers
Trigger generates some derived column values automatically
Enforces referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions
Here,
i. CREATE [OR REPLACE] TRIGGER trigger_name: It creates or replaces an existing trigger with
the trigger_name.
ii. {BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would be executed. The
INSTEAD OF clause is used for creating trigger on a view.
iii. {INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.
iv. [OF col_name]: This specifies the column name that would be updated.
v. [ON table_name]: This specifies the name of the table associated with the trigger.
vi. [REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values for various
DML statements, like INSERT, UPDATE, and DELETE.
vii. [FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be executed for
each row being affected. Otherwise the trigger will execute just once when the SQL
statement is executed, which is called a table level trigger.
viii. WHEN (condition): This provides a condition for rows for which the trigger would fire. This
clause is valid only for row level triggers.
PL/SQL - Packages
Package is a group of related functions, procedures, types, cursors, etc. PL/SQL package is like a
library once written stored in the Oracle database and can be used by many applications. A
PL/SQL package has two parts: