DBMS Cep 6
DBMS Cep 6
GRANT:
• This command gives rights to user for a data items.
Syntax:-
• GRANT privilege ON object TO user [WITH GRANT OPTION]
REVOKE:
• This command takes back rights from user for a data items.
Syntax:- REVOKE privilege ON object FROM user {RESTRICT/CASCADE}
• Privileges are various operations that we will perform on table or view E.g.
INSERT, UPDATE, DELETE, SELECT etc.
• Object is table or view or any data item.
• User is the name of user.
• With grant option is used if user wants to pass the rights to other user. Means if a
user get a rights with the grant option then he/she can give this rights to another
user.
• When user executes a REVOKE command with the cascade option then it will
take back given rights from all the users who get those rights from this user.
ROLLBACK:
• The execution of ROLLBACK erases all the modification made by the current
transaction.
• once the ROLLBACK statement is executed the database reaches its previous
state.
• Example: Before the execution of first statement of the transaction.
• ROLLBACK occurs when the transaction is aborted in middle of the execution.
• Syntax:- ROLLBACK;
COMMIT:
• COMMIT validates the modification made by the current transaction .
• After execution of COMMIT statement the transaction can not be ROLLBACK.
• COMMIT occurs when the transaction gets executed successfully.
• Syntax:- COMMIT;
33. List and explain aggregation functions with suitable example
Aggregate Functions are all about
• Performing calculations on multiple rows
• Of a single column of a table
• And returning a single value.
The ISO standard defines five (5) aggregate functions namely;
1) COUNT
2) SUM
3) AVG
4) MIN
5) MAX
COUNT Function
➔ The COUNT function returns the total number of values in the specified field. It
works on both numeric and non-numeric data types. All aggregate functions
bydefault exclude nulls values before working on the data.
➔ COUNT (*) is a special implementation of the COUNT function that returns the
count of all the rows in a specified table. COUNT (*) also considers Nulls and
duplicates.
DISTINCT Keyword
➔ The DISTINCT keyword that allows us to omit duplicates from our results
➔ SELECT COUNT(DISTINCT column_name) FROM tablename
MIN function
➔ The MIN function returns the smallest value in the specified table field.
➔ SELECT MIN(column_name) FROM tablename;
MAX function
➔ Just as the name suggests, the MAX function is the opposite of the MIN
function. It returns the largest value from the specified table field.
➔ SELECT MAX(column_name) FROM tablename;
SUM function
➔ SUM function which returns the sum of all the values in the specified column.
SUM works on numeric fields only. Null values are excluded from the result
returned.
➔ SELECT SUM(column_name) FROM tablename;
AVG function
➔ AVG function returns the average of the values in a specified column. Just like
the SUM function, it works only on numeric data types.
➔ SELECT AVG(column_name) FROM tablename;
1. %FOUND
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one
or more rows or a SELECT INTO statement returned one or more rows.
Otherwise, it returns FALSE.
2. %NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT,
UPDATE, or DELETE statement affected no rows, or a SELECT INTO
statement returned no rows. Otherwise, it returns FALSE.
3. %ISOPEN
Always returns FALSE for implicit cursors, because Oracle closes the SQL
cursor automatically after executing its associated SQL statement.
4. %ROWCOUNT
Returns the number of rows affected by an INSERT, UPDATE, or DELETE
statement, or returned by a SELECT INTO statement.
Explicit Cursors
Explicit cursors are programmer-defined cursors for gaining more control over
the context area. An explicit cursor should be defined in the declaration section of the
PL/SQL Block. It is created on a SELECT Statement which returns more than one row.
(2) Triggers
Triggers are stored programs, which are automatically executed or fired when some
events occur. Triggers are, in fact, written to be executed in response to any of the
following events −
• A Database Manipulation (DML) statement (DELETE, INSERT OR
UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
Triggers can be defined on the table, view, schema, or database with which the event
is associated.
Advantages of Triggers
• Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
Creating Triggers
The syntax for creating a trigger is −
CREATE [OR REPLACE ] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF }
{INSERT [OR] | UPDATE [OR] | DELETE}
[OF col_name]
ON table_name
[REFERENCING OLD AS NEW AS n]
[FOR EACH ROW]
WHEN (condition)
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
Where,
• CREATE [OR REPLACE] TRIGGER trigger_name − Creates or replaces an
existing trigger with the trigger_name.
• {BEFORE | AFTER | INSTEAD OF} − This specifies when the trigger will be
executed. The INSTEAD OF clause is used for creating trigger on a view.
• {INSERT [OR] | UPDATE [OR] | DELETE} − This specifies the DML
operation.
• [OF col_name] − This specifies the column name that will be updated.
• [ON table_name] − This specifies the name of the table associated with the
trigger.
• [REFERENCING OLD AS o NEW AS n] − This allows you to refer new and
old values for various DML statements, such as INSERT, UPDATE, and
DELETE.
• [FOR EACH ROW] − This specifies a row-level trigger, i.e., the trigger will 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.
• WHEN (condition) − This provides a condition for rows for which the trigger
would fire. This clause is valid only for row-level triggers.
Example
EXEC SelectAllCustomers City = "Mexico", PostalCode = "05021";
36. Write PL/SQL code to print sum of even numbers between 1 to 100 numbers
DECLARE
-- Declare variable
num NUMBER(3) := 0;
sum NUMBER(4) := 0;
BEGIN
WHILE num <= 100 LOOP
sum := sum1 + num;
num := num + 2;
END LOOP;
dbms_output.Put_line('Sum of even numbers is ' || sum);
END;