Unit 2 - Notes DBMS
Unit 2 - Notes DBMS
DCL (Data Control Language) - Used to retrieve the saved data from database.
Grant It is used for give user access to the database
Revoke It is used for to take back the access or permissions from the user
DML (Data Manipulation Language) - used to Manipulate the data in the database
Select It is used for select data from the Table based on the requirements
Insert It is sued for Inserting data into existing table
Update It is used for update data in the Table based on the requirement
Delete It is used for delete data from the Table
Merge It is used for merge operations
Call It is used for call a structured query language or Java sub program
Lock Table It have ability to control the concurrency
TCL (Transaction Control Language) used to run the changes made by the DML commands
Commit It is used for save the transaction on the Database.
Rollback It is used for restore the database to original state from last commit.
Savepoint It is used for go back to the saved state.
Oracle Date Functions
Function Example Result Description
ADD_MONTHS( DATE Add a number of
ADD_MONTHS 31-MAR-16
'2016-02-29', 1 ) months (n)
SELECT CURRENT_DATE 06-AUG-2017 Return the current date
CURRENT_DATE
FROM dual 19:43:44 and time
SELECT 06-AUG-17 Return the current date
CURRENT_TIMESTAMP CURRENT_TIMESTAMP 08.26.52.742000000 and time with time zone
FROM dual PM -07:00
Extract YEAR,
EXTRACT(YEAR FROM
EXTRACT 2017 MONTH, DAY, …
SYSDATE)
from a date time
LAST_DAY(DATE '2016- Gets the last day of the
LAST_DAY 29-FEB-16
02-01') month
SELECT 06-AUG-17 Return
LOCALTIMESTAMP LOCALTIMESTAMP 08.26.52.742000000 a TIMESTAMP value
FROM dual PM
MONTHS_BETWEEN( Return the number of
MONTHS_BETWEEN DATE '2017-07-01', DATE 6 months between two
'2017-01-01' ) dates.
NEXT_DAY( DATE '2000- Get the first weekday
NEXT_DAY 02-JAN-00
01-01', 'SUNDAY' )
Return the current
SYSDATE SYSDATE 01-AUG-17
system date and time
01-AUG-17 Return the system date
SELECT SYSTIMESTAMP
SYSTIMESTAMP 01.33.57.929000000 and time
FROM dual;
PM -07:00
TO_CHAR( DATE'2017-01- Sunday, January 01, Convert a DATE to a
TO_CHAR
01', 'DL' ) 2017 character string
TO_DATE( '01 Jan 2017', Convert character string
TO_DATE 01-JAN-17
'DD MON YYYY' ) to a DATE value.
Oracle String Functions
Function Example Result Purpose
Returns an ASCII code value of a
ASCII ASCII(‘A’) 65
character.
Converts a numeric value to its
CHR CHR(’65’) ‘A’
corresponding ASCII character.
Concatenate two strings and return the
CONCAT CONCAT(‘A’,’BC’) ‘ABC’
combined string.
Converts the first character in each
‘Hi
INITCAP INITCAP(‘hi there’) word in a specified string to uppercase
There’
and the rest to lowercase.
Search for a substring and return the
INSTR INSTR( ‘This is a playlist’, ‘is’) 3 location of the substring in a string
Return the number of characters (or
LENGTH LENGTH(‘ABC’) 3
length) of a specified string
Return a string with all characters
LOWER LOWER(‘Abc’) ‘abc’
converted to lowercase.
Return a string that is left-padded with
LPAD LPAD(‘ABC’,5,’*’) ‘**ABC’ the specified characters to a certain
length.
Remove spaces or other specified
LTRIM LTRIM(‘ ABC ‘) ‘ABC ‘ characters in a set from the left end of a
string.
‘BLACK Replace all occurrences of a substring
REPLACE(‘JACK AND
REPLACE AND by another substring in a string.
JOND’,’J’,’BL’);
BLOND’
Return a string that is right-padded
RPAD RPAD(‘ABC’,5,’*’) ‘ABC**’ with the specified characters to a
certain length.
Remove all spaces or specified
RTRIM RTRIM(‘ ABC ‘) ‘ ABC’ character in a set from the right end of
a string.
SUBSTR SUBSTR(‘Oracle Substring’, 1, 6 ) ‘Oracle’ Extract a substring from a string.
Replace all occurrences of characters
TRANSLATE TRANSLATE(‘12345’, ‘143’, ‘bx’) ‘b2x5’
by other characters in a string.
Remove the space character or other
TRIM TRIM(‘ ABC ‘) ‘ABC’ specified characters either from the
start or end of a string.
Convert all characters in a specified
UPPER UPPER(‘Abc’) ‘ABC’
string to uppercase.
Queries Involving More than One Relation
Multiple relation queries involve the execution of queries that involve multiple tables in a database.
JOIN operations are used to combine related tables into a single result set.
Natural Join
It is a type of inner type that joins two or more tables based on the same column name and has the same
data type present on both tables.
Syntax:
Select * from tablename1 Natural JOIN tablename_2;
CROSS JOIN
It is also known as CARTESIAN JOIN, which returns the Cartesian product of two or more joined tables.
The CROSS JOIN produces a table that merges each row from the first table with each second table row.
It is not required to include any condition in CROSS JOIN.
Syntax:
Select * from table_1 cross join table_2;
SELF JOIN
It is a SELF JOIN used to create a table by joining itself as there were two tables. It makes temporary
naming of at least one table in an SQL statement.
Syntax:
Select column1, column2, column(s) FROM table_1 Tbl1, table_2 Tbl2 WHERE condition;
Tbl1 and Tbl2 are two different table aliases for the same table.
Aggregation Operators in DBMS
In a DBMS, aggregation operators are used to perform operations on a group of values to return a single
summarizing value.
The most common aggregation operators include COUNT, SUM, AVG, MIN, and MAX.
The HAVING clause filters groups based on conditions after GROUP BY forms them. It functions like
WHERE but for grouped data.
The GROUP BY statement is used with SELECT. WHERE goes before GROUP BY, and ORDER BY
goes after.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes
o Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a
valid set of values for an attribute.
o The data type of domain includes string, character,
integer, time, date, currency, etc. The value of the
attribute must be available in the corresponding domain.
4. Key constraints
o Keys are the entity set that is used to identify an
entity within its entity set uniquely.
o An entity set can have multiple keys, but out of
which one key will be the primary key. A primary
key can contain a unique and null value in the
relational table.
Triggers
WHAT IS TRIGGER
A trigger is a special kind of stored procedure that is activated ("triggered") in response to a particular event in a
database.
Trigger is called automatically when a data modification event occurs against a table.
3 TYPES OF TRIGGERS
1) DML triggers are automatically fired when an INSERT, UPDATE or DELETE event occurs on a table.
2) DDL triggers are automatically invoked when a CREATE, ALTER, or DROP event occurs in a database.
3) Logon triggers is invoked when a LOGON event is raised when a user session is established.
Syntax
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
ON table_name
[FOR EACH ROW]
WHEN (condition)
[trigger_body]
{BEFORE / AFTER / INSTEAD OF}: Choose when the trigger acts (before, after, or instead of a DML event).
{INSERT [OR] / UPDATE [OR] / DELETE}: Specify the DML operation (insert, update, delete) that activates the trigger.
Advantages of Triggers
Triggers in SQL can replace schedulers by immediately checking data changes, eliminating the
need for background scheduler applications.
They enhance security by enforcing additional checks on sensitive data during insertions,
updates, or deletions, ensuring system integrity.
Triggers prevent the insertion of invalid data into columns by validating input or performing
corrective actions, such as logging errors or data manipulation.
Disadvantages of Triggers
Triggers may be difficult to troubleshoot as they execute automatically in the database. If there is
some error then it is hard to find the logic of trigger because they are fired before or after
updates/inserts happen.
The triggers may increase the overhead of the database as they are executed every time any field
is updated.
What is JDBC?
JDBC stands for Java DataBase Connectivity, which is a standard Java API for database-
independent connectivity between the Java programming language and a wide range of databases.