0% found this document useful (0 votes)
74 views

Oracle 1 Important

This document provides an overview of querying and modifying data in an Oracle database. It describes SQL commands for retrieving, filtering, sorting, joining, grouping and aggregating data from tables. It also covers inserting, updating, deleting data and managing database objects like tables and constraints. The document includes a sample database diagram with descriptions of its tables.

Uploaded by

Glenutzu Samp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Oracle 1 Important

This document provides an overview of querying and modifying data in an Oracle database. It describes SQL commands for retrieving, filtering, sorting, joining, grouping and aggregating data from tables. It also covers inserting, updating, deleting data and managing database objects like tables and constraints. The document includes a sample database diagram with descriptions of its tables.

Uploaded by

Glenutzu Samp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Querying data

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.

 SELECT – show you how to query 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.

 IN – determine if a value matches any value in a list or a subquery.

 BETWEEN – filter data based on a range of values.

 LIKE  – perform matching based on specific patterns.

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

 A visual explanation of Oracle Joins – a brief introduction to joins in Oracle using


visual illustrations.

 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

 HAVING – show you how to filter a group of rows.

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.

 Correlated Subquery – learn about the correlated subquery which is a subquery


that depends on the values returned by the outer query.

 EXISTS and NOT EXISTS – check for the existence of rows returned by a


subquery.

 ANY, SOME, and ALL – compare a value to a list or subquery. Note


that SOME and ANY are the same so they are interchangeable.

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.

 INTERSECT – teach you how to make an intersection of the results of two


independent queries.

 MINUS – learn how to subtract a result from another.

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.

 ROLLUP – describe how to calculate multiple levels of subtotals across a


specified group of dimensions.

 PIVOT – show you how to transpose rows to columns to make the crosstab
reports.

 UNPIVOT – a guide to rotating columns into rows.

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 – learn how to insert a row into a table.

 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.

 UPDATE – teach you how to change the existing values of a table.

 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

 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.

 FLOAT – demystify float data type in Oracle by practical examples.

 CHAR – learn about fixed-length character string.

 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.

 NVARCHAR2 – learn how to store variable-length Unicode characters in the


database.

 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.

 INTERVAL– focus on the interval data types to store periods of time.

 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.

 UNIQUE constraint – discuss how to ensure data stored in a column or a group of


columns is unique among rows within the whole table.

 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

The following illustrates the sample database diagram:

Table Names Description Records

 CONTACTS store contact person information of customers 319 records

https://www.oracletutorial.com/oracle-basics/
Table Names Description Records

 COUNTRIES store country information 25 records


 CUSTOMERS store customer master 319 records
 EMPLOYEES store employee master 107 records
 INVENTORIES store inventory information of products 1112 records
 LOCATIONS store locations of warehouses 23 records
 ORDERS store order header information 105 records
 ORDER_ITEMS store order line items 665 records
 PRODUCT_CATEGORIES store product categories 5 records
 PRODUCTS store product information 288 records
 REGIONS store regions where the company operates 4 records
 WAREHOUSES store warehouse information 9 records
Download Oracle Sample database

Download the following sample database in zip file format:

Download Oracle Sample Database

After downloading the file, you should extract it. The zip file contains the
following *.sql files:

  ot_create_user.sql is for creating OT user and grant privileges


  ot_schema.sql is for creating database objects such as tables, constraints, etc.
  ot_data.sql is for loading data into the tables.
  ot_drop.sql is for removing all objects in the sample database.
The following illustrates the statements for creating the database objects.

CREATE TABLE regions


  (
    region_id NUMBER GENERATED BY DEFAULT AS IDENTITY
    START WITH 5 PRIMARY KEY,
    region_name VARCHAR2( 50 ) NOT NULL
  );
-- countries table
CREATE TABLE countries
  (
    country_id   CHAR( 2 ) PRIMARY KEY  ,
    country_name VARCHAR2( 40 ) NOT NULL,
    region_id    NUMBER                 , -- fk
    CONSTRAINT fk_countries_regions FOREIGN KEY( region_id )
      REFERENCES regions( region_id )
      ON DELETE CASCADE

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/

You might also like