SlideShare a Scribd company logo
Interacting with Oracle Database
Server:
SQL Statements in PL/SQL Programs
SELECT Statements in PL/SQL
• SELECT select_list INTO {variable_name[, variable_name]... | record_name}
FROM table [WHERE condition];
• Use the SELECT statement to retrieve data from the database.
– select_list: List of at least one column; can include SQL expressions, row
functions, or group functions
– variable_name: Scalar variable that holds the retrieved value
– record_name:PL/SQL record that holds the retrieved values
– Table: Specifies the database table name
– Condition:
• Ex1:
DECLARE
v_fname VARCHAR2(25);
BEGIN
SELECT custName INTO v_fname
FROM tbCustomer WHERE custID=2;
DBMS_OUTPUT.PUT_LINE(' cust name is : '||v_fname);
END;
SELECT Statements in PL/SQL
• EX1:
SELECT EMPID, EMPNAME,GENDER,ADDRESS,DOB,SSID
FROM TBEMPLOYEE;
• EX:SELECT DISTINCT ADDRESS FROM TBEMPLOYEE;
• Ex2:
DECLARE
empName TBEMPLOYEE.EMPNAME%TYPE;
empDob TBEMPLOYEE.DOB%TYPE;
empSalary TBEMPLOYEE.SALARY%TYPE;
BEGIN
SELECT EMPNAME,dob, salary
INTO empName,empDob, empSalary
FROM TBEMPLOYEE
WHERE EMPNAME = 'Chan Da' AND GENDER='Male' AND Address='Phnom Penh';
DBMS_OUTPUT.PUT_LINE ('Name is :'|| empName);
DBMS_OUTPUT.PUT_LINE ('Salary is :'|| empSalary);
DBMS_OUTPUT.PUT_LINE ('DOB is :'|| empDob);
END;
Manipulating data with PL/SQL
• Make changes to database tables by using DML commands:
• The INSERT statement adds new rows to the table.
• The UPDATE statement modifies existing rows in the table.
• The DELETE statement removes rows from the table.
• The MERGE statement selects rows from one table to update or insert into another
table.
• EX1:
BEGIN
INSERT INTO employees
(employee_id, first_name, last_name, email,hire_date, job_id, salary)
VALUES(employees_seq.NEXTVAL, 'Ruth', 'Cores','RCORES',CURRENT_DATE, 'AD_ASST',
4000);
END;
EX2:
BEGIN
INSERT INTO TBEMPLOYEE(EMPID,EMPNAME,GENDER, ADDRESS,DOB,SSID,SALARY)
VALUES(4, 'Ruth', 'FEMALE','PHNOM PENH','1-JAN-1979', '112233',123);
END;
Manipulating data with PL/SQL
• EX2: Update
DECLARE
sal_increase employees.salary%TYPE := 800;
BEGIN
UPDATE employees
SET salary = salary + sal_increase
WHERE job_id = 'ST_CLERK';
END;
• Ex3:
DECLARE
GENDER_V TBEMPLOYEE.GENDER%TYPE := 'FEMALE';
BEGIN
UPDATE TBEMPLOYEE SET GENDER=GENDER_V WHERE EMPID=4;
END;
• EX3:Delete
DECLARE
deptno employees.department_id%TYPE := 10;
BEGIN
DELETE FROM employees
WHERE department_id = deptno;
END;
EX4:
DECLARE
GENDER_VAR VARCHAR2(20) :='MALE';
BEGIN
DELETE FROM TBEMPLOYEE WHERE
GENDER=GENDER_VAR;
END;
Manipulating data with PL/SQL
• Syntax: Merge
BEGIN
MERGE INTO copy_emp c
USING employees e
ON (e.employee_id = c.empno)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
. . .
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id, e.first_name, e.last_name,
. . .,e.department_id);
END;
Manipulating data with PL/SQL
• EX4: Merge
BEGIN
MERGE INTO copy_emp c
USING employees e
ON (e.employee_id = c.empno)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
c.phone_number = e.phone_number,
c.hire_date = e.hire_date,
c.job_id = e.job_id,
c.salary = e.salary,
c.commission_pct = e.commission_pct,
c.manager_id = e.manager_id,
c.department_id = e.department_id
WHEN NOT MATCHED THEN
INSERT VALUES(e.employee_id,
e.first_name, e.last_name,
e.email, e.phone_number, e.hire_date,
e.job_id,
e.salary, e.commission_pct, e.manager_id,
e.department_id);
END;
Manipulating data with PL/SQL
• EX5:
BEGIN
MERGE INTO TBEMPLOYEEBACK c
USING TBEMPLOYEE e
ON (e.EMPID = c.EMPID)
WHEN MATCHED THEN
UPDATE SET
c.EMPNAME = e.EMPNAME,
c.GENDER = e.GENDER,
c.ADDRESS = e.ADDRESS,
c.DOB = e.DOB,
c.SSID = e.SSID,
c.SALARY= e.SALARY
WHEN NOT MATCHED THEN
INSERT VALUES(e.EMPID, e.EMPNAME, e.GENDER,
e.ADDRESS, e.DOB, e.SSID, e.SALARY);
END;
EX6:
MERGE INTO bonuses D USING (SELECT
employee_id, salary, department_id FROM
employees WHERE department_id = 80) S
ON (D.employee_id = S.employee_id)
WHEN MATCHED THEN UPDATE SET
D.bonus = D.bonus + S.salary*.01 DELETE
WHERE (S.salary > 8000) WHEN NOT
MATCHED THEN INSERT (D.employee_id,
D.bonus) VALUES (S.employee_id,
S.salary*0.1) WHERE (S.salary <= 8000);
Table Relationship
• In SQL Developer, go to View > Data Modeler > Browser. In the Browser view
right click on Relational Models and create a new one. This should create a new
blank diagram. You can drag and drop tables from the Connection view into the
diagram.
• ALTER TABLE TBPRODUCTS ADD
CONSTRAINT FK_PRODUCT_CATE_NUM
FOREIGN KEY (CATEID) REFERENCES TBCATEGORY(CATEID);
• ALTER TABLE TBSALE ADD
CONSTRAINT FK_SALE_CUSTOMER_NUM
FOREIGN KEY (CUSTID)
REFERENCES TBCUSTOMER(CUSTID);
Or using wizard by sql developer
JOIN operations
• The JOIN operations, which are among the possible TableExpressions in a FROM clause,
perform joins between two tables.
1. INNER JOIN operation: Specifies a join between two tables with an explicit join clause.
SYNTAX:
TableExpression [ INNER ] JOIN
TableExpression { ON booleanExpression | USING clause }
EX1:
SELECT *
FROM TBPRODUCTS INNER JOIN
TBCATEGORY ON
TBPRODUCTS.CATEID = TBCATEGORY.CATEID;
EX2:
SELECT * FROM SAMP.EMPLOYEE
INNER JOIN SAMP.STAFF ON
EMPLOYEE.SALARY < STAFF.SALARY
EX6:
MERGE INTO bonuses D USING (SELECT
employee_id, salary, department_id FROM
employees WHERE department_id = 80) S
ON (D.employee_id = S.employee_id)
WHEN MATCHED THEN UPDATE SET
D.bonus = D.bonus + S.salary*.01 DELETE
WHERE (S.salary > 8000) WHEN NOT
MATCHED THEN INSERT (D.employee_id,
D.bonus) VALUES (S.employee_id,
S.salary*0.1) WHERE (S.salary <= 8000);
JOIN operations
• Oracle JOINS are used to retrieve data from multiple tables. An Oracle JOIN is
performed whenever two or more tables are joined in a SQL statement.
• There are 4 different types of Oracle joins:
– Oracle INNER JOIN (or sometimes called simple join)
– Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN)
– Oracle RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
– Oracle FULL OUTER JOIN (or sometimes called FULL JOIN)
• INNER JOIN (SIMPLE JOIN)
• Oracle INNER JOINS return all rows from multiple tables where the join condition is
met. Syntax
– SELECT columns FROM table1 INNER JOIN table2 ON table1.column =
table2.column;
Ex1:
SELECT suppliers.supplier_id,
suppliers.supplier_name, orders.order_date
FROM suppliers INNER JOIN orders ON
suppliers.supplier_id = orders.supplier_id;
JOIN operations
• Old Syntax
– older implicit syntax as follows (but we still recommend using the INNER JOIN
keyword syntax):
– SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM
suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id;
JOIN operations
• LEFT OUTER JOIN
– This type of join returns all rows from the LEFT-hand table specified in the ON
condition and only those rows from the other table where the joined fields are
equal (join condition is met).
• Syntax: Oracle LEFT OUTER JOIN is:
• SELECT columns FROM table1 LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
• Ex1:
– SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM
suppliers LEFT OUTER JOIN orders ON suppliers.supplier_id = orders.supplier_id;
• Old Syntax
– LEFT OUTER JOIN example above
could be rewritten using the older
implicit syntax that utilizes the outer
join operator (+) as follows
(but we still recommend using the LEFT OUTER JOIN
keyword syntax):
SELECT suppliers.supplier_id, suppliers.supplier_name,
orders.order_date FROM suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id(+);
JOIN operations
• Right join returns all rows from the RIGHT-hand table specified in the ON condition
and only those rows from the other table where the joined fields are equal (join
condition is met).
• Syntax
• SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column =
table2.column;
EX:
SELECT orders.order_id, orders.order_date,
suppliers.supplier_name FROM suppliers RIGHT
OUTER JOIN orders ON suppliers.supplier_id =
orders.supplier_id;
Old Syntax:utilizes the outer join operator (+) as follows (but we still
recommend using the RIGHT OUTER JOIN keyword syntax):
SELECT orders.order_id, orders.order_date, suppliers.supplier_name
FROM suppliers, orders WHERE suppliers.supplier_id(+) =
orders.supplier_id;
JOIN operations
• FULL OUTER JOIN
• returns all rows from the LEFT-hand table and RIGHT-hand table with nulls in place
where the join condition is not met.
• Syntax
• SELECT columns FROM table1 FULL [OUTER] JOIN table2 ON table1.column =
table2.column;
Ex:
SELECT suppliers.supplier_id, suppliers.supplier_name,
orders.order_date FROM suppliers FULL OUTER JOIN
orders ON suppliers.supplier_id = orders.supplier_id;
Introducing SQL cursors
• EX4: Merge
BEGIN
MERGE INTO copy_emp c
USING employees e
ON (e.employee_id = c.empno)
WHEN MATCHED THEN
UPDATE SET
c.first_name = e.first_name,
c.last_name = e.last_name,
c.email = e.email,
c.phone_number = e.phone_number,
c.hire_date = e.hire_date,
c.job_id = e.job_id,
c.salary = e.salary,
c.commission_pct = e.commission_pct,
c.manager_id = e.manager_id,
c.department_id = e.department_id

More Related Content

PPTX
SQL Tutorial for Beginners
PDF
SQL JOINS
PPTX
Lab1 select statement
PPT
Intro to tsql unit 9
PPTX
Commands
PPTX
Oracle: Basic SQL
PDF
Database Management System 1
SQL Tutorial for Beginners
SQL JOINS
Lab1 select statement
Intro to tsql unit 9
Commands
Oracle: Basic SQL
Database Management System 1

What's hot (18)

PPT
Single row functions
PPT
MY SQL
PPT
SQL Introduction to displaying data from multiple tables
PPTX
Lab5 sub query
PDF
SQL Functions and Operators
PPTX
MULTIPLE TABLES
PDF
Introduction to oracle functions
PPT
Myth busters - performance tuning 101 2007
PPTX
Structured query language constraints
DOCX
PPT
PDF
Chapter9 more on database and sql
PPT
Les09 (using ddl statements to create and manage tables)
PPT
Les02 (restricting and sorting data)
PPT
PPT
Aggregate Functions,Final
PPTX
1. dml select statement reterive data
DOC
Single row functions
MY SQL
SQL Introduction to displaying data from multiple tables
Lab5 sub query
SQL Functions and Operators
MULTIPLE TABLES
Introduction to oracle functions
Myth busters - performance tuning 101 2007
Structured query language constraints
Chapter9 more on database and sql
Les09 (using ddl statements to create and manage tables)
Les02 (restricting and sorting data)
Aggregate Functions,Final
1. dml select statement reterive data
Ad

Viewers also liked (8)

PPTX
PL SQL Quiz | PL SQL Examples
PPT
Oracle forms les16
PDF
Oracle 11g PL/SQL notes
PPTX
Take Full Advantage of the Oracle PL/SQL Compiler
PPT
PL/SQL Introduction and Concepts
PPTX
PL/SQL Fundamentals I
PPTX
ORACLE PL SQL FOR BEGINNERS
PPTX
Oracle: PLSQL Commands
PL SQL Quiz | PL SQL Examples
Oracle forms les16
Oracle 11g PL/SQL notes
Take Full Advantage of the Oracle PL/SQL Compiler
PL/SQL Introduction and Concepts
PL/SQL Fundamentals I
ORACLE PL SQL FOR BEGINNERS
Oracle: PLSQL Commands
Ad

Similar to Interacting with Oracle Database (20)

PDF
Database Systems - SQL - DDL Statements (Chapter 3/3)
PDF
0808.pdf
PDF
0808.pdf
PPTX
SQL Data Manipulation language and DQL commands
PPTX
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
PPTX
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
PPTX
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
PPTX
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
PPTX
Data Manipulation Language.pptx
PDF
sql language
PPTX
DOODB_LAB.pptx
PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
PPTX
My SQL.pptx
PPTX
PDF
Structure query language, database course
PPT
Sql dml & tcl 2
PPT
PPT
Les08 (manipulating data)
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Oraclesql
Database Systems - SQL - DDL Statements (Chapter 3/3)
0808.pdf
0808.pdf
SQL Data Manipulation language and DQL commands
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SQL.pptx SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTE...
SAMPLE QUESTION PAPER (THEORY) CLASS: XII SESSION: 2024-25 COMPUTER SCIENCE...
Data Manipulation Language.pptx
sql language
DOODB_LAB.pptx
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
My SQL.pptx
Structure query language, database course
Sql dml & tcl 2
Les08 (manipulating data)
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Oraclesql

More from Chhom Karath (20)

PDF
set1.pdf
PPTX
Set1.pptx
PDF
orthodontic patient education.pdf
PDF
New ton 3.pdf
PPTX
ច្បាប់ញូតុនទី៣.pptx
PPTX
Control tipping.pptx
PPTX
Bulbous loop.pptx
PPTX
brush teeth.pptx
PPTX
bracket size.pptx
PPTX
arch form KORI copy.pptx
PPTX
Bracket size
PPTX
Couple
PPTX
ច្បាប់ញូតុនទី៣
PPTX
PPTX
Shoe horn loop
PPTX
Opus loop
PPTX
V bend
PPTX
Closing loop
PPTX
Maxillary arch form
PPTX
Front face analysis
set1.pdf
Set1.pptx
orthodontic patient education.pdf
New ton 3.pdf
ច្បាប់ញូតុនទី៣.pptx
Control tipping.pptx
Bulbous loop.pptx
brush teeth.pptx
bracket size.pptx
arch form KORI copy.pptx
Bracket size
Couple
ច្បាប់ញូតុនទី៣
Shoe horn loop
Opus loop
V bend
Closing loop
Maxillary arch form
Front face analysis

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Types and Its function , kingdom of life
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Institutional Correction lecture only . . .
PDF
Pre independence Education in Inndia.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Complications of Minimal Access Surgery at WLH
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Cell Types and Its function , kingdom of life
Sports Quiz easy sports quiz sports quiz
Institutional Correction lecture only . . .
Pre independence Education in Inndia.pdf
Cell Structure & Organelles in detailed.
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet

Interacting with Oracle Database

  • 1. Interacting with Oracle Database Server: SQL Statements in PL/SQL Programs
  • 2. SELECT Statements in PL/SQL • SELECT select_list INTO {variable_name[, variable_name]... | record_name} FROM table [WHERE condition]; • Use the SELECT statement to retrieve data from the database. – select_list: List of at least one column; can include SQL expressions, row functions, or group functions – variable_name: Scalar variable that holds the retrieved value – record_name:PL/SQL record that holds the retrieved values – Table: Specifies the database table name – Condition: • Ex1: DECLARE v_fname VARCHAR2(25); BEGIN SELECT custName INTO v_fname FROM tbCustomer WHERE custID=2; DBMS_OUTPUT.PUT_LINE(' cust name is : '||v_fname); END;
  • 3. SELECT Statements in PL/SQL • EX1: SELECT EMPID, EMPNAME,GENDER,ADDRESS,DOB,SSID FROM TBEMPLOYEE; • EX:SELECT DISTINCT ADDRESS FROM TBEMPLOYEE; • Ex2: DECLARE empName TBEMPLOYEE.EMPNAME%TYPE; empDob TBEMPLOYEE.DOB%TYPE; empSalary TBEMPLOYEE.SALARY%TYPE; BEGIN SELECT EMPNAME,dob, salary INTO empName,empDob, empSalary FROM TBEMPLOYEE WHERE EMPNAME = 'Chan Da' AND GENDER='Male' AND Address='Phnom Penh'; DBMS_OUTPUT.PUT_LINE ('Name is :'|| empName); DBMS_OUTPUT.PUT_LINE ('Salary is :'|| empSalary); DBMS_OUTPUT.PUT_LINE ('DOB is :'|| empDob); END;
  • 4. Manipulating data with PL/SQL • Make changes to database tables by using DML commands: • The INSERT statement adds new rows to the table. • The UPDATE statement modifies existing rows in the table. • The DELETE statement removes rows from the table. • The MERGE statement selects rows from one table to update or insert into another table. • EX1: BEGIN INSERT INTO employees (employee_id, first_name, last_name, email,hire_date, job_id, salary) VALUES(employees_seq.NEXTVAL, 'Ruth', 'Cores','RCORES',CURRENT_DATE, 'AD_ASST', 4000); END; EX2: BEGIN INSERT INTO TBEMPLOYEE(EMPID,EMPNAME,GENDER, ADDRESS,DOB,SSID,SALARY) VALUES(4, 'Ruth', 'FEMALE','PHNOM PENH','1-JAN-1979', '112233',123); END;
  • 5. Manipulating data with PL/SQL • EX2: Update DECLARE sal_increase employees.salary%TYPE := 800; BEGIN UPDATE employees SET salary = salary + sal_increase WHERE job_id = 'ST_CLERK'; END; • Ex3: DECLARE GENDER_V TBEMPLOYEE.GENDER%TYPE := 'FEMALE'; BEGIN UPDATE TBEMPLOYEE SET GENDER=GENDER_V WHERE EMPID=4; END; • EX3:Delete DECLARE deptno employees.department_id%TYPE := 10; BEGIN DELETE FROM employees WHERE department_id = deptno; END; EX4: DECLARE GENDER_VAR VARCHAR2(20) :='MALE'; BEGIN DELETE FROM TBEMPLOYEE WHERE GENDER=GENDER_VAR; END;
  • 6. Manipulating data with PL/SQL • Syntax: Merge BEGIN MERGE INTO copy_emp c USING employees e ON (e.employee_id = c.empno) WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, . . . WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, . . .,e.department_id); END;
  • 7. Manipulating data with PL/SQL • EX4: Merge BEGIN MERGE INTO copy_emp c USING employees e ON (e.employee_id = c.empno) WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, c.phone_number = e.phone_number, c.hire_date = e.hire_date, c.job_id = e.job_id, c.salary = e.salary, c.commission_pct = e.commission_pct, c.manager_id = e.manager_id, c.department_id = e.department_id WHEN NOT MATCHED THEN INSERT VALUES(e.employee_id, e.first_name, e.last_name, e.email, e.phone_number, e.hire_date, e.job_id, e.salary, e.commission_pct, e.manager_id, e.department_id); END;
  • 8. Manipulating data with PL/SQL • EX5: BEGIN MERGE INTO TBEMPLOYEEBACK c USING TBEMPLOYEE e ON (e.EMPID = c.EMPID) WHEN MATCHED THEN UPDATE SET c.EMPNAME = e.EMPNAME, c.GENDER = e.GENDER, c.ADDRESS = e.ADDRESS, c.DOB = e.DOB, c.SSID = e.SSID, c.SALARY= e.SALARY WHEN NOT MATCHED THEN INSERT VALUES(e.EMPID, e.EMPNAME, e.GENDER, e.ADDRESS, e.DOB, e.SSID, e.SALARY); END; EX6: MERGE INTO bonuses D USING (SELECT employee_id, salary, department_id FROM employees WHERE department_id = 80) S ON (D.employee_id = S.employee_id) WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01 DELETE WHERE (S.salary > 8000) WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus) VALUES (S.employee_id, S.salary*0.1) WHERE (S.salary <= 8000);
  • 9. Table Relationship • In SQL Developer, go to View > Data Modeler > Browser. In the Browser view right click on Relational Models and create a new one. This should create a new blank diagram. You can drag and drop tables from the Connection view into the diagram. • ALTER TABLE TBPRODUCTS ADD CONSTRAINT FK_PRODUCT_CATE_NUM FOREIGN KEY (CATEID) REFERENCES TBCATEGORY(CATEID); • ALTER TABLE TBSALE ADD CONSTRAINT FK_SALE_CUSTOMER_NUM FOREIGN KEY (CUSTID) REFERENCES TBCUSTOMER(CUSTID);
  • 10. Or using wizard by sql developer
  • 11. JOIN operations • The JOIN operations, which are among the possible TableExpressions in a FROM clause, perform joins between two tables. 1. INNER JOIN operation: Specifies a join between two tables with an explicit join clause. SYNTAX: TableExpression [ INNER ] JOIN TableExpression { ON booleanExpression | USING clause } EX1: SELECT * FROM TBPRODUCTS INNER JOIN TBCATEGORY ON TBPRODUCTS.CATEID = TBCATEGORY.CATEID; EX2: SELECT * FROM SAMP.EMPLOYEE INNER JOIN SAMP.STAFF ON EMPLOYEE.SALARY < STAFF.SALARY EX6: MERGE INTO bonuses D USING (SELECT employee_id, salary, department_id FROM employees WHERE department_id = 80) S ON (D.employee_id = S.employee_id) WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01 DELETE WHERE (S.salary > 8000) WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus) VALUES (S.employee_id, S.salary*0.1) WHERE (S.salary <= 8000);
  • 12. JOIN operations • Oracle JOINS are used to retrieve data from multiple tables. An Oracle JOIN is performed whenever two or more tables are joined in a SQL statement. • There are 4 different types of Oracle joins: – Oracle INNER JOIN (or sometimes called simple join) – Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN) – Oracle RIGHT OUTER JOIN (or sometimes called RIGHT JOIN) – Oracle FULL OUTER JOIN (or sometimes called FULL JOIN) • INNER JOIN (SIMPLE JOIN) • Oracle INNER JOINS return all rows from multiple tables where the join condition is met. Syntax – SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column; Ex1: SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id;
  • 13. JOIN operations • Old Syntax – older implicit syntax as follows (but we still recommend using the INNER JOIN keyword syntax): – SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id;
  • 14. JOIN operations • LEFT OUTER JOIN – This type of join returns all rows from the LEFT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met). • Syntax: Oracle LEFT OUTER JOIN is: • SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column = table2.column; • Ex1: – SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers LEFT OUTER JOIN orders ON suppliers.supplier_id = orders.supplier_id; • Old Syntax – LEFT OUTER JOIN example above could be rewritten using the older implicit syntax that utilizes the outer join operator (+) as follows (but we still recommend using the LEFT OUTER JOIN keyword syntax): SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers, orders WHERE suppliers.supplier_id = orders.supplier_id(+);
  • 15. JOIN operations • Right join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met). • Syntax • SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column = table2.column; EX: SELECT orders.order_id, orders.order_date, suppliers.supplier_name FROM suppliers RIGHT OUTER JOIN orders ON suppliers.supplier_id = orders.supplier_id; Old Syntax:utilizes the outer join operator (+) as follows (but we still recommend using the RIGHT OUTER JOIN keyword syntax): SELECT orders.order_id, orders.order_date, suppliers.supplier_name FROM suppliers, orders WHERE suppliers.supplier_id(+) = orders.supplier_id;
  • 16. JOIN operations • FULL OUTER JOIN • returns all rows from the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is not met. • Syntax • SELECT columns FROM table1 FULL [OUTER] JOIN table2 ON table1.column = table2.column; Ex: SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers FULL OUTER JOIN orders ON suppliers.supplier_id = orders.supplier_id;
  • 17. Introducing SQL cursors • EX4: Merge BEGIN MERGE INTO copy_emp c USING employees e ON (e.employee_id = c.empno) WHEN MATCHED THEN UPDATE SET c.first_name = e.first_name, c.last_name = e.last_name, c.email = e.email, c.phone_number = e.phone_number, c.hire_date = e.hire_date, c.job_id = e.job_id, c.salary = e.salary, c.commission_pct = e.commission_pct, c.manager_id = e.manager_id, c.department_id = e.department_id

Editor's Notes

  • #4: DECLARE hire_date employees.hire_date%TYPE; sysdate hire_date%TYPE; employee_id employees.employee_id%TYPE := 176; BEGIN SELECT hire_date, sysdate INTO hire_date, sysdate FROM employees WHERE employee_id = employee_id; END;
  • #14: EX1: SELECT TBCUSTOMER.CUSTID,TBCUSTOMER.CustName,TBCUSTOMER.CustName, TBCUSTOMER.GENDER, TBSALE.INVOICEDATE FROM TBCUSTOMER INNER JOIN TBSALE ON TBCUSTOMER.CUSTID=TBSALE.CUSTID; EX2: SELECT TBCUSTOMER.CUSTID,TBCUSTOMER.CustName, TBCUSTOMER.GENDER, TBSALE.INVOICEDATE, TBEMPLOYEE.EMPNAME, TBEMPLOYEE.GENDER FROM TBCUSTOMER INNER JOIN(TBSALE INNER JOIN TBEMPLOYEE ON TBSALE.EMPID=TBEMPLOYEE.EMPID)ON TBCUSTOMER.CUSTID=TBSALE.CUSTID;