Oracle 1 Important
Oracle 1 Important
This section helps you learn how to query data from the Oracle Database. We will start
with a simple query that allows you to retrieve data from a single table.
Sorting data
ORDER BY – sort the result set of a query in ascending or descending order.
Filtering data
DISTINCT – introduce you how to eliminate duplicate rows from the output of a
query.
WHERE – learn how to specify a condition for rows in the result set returned by a
query.
AND – combine two or more Boolean expressions and return true if all
expressions are true.
OR– combine two or more Boolean expressions and return true if one of the
expressions is true.
FETCH – show you how to limit rows returned by a query using the row limiting
clause.
https://www.oracletutorial.com/oracle-basics/
IS NULL and IS NOT NULL – check if an expression or values in a column is
NULL or not.
Joining tables
INNER JOIN – show you how to query rows from a table that have matching rows
from another table.
LEFT JOIN – introduce you to the left-join concept and learn how to use it to
select rows from the left table that have or don’t have the matching rows in the right
table.
RIGHT JOIN – explain the right-join concept and show you how to apply it to
query rows from the right table that have or don’t have the matching rows in the left
table.
FULL OUTER JOIN – describe how to use the full outer join or full join to query
data from two tables.
CROSS JOIN – cover how to make a Cartesian product from multiple tables.
Self-join – show you how to join a table to itself to query hierarchical data or
compare rows within the same table.
Grouping data
GROUP BY– teach you how to group rows into subgroups and apply an
aggregate function for each group
https://www.oracletutorial.com/oracle-basics/
Subquery
Subquery – introduce the concept of subquery and how to use the subqueries to
perform advanced data selection techniques.
Set Operators
This section walks you the steps of using the set operators to combine result sets of two
or more independent queries.
UNION – show you how to combine the results of two queries into a single result.
More on Groupings
Grouping sets – introduce you to the grouping set concepts and show you how to
generate multiple grouping sets in a query.
https://www.oracletutorial.com/oracle-basics/
CUBE – learn how to use CUBE to generate subtotals for all possible
combinations of a specified group of dimensions.
PIVOT – show you how to transpose rows to columns to make the crosstab
reports.
Modifying data
In this section, you’ll learn how to change the contents of an Oracle database. The SQL
commands for modifying data are referred to as Data Manipulation Language (DML).
INSERT INTO SELECT – insert data into a table from the result of a query.
INSERT ALL – discuss multitable insert statement to insert multiple rows into a
table or multiple tables.
DELETE – show you how to delete one or more row from a table.
MERGE – walk you through the steps of performing a mixture of insertion, update,
and deletion using a single statement.
Data definition
This section shows you how to manage the most important database objects including
databases and tables.
https://www.oracletutorial.com/oracle-basics/
CREATE TABLE – walk you through the steps of creating new tables in the
database.
Identity Column – learn how to use the identity clause to define the identity
column for a table.
ALTER TABLE – teach you how to change the structure of existing tables.
ALTER TABLE ADD column – show you how to add one or more columns to an
existing table
ALTER TABLE MODIFY column – show you how to change the definition of
existing columns in a table.
Drop columns – learn how to use various statements to drop one or more
columns from a table.
DROP TABLE – show you how to delete tables from the database.
TRUNCATE TABLE – delete all data from a table faster and more efficiently.
RENAME table – walk you through the process of renaming a table and handling
its dependent objects.
Virtual columns – introduce you to virtual columns and how to use them in the
database tables.
Oracle data types – give you an overview of the built-in Oracle data types.
NUMBER – introduces you to the numeric data type and show you how to use it
to define numeric columns for a table.
NCHAR – show you how to store fixed-length Unicode character data and
explain the differences between CHAR and NCHAR data types
https://www.oracletutorial.com/oracle-basics/
VARCHAR2 – introduce you to the variable-length character and show you how to
define variable-length character columns in a table.
DATE – discuss the date and time data type and show you how to handle date-
time data effectively.
TIMESTAMP – introduce you how to store date and time with the fractional
seconds precision.
TIMESTAMP WITH TIME ZONE – learn how to store datetime with timezone
data.
Constraints
Primary key – explain you to the primary key concept and show you how to use
the primary key constraint to manage a primary key of a table.
Foreign key – introduce you to the foreign key concept and show you use the
foreign key constraint to enforce the relationship between tables.
NOT NULL constraint – show you how to ensure a column not to accept null
values.
CHECK constraint – walk you through the process of adding logic for checking
data before storing them in tables.
https://www.oracletutorial.com/oracle-basics/
Oracle sample database diagram
https://www.oracletutorial.com/oracle-basics/
Table Names Description Records
After downloading the file, you should extract it. The zip file contains the
following *.sql files:
https://www.oracletutorial.com/oracle-basics/
);
-- location
CREATE TABLE locations
(
location_id NUMBER GENERATED BY DEFAULT AS IDENTITY START WITH 24
PRIMARY KEY ,
address VARCHAR2( 255 ) NOT NULL,
postal_code VARCHAR2( 20 ) ,
city VARCHAR2( 50 ) ,
state VARCHAR2( 50 ) ,
country_id CHAR( 2 ) , -- fk
CONSTRAINT fk_locations_countries
FOREIGN KEY( country_id )
REFERENCES countries( country_id )
ON DELETE CASCADE
);
-- warehouses
CREATE TABLE warehouses
(
warehouse_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 10
PRIMARY KEY,
warehouse_name VARCHAR( 255 ) ,
location_id NUMBER( 12, 0 ), -- fk
CONSTRAINT fk_warehouses_locations
FOREIGN KEY( location_id )
REFERENCES locations( location_id )
ON DELETE CASCADE
);
-- employees
CREATE TABLE employees
(
employee_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 108
PRIMARY KEY,
first_name VARCHAR( 255 ) NOT NULL,
last_name VARCHAR( 255 ) NOT NULL,
email VARCHAR( 255 ) NOT NULL,
phone VARCHAR( 50 ) NOT NULL ,
hire_date DATE NOT NULL ,
manager_id NUMBER( 12, 0 ) , -- fk
job_title VARCHAR( 255 ) NOT NULL,
CONSTRAINT fk_employees_manager
FOREIGN KEY( manager_id )
REFERENCES employees( employee_id )
ON DELETE CASCADE
);
-- product category
CREATE TABLE product_categories
(
category_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 6
PRIMARY KEY,
category_name VARCHAR2( 255 ) NOT NULL
);
-- products table
CREATE TABLE products
(
product_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 289
PRIMARY KEY,
product_name VARCHAR2( 255 ) NOT NULL,
description VARCHAR2( 2000 ) ,
standard_cost NUMBER( 9, 2 ) ,
https://www.oracletutorial.com/oracle-basics/
list_price NUMBER( 9, 2 ) ,
category_id NUMBER NOT NULL ,
CONSTRAINT fk_products_categories
FOREIGN KEY( category_id )
REFERENCES product_categories( category_id )
ON DELETE CASCADE
);
-- customers
CREATE TABLE customers
(
customer_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 320
PRIMARY KEY,
name VARCHAR2( 255 ) NOT NULL,
address VARCHAR2( 255 ) ,
website VARCHAR2( 255 ) ,
credit_limit NUMBER( 8, 2 )
);
-- contacts
CREATE TABLE contacts
(
contact_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 320
PRIMARY KEY,
first_name VARCHAR2( 255 ) NOT NULL,
last_name VARCHAR2( 255 ) NOT NULL,
email VARCHAR2( 255 ) NOT NULL,
phone VARCHAR2( 20 ) ,
customer_id NUMBER ,
CONSTRAINT fk_contacts_customers
FOREIGN KEY( customer_id )
REFERENCES customers( customer_id )
ON DELETE CASCADE
);
-- orders table
CREATE TABLE orders
(
order_id NUMBER
GENERATED BY DEFAULT AS IDENTITY START WITH 106
PRIMARY KEY,
customer_id NUMBER( 6, 0 ) NOT NULL, -- fk
status VARCHAR( 20 ) NOT NULL ,
salesman_id NUMBER( 6, 0 ) , -- fk
order_date DATE NOT NULL ,
CONSTRAINT fk_orders_customers
FOREIGN KEY( customer_id )
REFERENCES customers( customer_id )
ON DELETE CASCADE,
CONSTRAINT fk_orders_employees
FOREIGN KEY( salesman_id )
REFERENCES employees( employee_id )
ON DELETE SET NULL
);
-- order items
CREATE TABLE order_items
(
order_id NUMBER( 12, 0 ) , -- fk
item_id NUMBER( 12, 0 ) ,
product_id NUMBER( 12, 0 ) NOT NULL , -- fk
quantity NUMBER( 8, 2 ) NOT NULL ,
unit_price NUMBER( 8, 2 ) NOT NULL ,
CONSTRAINT pk_order_items
PRIMARY KEY( order_id, item_id ),
CONSTRAINT fk_order_items_products
FOREIGN KEY( product_id )
REFERENCES products( product_id )
https://www.oracletutorial.com/oracle-basics/
ON DELETE CASCADE,
CONSTRAINT fk_order_items_orders
FOREIGN KEY( order_id )
REFERENCES orders( order_id )
ON DELETE CASCADE
);
-- inventories
CREATE TABLE inventories
(
product_id NUMBER( 12, 0 ) , -- fk
warehouse_id NUMBER( 12, 0 ) , -- fk
quantity NUMBER( 8, 0 ) NOT NULL,
CONSTRAINT pk_inventories
PRIMARY KEY( product_id, warehouse_id ),
CONSTRAINT fk_inventories_products
FOREIGN KEY( product_id )
REFERENCES products( product_id )
ON DELETE CASCADE,
CONSTRAINT fk_inventories_warehouses
FOREIGN KEY( warehouse_id )
REFERENCES warehouses( warehouse_id )
ON DELETE CASCADE
);
In this tutorial, we have introduced you the Oracle sample database and shown you how
to download it. Now, you should be ready to create the sample database in your Oracle
database server for practice.
https://www.oracletutorial.com/oracle-basics/