Database Design and Programming 20210071
Database Design and Programming 20210071
Programming
Tahaluf Training Center 2021
1 Overview of Database
What is DBMS?
▪ MySQL.
▪ Oracle.
▪ SQL Server.
▪ IBM DB2.
▪ PostgreSQL.
▪ Amazon SimpleDB (cloud based) etc.
3. ACID-compliant.
1. Requirement analysis.
2. ER – Diagram.
3. Schema Diagram.
4. Class Diagram.
Problem Statement
Create Tables
View Data
Example
In a LMS System, there are many courses teach by a many teachers. Each
course has a name, status, date from and date to. Each course has an
exam. In this system, there are many employees work in different
positions.
Example Solution
Relationships:
1. Many-to-Many: Course and Teacher.
2. One-to-One: Course and Exam.
3. One-to-Many: Role and Employee.
Exercise
In a hospital System, there are many doctors and other employees work.
Doctors treat patients and provide them with care and medicines. There is
a room for every patient entering the hospital.
Exercise Solution
Relationships:
1. Many-to-Many: Patient and Medicine.
2. One-to-One: Patient and room.
3. One-to-Many: Role and Employee, Employee and Patient.
What is ER - Model?
What is ER - Diagram?
ER – Diagram Components
ER – Diagram Components
ER – Diagram Components
ER – Diagram Components
Draw ER – Diagram
Draw ER – Diagram
Example
1. Create an entities.
Draw ER – Diagram
2. Create a relationships.
Draw ER – Diagram
2. Create a relationships.
Draw ER – Diagram
3. Create an attributes.
Exercise
Exercise Solution
Example:
Example:
Example:
Example:
Example:
6. DATE: Valid date range from January 1, 4712 BC, to December 31,
9999 AD.
Example:
Example:
Example:
Example:
Example:
Example:
Example:
Example:
);
15. ROWID: Base 64 string representing the unique address of a row in its
table.
Example:
);
Example:
);
Example:
Example:
Example:
Example:
Example:
21. BFILE: Contains a locator to a large binary file stored outside the
database.
Example:
It only display the database design and does not display the actual data of
the database.
Schema can be a single table or more than one table which is related.
Example
Exercise
In a hospital System, there are many doctors and other employees work.
Doctors treat patients and provide them with care and medicines. There is
a room for every patient entering the hospital.
Example
Exercise
In a hospital System, there are many doctors and other employees work.
Doctors treat patients and provide them with care and medicines. There is
a room for every patient entering the hospital.
3 Constraints
SQL uses certain commands like Create, Drop, Insert etc. to carry out
the required tasks.
DML SQL commands that deals with the manipulation of data present
in database belong to DML or Data Manipulation Language and this
includes most of the SQL statements.
3 Constraints
Steps:
1. Create user username identified by password;
2. Grant permission to username .
3. Connect userename/password .
Use the CREATE TABLE statement to create one of the following types
of tables:
Tables are created with no data unless a subquery is specified. You can add
rows to a table with the INSERT statement.
After creating a table, you can define additional columns, partitions, and
integrity constraints with the ADD clause of the ALTER TABLE statement.
You can change the definition of an existing column or partition with the
MODIFY clause of the ALTER TABLE statement.
Syntax :
Example :
The tables on which a view is based are called base tables. The view is stored
as a SELECT statement in the data dictionary.
Example:
A typical usage for sequences is to create a primary key value, which must be
unique for each row.
Example:
An Oracle server index is a schema object that can speed up the retrieval of
rows by using a pointer.
An index provides direct and fast access to rows in a table. Its purpose is to
reduce the necessity of disk I/O by using an indexed path to locate data
quickly.
The index is used and maintained automatically by the Oracle server. Once an
index is created, no direct activity is required by the user.
Indexes are logically and physically independent of the table they index. This
means that they can be created or dropped at any time and have no effect on
the base tables or other indexes.
Example :
CREATE INDEX emp_last_name_idx
ON employees(last_name);
To refer to a table owned by another user, you need to prefix the table name
with the name of the user who created it followed by a period.
Creating a synonym eliminates the need to qualify the object name with the
schema and provides you with an alternative name for a table, view,
sequence, procedure, or other objects.
This method can be especially useful with lengthy object names, such as
views.
In the syntax:
Syntax:
DESCRIBE tablename
Example:
DESCRIBE DEPT
Syntax:
Select * from TableName;
Example:
Select * from DEPT;
3 Constraints
Oracle Database lets you create six types of constraints and lets you declare
them in two ways.
• unique constraint prohibits multiple rows from having the same value in
the same column or combination of columns but allows some values to
be null.
NOT NULL constraints must be declared inline. All other constraints can be
declared either inline or out of line.
The referenced table is called the parent table while the table with the
foreign key is called the child table. The foreign key in the child table will
generally reference a primary key in the parent table.
foreign key with cascade delete means that if a record in the parent table is
deleted, then the corresponding records in the child table will automatically
be deleted. This is called a cascade delete in Oracle.
A foreign key with a cascade delete can be defined in either a CREATE TABLE
statement or an ALTER TABLE statement.
We could also create a foreign key (with a cascade delete) with more than
one field as in the example below:
A foreign key with "set null on delete" means that if a record in the parent
table is deleted, then the corresponding records in the child table will have
the foreign key fields set to null. The records in the child table will not be
deleted.
A foreign key with a "set null on delete" can be defined in either a CREATE
TABLE statement or an ALTER TABLE statement.
In this example, we've created a foreign key "with a set null on delete"
called fk_supplier that references the supplier table based on the
supplier_id field.
We could also create a foreign key "with a set null on delete" with more
than one field as in the example below:
select constraint_name,constraint_type,table_name
from user_constraints
Insert
Update
Delete
DML Commands
1. INSERT.
2. UPDATE.
3. DELETE.
4. SELECT.
Exercise
Exercise Solution
Exercise Solution
This command can used to add one or more records to a single table in a
database.
Syntax
There are many mistakes that users make with the insert statement:
Example
Example Solution
Exercise
Insert a three record into Course table. Then, Insert a five record into
Exam table.
Exercise Solution
Exercise Solution
Syntax
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
Example
Example Solution
UPDATE Course
SET Name = 'English100'
WHERE Description = 'English';
Exercise
Exercise Solution
UPDATE Exam
SET Name = 'Final Exam'
WHERE id = 2;
Syntax
Example
Example Solution
Exercise
Exercise Solution
Syntax
Example
Example Solution
FROM table;
SELECT Example
SELECT * Example
Null value
Syntax
column_name AS alias_name
Syntax
table_name alias_name
Example
SELECT DISTINCT name, startdate, enddate, description
FROM Course;
Example
Comparison Operator
= : equal
Example
Comparison Operator
Example
Comparison Operator
!= : Not equal
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
Example
Comparison Operator
LIKE Examples
Comparison Operator
LIKE Examples
SQL function
SQL function
✓ Character.
✓ Number.
✓ Date.
✓ Conversion.
✓ When a program crashes, all the updates to this point are rolled
back implicitly.
Commit Example
Commit Example
ROLLBACK Example
ROLLBACK Example
ROLLBACK;
SELECT * FROM DEPT;
Automatic commit
Automatic Rollback
SELECT *
FROM DEPT
WHERE DNAME = 'Admin'
FOR UPDATE
ORDER BY DEPTNO;
2 INNER JOIN
3 LEFT OUTER
6 Queries
The related tables of a large database are linked through the use of
foreign and primary keys or what are often referred to as common
columns.
The ability to join tables will enable you to add more meaning to the
result table that is produced.
The INNER join is such a join when equijoins and non equijoins are
performed, rows from the source and target tables are matched using
a join condition formulated with equality and inequality operators,
respectively. These are referred to as inner joins.
Syntax:
It returns all rows from the table A as well as the unmatched rows
from the table B. For all rows in A that have no matching rows in B,
Oracle Database returns null for any select list expressions containing
columns of B.
Syntax:
A RIGHT OUTER JOIN performs an inner join of two tables (supposed table A
which writes before the join keyword and table B which writes after the join
keyword in the SQL statement ) based on the condition specified after the ON
keyword.
It returns all rows from the table B as well as the unmatched rows from the
table A. For all rows in B that have no matching rows in A, Oracle Database
returns null for any select list expressions containing columns of A.
Syntax:
A full outer join performs a join between two tables that returns the results
of an INNER join as well as the results of a left and right outer join
Syntax:
Q1: Write a SQL query to display the item name, price, and company name
of all the products.
Solution:
SELECT item_mast.pro_name, pro_price,
company_mast.com_name FROM item_mast INNER JOIN
company_mast ON item_mast.pro_com =
company_mast.com_id;
Q2: The following query retrieves all the matching rows in the departments
table, and employees table for the criteria same department_id in both
tables, and also those rows from employees table even if there is no match in
the departments table.
Solution:
SELECT e.first_name, e.department_id,
d.department_name FROM employees e RIGHT OUTER JOIN
departments d ON (e.department_id = d.department_id);
Solution:
1 Subqueries
2 VIEW
4 Synonyms
Types of Subqueries:
• Single Row Sub Query: Sub query which returns single row output. They
mark the usage of single row comparison operators, when used in WHERE
conditions.
• Multiple row sub query: Sub query returning multiple row output. They
make use of multiple row comparison operators like IN, ANY, ALL. There
can be sub queries returning multiple columns also.
This type of subquery also includes subqueries that use the EXISTS operator
to test the existence of data rows satisfying specified criteria.
In the below SELECT query, inner SQL returns only one row i.e. the
minimum salary for the company. It in turn uses this value to
compare salary of all the employees and displays only those, whose
salary is equal to minimum salary.
Syntax:
The syntax for the CREATE VIEW Statement in Oracle/PLSQL is:
view_name
It specifies the name of the Oracle VIEW that you want to create.
WHERE conditions
Optional. The conditions that must be met for the records to be
included in the VIEW.
In this example, we are creating two tables suppliers and orders first.
This can be useful when you need to create a unique number to act
as a primary key.
Syntax:
The syntax to create a sequence in Oracle is:
sequence_name
The name of the sequence that you wish to create.
If you omit the MAXVALUE option, your sequence will automatically default
to:
MAXVALUE 999999999999999999999999999
To retrieve the next value in the sequence order, you need to use nextval.
supplier_seq.NEXTVAL;
Once you have created your sequence in Oracle, you might find that you
need to remove it from the database.
Syntax:
Thinking Question !!
Question: While creating a sequence, what does cache and nocache options
mean?
For example, you could create a sequence with a cache of 20 as follows:
Answer: With respect to a sequence, the cache option specifies how many
sequence values will be stored in memory for faster access.
This results in a "gap" in the assigned sequence values. When the system
comes back up, Oracle will cache new numbers from where it left off in the
sequence, ignoring the so called "lost" sequence values.
Answer: You can change the LASTVALUE for an Oracle sequence, by executing
an ALTER SEQUENCE command.
For example, if the last value used by the Oracle sequence was 100 and you
would like to reset the sequence to serve 225 as the next value. You would
execute the following commands.
You generally use synonyms when you are granting access to an object from
another schema and you don't want the users to have to worry about
knowing which schema owns the object.
Syntax:
OR REPLACE:
Allows you to recreate the synonym (if it already exists) without having to
issue a DROP synonym command.
Syntax:
PUBLIC
It means that the synonym is a public synonym and is accessible to all users.
Remember though that the user must first have the appropriate privileges to
the object to use the synonym.
Syntax:
schema
The appropriate schema. If this phrase is omitted, Oracle assumes that you
are referring to your own schema.
object_name:
The name of the object for which you are creating the synonym. It can be one
of the following:
• table
• view
• sequence
• stored procedure
• function
• package
• materialized view
• java class schema object
• user-defined object
• synonym
Example:
Now, users of other schemas can reference the table called suppliers without
having to prefix the table name with the schema named app.
Now, users of other schemas can reference the table called suppliers without
having to prefix the table name with the schema named app:
SELECT *
FROM suppliers;
If this synonym already existed and you wanted to redefine it, you could
always use the OR REPLACE phrase as follows:
Once a synonym has been created in Oracle, you might at some point need
to drop the synonym.
Syntax:
PUBLIC
Allows you to drop a public synonym. If you have specified PUBLIC, then you
don't specify a schema.
force
It will force Oracle to drop the synonym even if it has dependencies. It is
probably not a good idea to use force as it can cause invalidation of Oracle
objects.
Example:
This DROP statement would drop the synonym called suppliers that we
defined earlier.
1 PL/SQL
2 Anonymous Block
3 Data Types
4 Variables
5 Comments
SQL is a popular language for both querying and updating data in the
relational database management systems (RDBMS).
Example :
SET SERVEROUTPUT ON
BEGIN
DBMS_OUTPUT.put_line ('Hello World!');
END;
/
/
Welcome in Tahluf
DECLARE
l_message VARCHAR2( 255 ) := 'Hello World!';
BEGIN
DBMS_OUTPUT.PUT_LINE( l_message );
END;
DECLARE
v_result NUMBER;
BEGIN
v_result := 1 / 0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE( SQLERRM );
END;
The scalar types are types that store single values such as number,
Boolean, character, and datetime .
composite types are types that store multiple values, for example,
record and collection
Hence, you should always use PLS_INTEGER values for all calculation
in its range to increase the efficiency of programs.
The BOOLEAN datatype has three data values: TRUE, FALSE, and
NULL. Boolean values are typically used in control flow structure such
as IF-THEN, CASE, and loop statements like LOOP, FOR LOOP, and
WHILE LOOP.
SQL does not have the BOOLEAN data type, therefore, you cannot:
PL/SQL uses the SQL character data types such as CHAR, VARCHAR2,
LONG, RAW, LONG RAW, ROWID, and UROWID.
It is not a good practice to use data type synonym unless you are
accessing a non-Oracle Database.
• First, specify the name of the variable. The name of the variable
should be as descriptive as possible, e.g., l_total_sales,
l_credit_limit, and l_sales_revenue.
DECLARE
l_total_sales NUMBER(15,2);
l_credit_limit NUMBER (10,0);
l_contact_name VARCHAR2(255);
BEGIN
NULL;
END;
Default values:
DECLARE
l_product_name VARCHAR2( 100 ) := 'Laptop';
BEGIN
NULL;
END;
DECLARE
l_product_name VARCHAR2(100) DEFAULT 'Laptop';
BEGIN
NULL;
END;
If you impose the NOT NULL constraint on a value, then the variable
cannot accept a NULL value.
DECLARE
l_shipping_status VARCHAR2( 25 ) NOT NULL := 'Shipped';
BEGIN
l_shipping_status := '';
END;
Anchored declarations:
Typically, you declare a variable and select a value from a table column to this
variable.
If the data type of the table column changes, you must adjust the program to
make it work with the new type.
PL/SQL allows you to declare a variable whose data type anchor to a table
column or another
DECLARE
l_name DEPT.DNAME%TYPE;
l_loc DEPT.loc%TYPE;
BEGIN
SELECT
DNAMe, loc
INTO
l_name, l_loc
FROM
DEPT
WHERE
DEPTNO = 3;
DBMS_OUTPUT.PUT_LINE(l_name || ':' || l_loc );
END;
/
DECLARE
l_Name student.name%TYPE;
l_average_mark student.mark%TYPE;
l_max_mark student.mark%TYPE;
l_min_mark student.mark%TYPE;
BEGIN
-- get credit limits
SELECT
MIN(mark),
MAX(mark),
AVG(mark)
INTO
l_min_mark,
l_max_mark,
l_average_mark
FROM student;
SELECT
name
INTO
l_name
FROM
student
WHERE
id=1;
When compiling the PL/SQL code, the Oracle precompiler ignores comments.
However, you should always use comments to make your code more
readable and to help you and other developers understand it better in the
future.
1 Conditional control
3 Exception
4 Records
5 Cursors
• IF THEN
• IF THEN ELSE
• IF THEN ELSIF
IF THEN statement:
The condition is a Boolean expression that always evaluates to TRUE,
FALSE, or NULL.
Syntax :
IF condition THEN
statements;
END IF;
Example :
IF condition THEN
statements;
ELSE
else_statements;
END IF;
Example :
DECLARE
n_sales NUMBER := 300000;
n_commission NUMBER( 10, 2 ) := 0;
BEGIN
IF n_sales > 200000 THEN
n_commission := n_sales * 0.1;
ELSE
n_commission := n_sales * 0.05;
END IF;
END;
IF condition_1 THEN
statements_1
ELSIF condition_2 THEN
statements_2
[ ELSE
else_statements
]
END IF;
Example :
DECLARE
n_sales NUMBER := 300000;
n_commission NUMBER( 10, 2 ) := 0;
BEGIN
IF n_sales > 200000 THEN
n_commission := n_sales * 0.1;
ELSIF n_sales <= 200000 AND n_sales > 100000 THEN
n_commission := n_sales * 0.05;
ELSIF n_sales <= 100000 AND n_sales > 50000 THEN
n_commission := n_sales * 0.03;
ELSE
n_commission := n_sales * 0.02;
END IF;
END;
Nested IF statement:
You can nest an IF statement within another IF statement as shown
below:
IF condition_1 THEN
IF condition_2 THEN
nested_if_statements;
END IF;
ELSE
else_statements;
END IF;
The CASE statement has two types: simple CASE statement and
searched CASE statement.
DECLARE
c_grade CHAR( 1 );
c_rank VARCHAR2( 20 );
BEGIN
c_grade := 'B';
CASE c_grade
WHEN 'A' THEN
c_rank := 'Excellent' ;
WHEN 'B' THEN
c_rank := 'Very Good' ;
WHEN 'C' THEN
c_rank := 'Good' ;
WHEN 'D' THEN
c_rank := 'Fair' ;
WHEN 'F' THEN
c_rank := 'Poor' ;
ELSE
c_rank := 'No such grade' ;
END CASE;
DBMS_OUTPUT.PUT_LINE( c_rank );
END;
CASE
WHEN condition_1 THEN statements_1
WHEN condition_2 THEN statements_2
...
WHEN condition_n THEN statements_n
[ ELSE
else_statements ]
END CASE;]
DECLARE
n_sales NUMBER;
n_commission NUMBER;
BEGIN
n_sales := 150000;
CASE
WHEN n_sales > 200000 THEN
n_commission := 0.2;
WHEN n_sales >= 100000 AND n_sales < 200000 THEN
n_commission := 0.15;
WHEN n_sales >= 50000 AND n_sales < 100000 THEN
n_commission := 0.1;
WHEN n_sales > 30000 THEN
n_commission := 0.05;
ELSE
n_commission := 0;
END CASE;
GOTO label_name;
The label_name is the name of a label that identifies the target statement. In
the program, you surround the label name with double enclosing angle
brackets as shown below:
<<label_name>>;
BEGIN
GOTO second_message;
<<first_message>>
DBMS_OUTPUT.PUT_LINE( 'Hello' );
GOTO the_end;
<<second_message>>
DBMS_OUTPUT.PUT_LINE( 'PL/SQL GOTO
Demo' );
GOTO first_message;
<<the_end>>
DBMS_OUTPUT.PUT_LINE( 'and good bye...' );
END;
DECLARE
l_counter NUMBER := 0;
BEGIN
LOOP
l_counter := l_counter + 1;
IF l_counter > 3 THEN
EXIT;
END IF;
dbms_output.put_line( 'Inside loop: ' || l_counter ) ;
END LOOP;
-- control resumes here after EXIT
dbms_output.put_line( 'After loop: ' || l_counter );
END;
BEGIN
FOR l_counter IN 1..5
LOOP
DBMS_OUTPUT.PUT_LINE( l_counter );
END LOOP;
END;
DECLARE
l_step PLS_INTEGER := 2;
BEGIN
FOR l_counter IN 1..5 LOOP
dbms_output.put_line (l_counter*l_step);
END LOOP;
END;
DECLARE
l_counter PLS_INTEGER := 10;
BEGIN
FOR l_counter IN 1.. 5 loop
DBMS_OUTPUT.PUT_LINE (l_counter);
end loop;
-- after the loop
DBMS_OUTPUT.PUT_LINE (l_counter);
END;
WHILE condition
LOOP
statements;
END LOOP;
DECLARE
n_counter NUMBER := 1;
BEGIN
WHILE n_counter <= 5
LOOP
DBMS_OUTPUT.PUT_LINE( 'Counter : ' ||
n_counter );
n_counter := n_counter + 1;
END LOOP;
END;
The following example is the same as the one above except that it has
an additional EXITWHEN statement.
DECLARE
n_counter NUMBER := 1;
BEGIN
WHILE n_counter <= 5
LOOP
DBMS_OUTPUT.PUT_LINE( 'Counter : ' || n_counter );
n_counter := n_counter + 1;
EXIT WHEN n_counter = 3;
END LOOP;
END;
The CONTINUE statement allows you to exit the current loop iteration
and immediately continue on to the next iteration of that loop.
CONTINUE;
IF condition THEN
CONTINUE;
END IF;
BEGIN
FOR n_index IN 1 .. 10
LOOP
-- skip odd numbers
IF MOD( n_index, 2 ) = 1 THEN
CONTINUE;
END IF;
DBMS_OUTPUT.PUT_LINE( n_index );
END LOOP;
END;
BEGIN
FOR n_index IN 1 .. 10
LOOP
-- skip even numbers
CONTINUE
WHEN MOD( n_index, 2 ) = 0;
DBMS_OUTPUT.PUT_LINE( n_index );
END LOOP;
END;
DECLARE
c_id student.id%type := 2;
c_name student.Name%type;
BEGIN
SELECT name, id INTO c_name, c_id
FROM student
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('id: ' || c_id);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such student!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
PL/SQL record helps you simplify your code by shifting from field-
level to record-level operations.
DECLARE
record_name table_name%ROWTYPE;
DECLARE
r_person persons%ROWTYPE;
BEGIN
-- assign values to person record
r_person.person_id := 1;
r_person.first_name := 'John';
r_person.last_name := 'Doe';
DECLARE
r_person persons%ROWTYPE;
BEGIN
-- get person data of person id 1
SELECT * INTO r_person
FROM persons
WHERE person_id = 1;
The implicit cursor is not elegant when the query returns zero or
multiple rows which cause NO_DATA_FOUND or TOO_MANY_ROWS
exception respectively.
For an explicit cursor, you have control over its execution cycle from
OPEN, FETCH, and CLOSE.
Declare a cursor
Before using an explicit cursor, you must declare it in the declaration
section of a block or package as follows:
In this syntax:
• First, specify the name of the cursor after the CURSOR keyword.
• Second, define a query to fetch data after the IS keyword.
Open a cursor
Before start fetching rows from the cursor, you must open it. To open
a cursor, you use the following syntax:
OPEN cursor_name;
To retrieve all rows in a result set, you need to fetch each row till the
last one.
Closing a cursor
After fetching all rows, you need to close the cursor with the CLOSE
statement:
CLOSE cursor_name;
cursor_name%attribute
• %ISOPEN
This attribute is TRUE if the cursor is open or FALSE if it is not.
• %FOUND
This attribute has four values:
1. NULL before the first fetch
2. TRUE if a record was fetched successfully
3. FALSE if no row returned
4. INVALID_CURSOR if the cursor is not opened
• %NOTFOUND
This attribute has four values:
1. NULL before the first fetch
2. FALSE if a record was fetched successfully
3. TRUE if no row returned
4. INVALID_CURSOR if the cursor is not opened
• %ROWCOUNT
The %ROWCOUNT attribute returns the number of rows fetched from
the cursor. If the cursor is not opened, this attribute returns
INVALID_CURSOR.
We will use the orders and order_items tables from the sample
database for the demonstration.
CURSOR c_sales IS
SELECT * FROM sales
ORDER BY total DESC;
3 Overview of Function
4 Create Function
5 Overview of Package
6 Create Package
Syntax
Setting a returned value for the OUT parameter and return it to call a
program.
Example
Example
EXECUTE TahalufTrainees;
Example
EXECUTE TahalufTrainees;
Example
Example
DECLARE
a number;
b number;
c number;
PROCEDURE findMin(x IN number, y IN number, z OUT
number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
Tahaluf Training Centre 05 Jul 2021
Create Stored Procedure
Example
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' ||
c);
END;
Example
DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
Example
Example
Example
DECLARE
c number(2);
BEGIN
c := TotalStudent();
dbms_output.put_line('Total no. of student: ' || c);
END;
Example
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x number, y number)
RETURN number
IS
z number;
BEGIN
Example
IF x > y THEN
z:= x;
RETURN z;
else
z:=y;
return z;
end if;RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
✓ Package specification.
Example
The package body has the codes for different ways declared in the
package specification.
Private declarations are hidden from the code outside the package.
Example
Example
Example
DECLARE
code student.id%type:= 1;
BEGIN
Package_student.addstudent(20, 'R', 93);
Package_student.addstudent(50, 'M', 93);
Package_student.delstudent(code);
END;
Benefits of Triggers
7. Auditing.
Syntax
Example
Example
Example
Example
UPDATE
student
SET
name= 'Mohammed'
WHERE
id =1;
Example
Example
Example
Syntax
Example
Example
UPDATE
customers
SET
credit_limit = credit_limit * 110;
Syntax
Example
Example
INSERT INTO
vw_customers(
name,
address,
website,
credit_limit,
first_name,
last_name,
email,
phone
)
Example
VALUES(
'Ahmed',
'Irbid',
'Ahmed.com',
2000,
'Ahmed',
'mohammed',
'[email protected]',
'0782944885'
);
Example
Example
Example
INSERT INTO
vw_customers(
name,
address,
website,
credit_limit,
first_name,
last_name,
email,
phone
)
Example
VALUES(
'Ahmed',
'Irbid',
'Ahmed.com',
2000,
'Ahmed',
'mohammed',
'[email protected]',
'0782944885'
);
Example
[1]. https://www.javatpoint.com/oracle-delete
[2]. https://www.techonthenet.com/oracle/truncate.php
[3]. https://en.wikibooks.org/wiki/Oracle_Database/SELECT_Statement
[4]. https://ramkedem.com/en/oracle-where-clause/
[5].https://docs.oracle.com/cd/A87860_01/doc/server.817/a85397/operator.
htm
[6]. https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj13658.html
[7]. https://www.javatpoint.com/oracle-group-by-clause
[8]. PL/SQL Procedure: A Step-by-step Guide to Create a Procedure
(oracletutorial.com)
[9]. Oracle INSTEAD OF Triggers By Practical Examples (oracletutorial.com)
[10]. https://www.studytonight.com/dbms/overview-of-dbms.php
[11]. https://strolve.com/what-is-software-development-life-cycle-sdlc/
[12]. https://ecomputernotes.com/database-system/rdbms/phases-of-design-
methodology
[13]. https://www.oracle-dba-online.com/introduction_to_oracle.htm
[14]. https://www.oracletutorial.com/getting-started/what-is-oracle-database/
[15]. https://docs.oracle.com/database/121/SQLRF/sql_elements001.htm#SQLRF0021