1ST and 2ND Quarter Prog F2F Final Version

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 56

Programming (Oracle

Database) Parts 1, 2 and 3


CONTENT
• Database
• Parts of a Database
• Oracle
• Relational Database
• Database Management System
• Structured Query Language
• Data Types
• Primary Key and Foreign Key
• SQL Statements
• SQL Operations and Clauses
• Aggregate Functions
Database
• A database is a collection of information that is organized so that
it can be easily accessed, managed and updated.
• Simply, a database is an organized collection of data.
• Data is organized into rows, column and tables and it is indexed
to make it easier to find relevant information. Data gets updated,
expanded and deleted as new information is added.
• Databases process workloads to create and update themselves,
querying the data they contain and running applications against
it.
Parts of a Database
• A table is a collection of related data held in structured format with a
database. It consists of columns and rows.
• A column is a set of data values of a particular simple type one for
each row of the table. A column is also known as a field or attribute.
• A row is also called as a record that represents a singe, implicitly
structured data item in a table.
• In simple terms, a database table can be thought of as consisting of
rows and columns of fields. Each row in a table represents a set of
related data and every row in the table has the same structure.
Parts of a Database

Column 1 (Fields) Column 2


(Fields)

(Record) Row 1 Row 1, Column 1 Row 1, Column 2

(Record) Row 2 Row 2, Column 1 Row 2, Column 2

(Record) Row 3 Row 3, Column 1 Row 3, Column 2


Oracle
• Oracle is one of the largest vendors in the enterprise. IT market
offering a wide variety of software and hardware technologies
for use in Corporate applications. Oracle is a corporation. It
makes software to create and manage databases.
• Oracle in 1979 was the first company to commercialize a
relational software, now called Oracle Database, remains its
flagship product. But in the ensuring decades, Oracle greatly
expanded its product portfolio through a combination of internal
development and numerous acquisitions. It now also sells
several other databases, multiple lines of business applications,
middleware, computer systems, data storage equipment,
development tools and more.
Relational Database

• A Relational Database is a database in which the data is


organized according to type with the relationships being
maintained between the differing types.
• A Relational Database is a collection of data items organized as a
set of formally described tables from which data can be accessed
or reassembled in many different ways without having to
reorganize the database tables. The relational database was
invented by E.F. Codd at IBM in 1970.
Database Management System

• A database management system (DBMS) is a system software


for creating and managing databases. The DBMS provides users
and programmers with a systematic way to create, retrieve,
update and manage data.
• A relational database management system (RDBMS) is a
program that lets you create, update and administer a relational
database. Most commercial RDBMS’s use the Structured Query
Language (SQL) to access the database although SQL was
invented after the development of the relational model and is not
necessary for its use.
Structured Query Language

• SQL (Structured Query Language) is a standardized


programming language used for managing relational databases
and performing various operations on the data in them. Initially
created in the 1970s, SQL is regularly used by database
administrators, as well as by developers writing data integration
scripts and data analysts looking to set up and run analytical
queries.
Data Types
3 Main Data Types
• 1. Number (Integer) – If a column is created as a number column,
only numbers can be stored in it.
• 2. VarChar2 – VARiable CHARacter
• 3. Date
Primary Key and Foreign Key

• Primary Key – combination of columns that uniquely identifies a


row.
• Foreign Key – combination of columns that uniquely identifies a
row in another table.
SQL Statements
DDL – Data Definition Language
Commands:
• CREATE
• ALTER
• DROP
DML – Data Manipulation Language
Commands:
• INSERT
• UPDATE
• DELETE
SQL Statements

DCL – Data Control Language


Commands:
• GRANT
• REVOKE
DQL – Data Query Language
Commands:
• SELECT
Data Definition Language
• CREATE
CREATE DATABASE <DATABASE NAME>
CREATE TABLE <TABLE NAME>

EXAMPLE:
CREATE DATABASE PEARL
CREATE TABLE SECTION (ID INTEGER PRIMARY KEY, FIRST
VARCHAR(10), LAST VARCHAR (10))
Data Definition Language
• ALTER
ALTER TABLE <TABLE NAME>
ADD ATTR DATATYPE
ALTER TABLE <TABLE NAME>
DROP COLUMN ATTR

EXAMPLE:
ALTER TABLE FOODCART ADD SOLD INT
ALTER TABLE FOODCART DROP COLUMN PROFIT
Data Definition Language
• DROP
Has 2 Options:
• CASCADE: Specifies that any foreign key constraint violations
that are caused by dropping the table will cause the
corresponding rows of the related table to be deleted.
• RESTRICT: blocks the deletion of the table of any foreign key
constraint violations would be created.

EXAMPLE:
DROP TABLE <table name> [ RESTRICT|CASCADE]
Data Manipulation Language
• INSERT
-adds new rows to a table
INSERT INTO <TABLE NAME>
VALUES ('value1', 'value2', NULL)

EXAMPLE:
INSERT INTO FOODCART VALUES (’02/26/08', ‘pizza', 70)
Data Manipulation Language
• UPDATE
- modifies one or more attributes
UPDATE <TABLE NAME> SET <ATTR> = <VALUE>
WHERE <SELECTION CONDITION>

EXAMPLE:
UPDATE FOODCART SET SOLD = 349 WHERE DATE = ’02/25/08’
AND FOOD = ‘PIZZA’
Data Manipulation Language

• DELETE
- deletes one or more rows from a table
DELETE FROM <TABLE NAME>
WHERE <CONDITION>

EXAMPLE:
DELETE * FROM FOODCART WHERE FOOD = ‘HOTDOG’
SQL Statements, Operations and Clauses
SQL Statements:
• Select
SQL Operations:
• Join
• Left Join
• Right Join
• Like
SQL Clauses:
• Order By
• Group By
• Having
SQL Statements
• SELECT
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT * FROM … WHERE
SELECT DISTINCT * FROM … WHERE

EXAMPLE:
SELECT * FROM person WHERE age > 30
SELECT weight FROM person WHERE age > 30
SELECT distinct weight FROM person WHERE age > 30
SQL Operations
• JOIN
- A join can be specified in the FROM clause which list the two input
relations and the WHERE clause which lists the join condition
SQL Operations
• Inner Join = join
EXAMPLE:
SELECT * FROM emp join dept (or FROM emp, dept) on emp.id =
dept.id
• Left Outer Join = left join
EXAMPLE:
SELECT * FROM emp left join dept on emp.id = dept.id
• Right Outer Join = right join
EXAMPLE:
SELECT * FROM emp right join dept on emp.id = dept.id
SQL Operations
• LIKE

% (arbitrary string)
SELECT * FROM emp WHERE ID like ‘%01’;
finds ID that ends with 01, e.g. 1001, 2001, etc.

_ (a single character)
SELECT * FROM emp WHERE ID like ‘_01_’;
finds ID that has the second and third character as 01, e.g. 1010,
1011, 1012, 1013, etc.
SQL Clauses
• ORDER BY
desc (descending order)
SELECT * FROM emp order by state desc
puts state in descending order, e.g. TN, MA, CA

asc (ascending order)


SELECT * FROM emp order by id asc
puts ID in ascending order, e.g. 1001, 1002, 1003
SQL Clauses
• GROUP BY

EXAMPLE:
SELECT food, sum(sold) as totalSold FROM FoodCart group by
food
SQL Clauses

• HAVING

EXAMPLE:
SELECT food, sum(sold) as totalSold FROM FoodCart group by food having
sum(sold) > 450
Aggregate Functions
• AGGREGATE FUNCTIONS
-Are used to provide summarization information for SQL
statements, which return a single value.
• COUNT(attr)
• SUM(attr)
• MAX(attr)
• MIN(attr)
• AVG(attr)
Aggregate Functions
• COUNT(attr) -> return # of rows that are not null
Ex: COUNT (distinct food) from FoodCart; -> 2
• SUM(attr) -> return the sum of values in the attr
Ex: SUM(sold) from FoodCart; -> 919
• MAX(attr) -> return the highest value from the attr
Ex: MAX(sold) from FoodCart; -> 500
• MIN(attr) -> return the lowest value from the attr
Ex: MIN(sold) from FoodCart; -> 70
• AVG(attr) -> return the average value from the attr
Ex: AVG(sold) from FoodCart; -> 306.33
• Note: value is rounded to the precision of the datatype
CONTENT
• UNION Operator
• IN Operator
• AND Operator
• OR Operator
• NOT Operator
• NULL Values
• NULL Operator
• Arithmetic Operators
• Subquery
The SQL UNION Operator

The UNION operator is used to combine the result-set of two or


more SELECT statements.

• Each SELECT statement within UNION must have the same


number of columns
• The columns must also have similar data types
• The columns in each SELECT statement must also be in the same
order
The SQL UNION Operator

• UNION Syntax:
SELECT column_name(s) FROM table1 UNION SELECT
column_name(s) FROM table2;
• UNION ALL Syntax:
The UNION operator selects only distinct values by default. To
allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1 UNION ALL SELECT
column_name(s) FROM table2;
The SQL UNION Operator
• UNION
EXAMPLE:
SELECT City FROM Customers UNION SELECT City FROM Suppliers
ORDER BY City;

• UNION ALL
EXAMPLE:
SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers
ORDER BY City;
THE SQL IN Operator

The IN operator allows you to specify multiple values in a WHERE


clause.

The IN operator is a shorthand for multiple OR conditions.

IN Syntax:
SELECT column_name(s) FROM table_name WHERE
column_name IN (value1, value2, ...);
THE SQL IN Operator
• IN
EXAMPLE:
SELECT * FROM Customers WHERE Country IN ('Germany', 'France',
'UK’);

• NOT IN
EXAMPLE:
SELECT * FROM Customers WHERE Country NOT IN ('Germany',
'France', 'UK');
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT
operators.

The AND and OR operators are used to filter records based on


more than one condition:
• The AND operator displays a record if all the conditions
separated by AND is TRUE.
• The OR operator displays a record if any of the conditions
separated by OR is TRUE.
• The NOT operator displays a record if the condition(s) is NOT
TRUE.
The SQL AND, OR and NOT Operators
• AND Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

• OR Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

• NOT Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
The SQL AND, OR and NOT Operators
• AND
EXAMPLE:
SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin’;

• OR
EXAMPLE:
SELECT * FROM Customers WHERE City='Berlin' OR City='München’;

• NOT
EXAMPLE:
SELECT * FROM Customers WHERE NOT Country='Germany';
Combining AND, OR and NOT
EXAMPLE:
SELECT * FROM Customers WHERE Country='Germany' AND
(City='Berlin' OR City='München’);

EXAMPLE:
SELECT * FROM Customers WHERE NOT Country='Germany' AND
NOT Country='USA';
NULL Value
What is a NULL Value?
• A field with a NULL value is a field with no value.
• If a field in a table is optional, it is possible to insert a new record or update a
record without adding a value to this field. Then, the field will be saved with
a NULL value.
• Note: It is very important to understand that a NULL value is different from
a zero value or a field that contains spaces. A field with a NULL value is one
that has been left blank during record creation!

How to Test for NULL Values?


• It is not possible to test for NULL values with comparison operators, such as
=, <, or <>.
• We will have to use the IS NULL and IS NOT NULL operators instead.
NULL Operators
• IS NULL Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

• IS NOT NULL Syntax:


SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
NULL Operators

• IS NULL
EXAMPLE:
SELECT LastName, FirstName, Address FROM Persons WHERE
Address IS NULL;

• IS NOT NULL
EXAMPLE:
SELECT LastName, FirstName, Address FROM Persons WHERE
Address IS NOT NULL;
Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division

• Addition
EXAMPLE:
SELECT ID, Lastname, Firstname, Salary + 1000 AS ADDED_SALARY FROM
EMPLOYEES

• Subtraction
EXAMPLE:
SELECT Lastname, Branch, Salary - 5000 AS LOWER_SALARY, Commission - 500 AS
LOWER_COMMISSION FROM EMPLOYEES
Arithmetic Operators
• Multiplication
EXAMPLE:
SELECT ID, Firstname, Salary * 12 AS ANNUAL_SALARY FROM
EMPLOYEES

• Division
EXAMPLE:
SELECT ID, Branch, Salary / 5 AS DIVIDED_SALARY, Commission /
5 AS DIVIDED_COMMISSION FROM EMPLOYEES
Subquery Syntax

• SELECT select_list
FROM table_name
WHERE operator
(SELECT select_list FROM table_name)
• The subquery (inner query) executes before the main query
(outer query)
• The result of the subquery is used by the main query
Table
ID Lastname Salary

1 Lim 13000

2 Tan 9000

3 Santos 10000

4 Sy 21000

5 Chu 11000
Subquery Examples

EX:
1. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary > (SELECT
Salary FROM EMPLOYEES WHERE Lastname = ‘Chu’)
OUTPUT:
Lastname Salary

Lim 13000

Sy 21000
Comparison Operators
Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to


Subquery Examples

2. SELECT Lastname, Salary FROM EMPLOYEES WHERE Lastname = (SELECT


Lastname FROM EMPLOYEES WHERE ID = 3)
AND Salary >
(SELECT Salary FROM EMPLOYEES WHERE Lastname = ‘Tan’)
OUTPUT:
Lastname Salary
Santos 10000
Subquery Examples
3. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary = (SELECT
MIN(Salary) FROM EMPLOYEES)
OUTPUT:
Lastname Salary

Tan 9000
Subquery Examples

4. SELECT ID, Lastname, MIN(Salary) FROM EMPLOYEES GROUP BY ID


Having MIN(Salary) > (SELECT MIN(Salary) FROM EMPLOYEES WHERE ID =
5)
OUTPUT:

ID Lastname MIN(Salary)

1 Lim 13000

4 Sy 21000
Multiple-Row Subqueries
Operator Meaning

IN Equal to any member of the list

ANY Returns true if at least one element exist is true

ALL Returns true if the relation for all elements are true
Subquery Examples
5. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary < Any
(SELECT Salary FROM EMPLOYEES WHERE ID = 1 OR ID = 4)
OUTPUT:
Lastname Salary

Lim 13000

Tan 9000

Santos 10000

Chu 11000
Subquery Examples
6. SELECT Lastname, Salary FROM EMPLOYEES WHERE Salary < ALL
(SELECT Salary FROM EMPLOYEES WHERE ID = 1 OR ID = 3)
OUTPUT:
Lastname Salary

Tan 9000
CONTENT

• EXISTS Operator
EXISTS Operator
• EXISTS Operator displays the records that exist in the table.
EXAMPLE:
SELECT Lastname, Salary FROM EMPLOYEES WHERE EXISTS (SELECT
Salary FROM EMPLOYEES WHERE Lastname = 'Chu’)
OUTPUT:
Lastname Salary
Lim 13000
Tan 9000
Santos 10000
Sy 21000
Chu 11000

You might also like