oracle dml
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 PresentationTRANSCRIPT
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
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)
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;
RETRIEVING DATA FROM A TABLE
Example:
SELECT Last, FirstFROM student;
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.
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)];
RETRIEVING DATA FROM A TABLE
SELECT *FROM deptWHERE Location = ‘Monroe’;
SELECT Lname, Fname, Salary, DeptidFROM employeeWHERE Salary >= 50000;
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;
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
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]];
RETRIEVING DATA FROM A TABLE
SELECT Last, FirstFROM studentORDER BY Last;
SELECT Last, FirstFROM studentORDER BY Last DESC;
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.
Group Functions
SELECT SUM(Salary), AVG(Salary),
MAX(Salary), MIN(Salary)FROM EMPLOYEE;
Grouping Data The rows in a table can be divided into
different groups to treat each group separately.
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]];
Grouping Data
SELECT DeptID, COUNT(*)FROM employeeGROUP BY DeptID
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
Cartesian Product
SELECT Last, First, Name FROM student, faculty;
Equijoin The equijoin is a join with a join
condition involving common columns from two tables.
Join Syntax:SELECT columnnamesFROM tablenamesWHERE join condition(s);
Equijoin
SELECT student.Last, faculty.Name
FROM student, faculty WHERE student.FacultyID = faculty.FacultyID
Table Aliases
SELECT s.Last, f.Name FROM student s, faculty f WHERE student.FacultyID = faculty.FacultyID
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
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
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;
Set Theory
Union Intersect Minus
Set theory Syntax
Generally, the syntax would looks like:
QuerySet OperationQuery
Set theory Syntax
Example:
Select last from studentUNIONSelect last from faculty
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)
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
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
Intersect
Select ssn from employee where dno=5
INTERSECTSelect superssn from employee
where dno=5
Minus
Select ssn from employee where dno=5
MINUSSelect superssn from employee
where dno=5
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
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
Convert
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
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)
Group Functions
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]];
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
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.
Join
Set Theory Union Intersect Minus
Generally, the syntax would looks like:QuerySet OperationQuery
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.
Natural join example
SELECT *FROM employee NATURAL JOIN department;
Type in the code and try to analysis the output.
Natural join example
SELECT *FROM department NATURAL JOIN Dept_locations;
Type in the code and try to analysis the output.
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
Division
Assume we have 2 suppliers and 2 products, the product and supply table looks as following
Division
We know that supplier #2 supplies all products, and it is our answer to find
First of all, find out who did not supply all
Then, use all to minus that list
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
RenameCREATE TABLE analysis ASSELECT dno, AVG(salary) average_salary, MIN(salary) min_salary, MAX(salary) max_salaryFROM employeeGROUP BY dno;
(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);