oracle dml

54
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas

Upload: tamika

Post on 30-Jan-2016

54 views

Category:

Documents


3 download

DESCRIPTION

Oracle DML. Dr. Bernard Chen Ph.D. University of Central Arkansas. SQL code execution. To start: SQL> CONNECT system (and then type in password) Then create a file: SQL> edit test (test is file name) remember to store the file in *.sql - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Oracle DML

Oracle DML

Dr. Bernard Chen Ph.D.University of Central Arkansas

Page 2: Oracle DML

SQL code execution To start:

SQL> CONNECT system(and then type in password)

Then create a file:SQL> edit test (test is file name)remember to store the file in *.sql

C:\oraclexe\app\oracle\product\11.2.0\server\bin

Once you done coding, execute the code:SQL> @c:\test.sql (test is file name)

Page 3: Oracle DML

RETRIEVING DATA FROM A TABLE The main purpose of the SQL language

is for querying the database

The most important statement or query is the SELECT query

 The general syntax isSELECT columnlistFROM tablename;

Page 4: Oracle DML

RETRIEVING DATA FROM A TABLE

Example:

SELECT Last, FirstFROM student;

Page 5: Oracle DML

RETRIEVING DATA FROM A TABLESELECT (*)

If you want to see all columns in a table, you do not have to list them all. 

You can use character asterisk (*) in place of the column list, and all columns will be displayed in the same order as the underlying table structure. 

Page 6: Oracle DML

RETRIEVING DATA FROM A TABLERESTRICTING DATA WITH A WHERE CLAUSE A WHERE clause is used with the SELECT

query to restrict rows picked

The general syntax of the WHERE clause is

SELECT columnlistFROM tablename[WHERE condition(s)];

Page 7: Oracle DML

RETRIEVING DATA FROM A TABLE

SELECT *FROM deptWHERE Location = ‘Monroe’;

SELECT Lname, Fname, Salary, DeptidFROM employeeWHERE Salary >= 50000;

Page 8: Oracle DML

RETRIEVING DATA FROM A TABLE

SELECT Lname, Fname, Salary, DnoFROM employeeWHERE Salary <= 50000 AND Salary

>=25000;

SELECT Lname, Fname, Salary, DnoFROM employeeWHERE Salary BETWEEN 25000 AND 50000;

Page 9: Oracle DML

RETRIEVING DATA FROM A TABLE

SORTING The order of rows in a table is arbitrary.

You may want to see rows in a specific order based on a column or columns

For example, you may want to see employees in alphabetical order by their name

Page 10: Oracle DML

RETRIEVING DATA FROM A TABLE The ORDER BY clause is used with the SELECT query

to sort rows in a table. 

The general syntax isSELECT columnlistFROM tablename[WHERE condition(s)][ORDER BY column|expression [ASC|

DESC]];

Page 11: Oracle DML

RETRIEVING DATA FROM A TABLE

SELECT Last, FirstFROM studentORDER BY Last;

SELECT Last, FirstFROM studentORDER BY Last DESC;

Page 12: Oracle DML

Group Functions The group functions perform an operation on a group of

rows and return one result.

Sum ()Finds sum of all values in a column, ignores null values.

Avg ()Finds average of all values in a column, ignores null values.

Max ()Finds maximum value and ignores null values.

Min ()Finds minimum value and ignores null values.

Count(), Count(*)Counts number of rows including nulls for *. Counts non-null values if

column or expression is used as argument.

Page 13: Oracle DML

Group Functions

SELECT SUM(Salary), AVG(Salary),

MAX(Salary), MIN(Salary)FROM EMPLOYEE;

Page 14: Oracle DML

Grouping Data The rows in a table can be divided into

different groups to treat each group separately.

Page 15: Oracle DML

Grouping Data The GROUP BY clause is used for

grouping data. The general syntax is

SELECT column, groupfunction (column)

FROM tablename[WHERE condition(s)][GROUP BY column|expression][ORDER BY column|expression [ASC|DESC]];

Page 16: Oracle DML

Grouping Data

SELECT DeptID, COUNT(*)FROM employeeGROUP BY DeptID

Page 17: Oracle DML

Joins

When the required data is in more than one table, related tables are joined using a join condition.

In most cases, the common columns are the primary key in one table and a foreign key in another

Page 18: Oracle DML

Cartesian Product

SELECT Last, First, Name FROM student, faculty;

Page 19: Oracle DML

Equijoin The equijoin is a join with a join

condition involving common columns from two tables.

Join Syntax:SELECT columnnamesFROM tablenamesWHERE join condition(s);

Page 20: Oracle DML

Equijoin

SELECT student.Last, faculty.Name

FROM student, faculty WHERE student.FacultyID = faculty.FacultyID

Page 21: Oracle DML

Table Aliases

SELECT s.Last, f.Name FROM student s, faculty f WHERE student.FacultyID = faculty.FacultyID

Page 22: Oracle DML

Multiple Joins SELECT e.Lname,

d.DeptName, q.QualDesc

FROM EMPLOYEE e, Department d, QUALIFICATION q

WHERE e.DeptID = d.DeptID AND e.QualID = q.QualID

Page 23: Oracle DML

Creating a table using a subquery You can create a table by using a

nested SELECT query. The Query will create a new table and

populated it with the rows selected from the other table.

CREATE TABLE tablenameASSELECT query

Page 24: Oracle DML

Creating a table using a subquery CREATE TABLE temp AS SELECT Employee ID, Lname, Fname,

Salary FROM employee WHERE DeptID=20;

DESCRIBE TABLE SELECT * FROM temp;

Page 25: Oracle DML

Set Theory

Union Intersect Minus

Page 26: Oracle DML

Set theory Syntax

Generally, the syntax would looks like:

QuerySet OperationQuery

Page 27: Oracle DML

Set theory Syntax

Example:

Select last from studentUNIONSelect last from faculty

Page 28: Oracle DML

UNION

Example: To retrieve the social security numbers

of all employees who either work in department 5 (RESULT1 below) or directly supervise an employee who works in department 5 (RESULT2 below)

Page 29: Oracle DML

UNION

DEP5_EMPS DNO=5 (EMPLOYEE)

RESULT1 SSN(DEP5_EMPS)

RESULT2 SUPERSSN(DEP5_EMPS)RESULT RESULT1 RESULT2

The union operation produces the tuples that are in either RESULT1 or RESULT2 or both

Page 30: Oracle DML

UNION

DEP5_EMPS DNO=5 (EMPLOYEE)

RESULT1 SSN(DEP5_EMPS)

RESULT2 SUPERSSN(DEP5_EMPS)RESULT RESULT1 RESULT2

Select ssn from employee where dno=5UNIONSelect superssn from employee where

dno=5

Page 31: Oracle DML

Intersect

Select ssn from employee where dno=5

INTERSECTSelect superssn from employee

where dno=5

Page 32: Oracle DML

Minus

Select ssn from employee where dno=5

MINUSSelect superssn from employee

where dno=5

Page 33: Oracle DML

Relational Algebra Overview Relational Algebra consists of several groups of

operations Unary Relational Operations

SELECT (symbol: (sigma)) PROJECT (symbol: (pi)) RENAME (symbol: (rho))

Relational Algebra Operations From Set Theory UNION ( ), INTERSECTION ( ), DIFFERENCE (or MINUS, – ) CARTESIAN PRODUCT ( x )

Binary Relational Operations JOIN (several variations of JOIN exist) DIVISION

Additional Relational Operations OUTER JOINS, OUTER UNION AGGREGATE FUNCTIONS

Page 34: Oracle DML

Relational Algebra Overview Relational Algebra consists of several groups of

operations Unary Relational Operations

SELECT (symbol: (sigma)) PROJECT (symbol: (pi)) RENAME (symbol: (rho))

Relational Algebra Operations From Set Theory UNION ( ), INTERSECTION ( ), DIFFERENCE (or MINUS, – ) CARTESIAN PRODUCT ( x )

Binary Relational Operations JOIN (several variations of JOIN exist) DIVISION

Additional Relational Operations OUTER JOINS, OUTER UNION AGGREGATE FUNCTIONS

Page 35: Oracle DML

Convert

Page 36: Oracle DML

Aggregate Functions and Grouping Use of the Aggregate Functional operation Ʒ

Ʒ MAX Salary (EMPLOYEE) retrieves the maximum salary value from the EMPLOYEE relation

Ʒ MIN Salary (EMPLOYEE) retrieves the minimum Salary value from the EMPLOYEE relation

Ʒ SUM Salary (EMPLOYEE) retrieves the sum of the Salary from the EMPLOYEE relation

Ʒ COUNT SSN, AVERAGE Salary (EMPLOYEE) computes the count (number) of employees and their average salary

Page 37: Oracle DML

Aggregate Functions and Grouping Grouping can be combined with Aggregate

Functions

Example: For each department, retrieve the DNO, COUNT SSN, and AVERAGE SALARY

A variation of aggregate operation Ʒ allows this:

Grouping attribute placed to left of symbol Aggregate functions to right of symbol

DNO Ʒ COUNT SSN, AVERAGE Salary (EMPLOYEE)

Page 38: Oracle DML

Group Functions

Page 39: Oracle DML

Grouping Data The GROUP BY clause is used for

grouping data. The general syntax is

SELECT column, groupfunction (column)

FROM tablename[WHERE condition(s)][GROUP BY column|expression][ORDER BY column|expression [ASC|DESC]];

Page 40: Oracle DML

JOIN JOIN Operation (denoted by )

The sequence of CARTESIAN PRODECT followed by SELECT is used quite commonly to identify and select related tuples from two relations

This operation is very important for any relational database with more than a single relation, because it allows us combine related tuples from various relations

Page 41: Oracle DML

JOIN The general form of a join operation

on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:

R <join condition>S

where R and S can be any relations that result from general relational algebra expressions.

Page 42: Oracle DML

Join

Page 43: Oracle DML

Set Theory Union Intersect Minus

Generally, the syntax would looks like:QuerySet OperationQuery

Page 44: Oracle DML

Natural Join *

In oracle, it do have natural join

A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables.

Page 45: Oracle DML

Natural join example

SELECT *FROM employee NATURAL JOIN department;

Type in the code and try to analysis the output.

Page 46: Oracle DML

Natural join example

SELECT *FROM department NATURAL JOIN Dept_locations;

Type in the code and try to analysis the output.

Page 47: Oracle DML

Division Unfortunately, there is no division operator in

Oracle

In order to do division, some complicated processes are required

Let us study an example:If we try to find the supplier that supplies all the

parts

Page 48: Oracle DML

Division

Assume we have 2 suppliers and 2 products, the product and supply table looks as following

Page 49: Oracle DML

Division

We know that supplier #2 supplies all products, and it is our answer to find

Page 50: Oracle DML

First of all, find out who did not supply all

Page 51: Oracle DML

Then, use all to minus that list

Page 52: Oracle DML

Rename

“Create Table table_name as” is actually the rename function in Oracle

table_name is the rename for table

You can also rename the attribute name by adding “new names” after each select item

Page 53: Oracle DML

RenameCREATE TABLE analysis ASSELECT dno, AVG(salary) average_salary, MIN(salary) min_salary, MAX(salary) max_salaryFROM employeeGROUP BY dno;

Page 54: Oracle DML

(Not) Exist

If a subquery returns any rows at all, EXISTS subquery is TRUE, and NOT EXISTS subquery is FALSE.

Syntax:SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);