Advanced Database Management System
Advanced Database Management System
A DBMS also has to provide some SQL queries make it simple to retrieve
uniform methods independent of a data.At the same time, many data
specific application for accessing the items can be accessed
information that is stored.
There is no correlation between the Data is kept in the form of tables that
data are linked together via foreign keys
DBMS does not impose any RDBMS defines the integrity constraint
constraints or security with regard to for the purpose of holding ACID
data manipulation. It is the user or property.
programmer’s responsibility to ensure
ACID properties.
Create table_course
(Course_id Varchar2 (20),
Department_id Varchar2 (20),
Title Varchar2 (20),
Constraint PK_course
Primary key (Course_id, Department_id),
Constraint FK_course
Foreign key (Department_id) references Department(Department_id));
11.CHECK Constraint
○ Using CHECK constraint SQL can specify the data validation for
columns during table creation.
○ CHECK clause is a Boolean condition that is either TRUE or FALSE.
○ If the condition evaluates to TRUE, the column value is accepted by
the database, if the condition evaluates to FALSE, the database will
return an error code.
○ The check constraint is declared in CREATE TABLE statement using
the syntax :
Column_name datatype [constraint constraint_name] [CHECK (Condition)]
○ Ex.
Create table_worker
(NameVarchar2 (25) NOT NULL,
Age Number,
Constraint CK_worker
CHECK (Age Between 18 AND 65) );
15.INSERT command
○ The syntax of insert statement is :
INSERT INTO table_name
[(column_name [ , column_name] ...... [ , column_name])]
VALUES
(column_value [ , column_value] ...... [ , column_value]);
○ The number of columns in the list of column_names must match the
number of literal values or expressions that appear in parenthesis after
the keyword values.
○ If the column names specified in the Insert statement are more than
values, then it returns an error.
○ Column and value data type must match.
○ Ex.
SQL> Insert into Employee
(E_name, B_Date, Salary, Address)
Values
('Sachin', '21-MAR-73', 50000.00, 'Mumbai');
16.DELETE Command
○ The syntax of delete statement is :
DELETE FROM table_name
[WHERE condition]
○ DELETE Command without WHERE clause will empty the table
completely.
○ Ex.
SQL>Delete from Student
Where Student_id = 'SE 201';
1 row deleted.
17.UPDATE Command
○ If you want to modify existing data in the database, UPDATE command
can be used to do that. With this statement you can update zero or
more rows in a table.
○ The syntax of UPDATE command is :
UPDATE table_name
SET column_name : : expression
[, column_name : : expression]
[, column_name : : expression]
[where condition]
○ The UPDATE statement references a single table and assigns an
expression to at least one column.
○ The WHERE clause is optional; if an UPDATE statement does not
contain a WHERE clause, the assignment of a value to a column will be
applied to all rows in the table.
25. IN Operator
○ The IN operator allows you to easily test if an expression matches any
value in a list of values.
○ It is used to help reduce the need for multiple OR conditions in a
SELECT, INSERT, UPDATE, or DELETE statement.
○ Syntax: expression IN (value1, value2, .... value_n);
○ Ex.
SELECT *
FROM customers
WHERE customer_id IN (5000, 7000, 8000, 9000);
○ This example would return all records from the customers table where
the customer_id is either 5000, 7000, 8000 or 9000.
31. OR Operator
○ The OR operator is a logical operator in SQL that combines two or
more expressions to return true if any of the conditions are true.
○ Syntax:
SELECT * FROM table_name WHERE condition1 OR condition2 OR...
conditionN;
○ Ex.
SELECT * FROM employees
WHERE last_name = 'Smith' OR first_name = 'John';
○ This will select all employees whose last_name is Smith OR first_name
is John.
○ The Oracle INNER JOIN would return the records where table1 and
table2 intersect.
○ The LEFT OUTER JOIN would return all records from table1 and only
those records from table2 that intersect with table1.
○ The Oracle RIGHT OUTER JOIN would return all records from table2
and only those records from table1 that intersect with table2.
○ The Oracle FULL OUTER JOIN would return all the records from both
table1 and table2.
56. Define the different Keywords & parameters used when Creating a
Sequence.
○ INCREMENT BY : Specifies the interval between sequence numbers. It
can be any positive or negative value but not zero.If this clause is
omitted ,the default value is 1 .
○ MINVALUE : Specifies the sequence minimum value.
○ NOMINVALUE : Specifies a minimum value of 1 for an ascending
sequence and – (10)^26 for a descending sequence.
○ MAXVALUE: Specifies the maximum value that a sequence can
generate.
○ NOMAXVALUE : Specifies a maximum of 10^27 for an ascending
sequence or -1 for a descending sequence. This is the default clause.
○ START WITH : Specifies the first sequence number to be generated.
The default for an ascending sequence is the sequence minimum
value(1) and for a descending sequence, it is the maximum value(-1)
○ CYCLE : Specifies that the sequence continues to generate repeat
values after reaching either its maximum value.
○ NOCYCLE: Specifies that a sequence cannot generate more values
after reaching the maximum value.
○ CACHE : :Specifies how many values a sequence oracle pre-allocates
and keeps in memory for faster access.The minimum value for this
parameter is two.
○ NOCACHE : Specifies that values of a sequence are not pre-allocated.
58. Procedures
○ Procedures are simply a PL/SQL block that executes certain tasks.
○ A procedure is completely portable among platforms in which Oracle
is executed.
○ A procedure is created using the CREATE PROCEDURE command.
Syntax:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]
BEGIN
executable_section
[EXCEPTION
exception_section]
END [procedure_name];
○ To execute the stored procedure simply call it by name in EXECUTE
command as
SQL> execute myproc1(7768);
This will execute myproc1 with the value 7768.
○ To delete a procedure DROP PROCEDURE command is used.
Syntax :
DROP PROCEDURE procedure_name;
PL/SQL Engine
○ The PL/SQL compilation and run-time system is a technology, not an
independent product. Think of this technology as an engine that
compiles and executes PL/SQL blocks and subprograms.
○ The engine can be installed in an Oracle server or in an application
development tool such as Oracle Forms or Oracle Reports.
○ So, PL/SQL can reside in two environments :
■ The Oracle server
■ Oracle tools.
○ These two environments are independent.
○ PL/SQL is bundled with the Oracle server but might be unavailable in
some tools.
○ In either environment, the PL/SQL engine accepts as input any valid
PL/SQL block or subprogram.
○ The figure shows the PL/SQL engine processing an anonymous block.
○ The engine executes procedural statements but sends SQL
statements to the SQL Statement Executor in the Oracle server.
Output:
7
5
x is largest than y
○ For...loop
■ Syntax:
FOR <var> IN [reverse] <start>. .<finish> LOOP
<commands> /* A list of statements. */
END LOOP;
■ Here, <var> can be any variable; it is local to the for-loop and
need not be declared.
■ Also, <start> and <finish> are constants.
■ The value of a variable <var> is automatically incremented by 1.
■ The commands inside the loops are automatically executed until
the final value of variable is reached.
■ Reverse is optional part, when you want to go from maximum
value to minimum value in that case reverse is used.
■ Ex.
BEGIN
For i in 1. .3 LOOP
dbms_output.put_line(i);
END LOOP;
END;
Output:
1
2
3
○ While loop:
■ Syntax:
WHILE <condition> LOOP
<commands> /* A list of statements. */
END LOOP;
■ This loop executes the commands if the condition is true.
■ Ex.
DECLARE
i NUMBER := 0;
BEGIN
While i<=3 LOOP
i := i+1;
dbms_output.put_line(i);
END LOOP;
END;
Output:
1
2
3
65. The GOTO statement
○ The GOTO statement changes the flow of control within a PL/SQL
block.
○ This statement allows execution of a section of code, which is not in
the normal flow of control.
○ The entry point into such a block of code is marked using the tags
<<userdefined name>>.
○ The GOTO statement can then make use of this user-defined name to
jump into that block for execution.
○ Syntax:
GOTO <codeblock name> ;
66. Cursors
○ Whenever a SQL statement is issued the Database server opens an
area of memory called Private SQL area in which the command is
processed and executed. An identifier for this area is called a cursor.
○ There are two types of cursors.
■ Implicit cursors - When the executable part of a PL/SQL block
issues a SQL command, PL/SQL creates an implicit cursor which
has the identifier SQL. PL/SQL internally manages this cursor.
■ Explicit cursors - If SELECT statements in PL/SQL block return
multiple rows then you have to explicitly create a cursor which is
called an explicit cursor. The set of rows returned by an explicit
cursor is called a result set. The row that is being processed is
called the current row.
○ Oracle uses four commands to handle Cursors. They are :
■ DECLARE - Defines the name and structure of the cursor
together with the SELECT statement.Cursors are defined within a
DECLARE section of a PL/SQL block with DECLARE command.
Syntax to declare a Cursor:
Cursor cursor_name [(parameters)] [RETURN return_type]
IS SELECT query.
Parameter and return are optional parts. When parameters are
passed to the cursor it is called a parameterized cursor.
■ OPEN - Cursors are opened with the OPEN statement, this
populates the cursor with data.
Syntax : OPEN Cursor_name[parameters];
■ FETCH - To access the rows of data within the cursor the FETCH
statement is used.
Syntax: FETCH Cursor_name[parameters];
■ CLOSE - The CLOSE statement releases the cursor and any rows
within it, you can open the cursor again to refresh the data in it.
Syntax: CLOSE cursor_name;
○ Ex.,
CURSOR c1
IS
SELECT course_number
FROM courses_tbl
WHERE course_name = name_in;
The result set of this cursor is all course_numbers whose
course_name matches the variable called name_in.
71.Triggers
○ A trigger is PL/SQL code block which is executed by an event which
occurs to a database table.
○ Triggers are implicitly invoked when INSERT, UPDATE or DELETE
command is executed.
○ A trigger is associated with a table or a view. When a view is used, the
base table triggers are normally enabled.
○ Triggers are stored as text and compiled at execute time, because of
this it is wise not to include much code in them. You may not use
COMMIT, ROLLBACK and SAVEPOINT statements within trigger
blocks.
○ The advantages of using trigger are :
■ It creates consistency and access restrictions to the database.
■ It implements security.
○ A trigger is created with CREATE TRIGGER command.
Syntax:
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE / AFTER / INSTEAD OF}
{DELETE /INSERT/UPDATE [OF column [,column....]}
ON {TABLE/VIEW}
FOR EACH {ROW / STATEMENT}
[WHEN (condition)]
PL/SQL block
○ The BEFORE trigger is used when some processing is needed before
execution of the command. The AFTER trigger is triggered only after
the execution of the associated triggering commands. INSTEAD OF
trigger is applied to view only.
○ Triggers may be called BEFORE or AFTER the following events :
INSERT, UPDATE and DELETE.
○ Thus every trigger is divided into three components as :
■ Triggering event - The trigger can be activated by a SQL
command or by system event or a user event which are called
triggering events.
■ Triggering restriction - WHEN clause is used to specify
triggering restriction i.e. it specifies what condition must be true
for the trigger to be activated.
■ Triggering action - PL/SQL block is a trigger action.
○ A trigger can be modified using OR REPLACE clause of CREATE
TRIGGER command.
○ A value of a column of a ROW-LEVEL trigger can be accessed using
NEW and OLD variable.
Syntax :
Column_name : NEW
Column_name : OLD
○ To enable or disable a specific trigger, ALTER TRIGGER command is
used.
Syntax :
ALTER TRIGGER trigger_name ENABLE / DISABLE ;
○ When a trigger is created, it is automatically enabled and it gets
executed according to the triggering command.
○ To enable or disable all the triggers of a table, ALTER TABLE command
is used.
Syntax :
ALTER TABLE table_name ENABLE / DISABLE ALL TRIGGERS;
○ To delete a trigger, use DROP TRIGGER command.
Syntax :
DROP TRIGGER trigger_name;
○ Ex.
CREATE TRIGGER tr_sal
BEFORE INSERT ON emp
FOR EACH ROW
BEGIN
IF :new.sal = 0 THEN
Raise_application_error('-20010','Salary should be greater than 0');
END IF;
END;
Best suited for complex queries Not so good for complex queries
Not suited for hierarchical data Best suited for hierarchical data
storage storage