Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

PSORC-Oracle SQL 1-Tables and Views

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

PSORC-Oracle SQL 1-Tables and Views

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

Oracle SQL-1

Oracle Tables and Views


Muchake Brian
Phone: 0701178573
Email: bmuchake@mak.ac.ug, bmuchake@gmail.com

Do not Keep Company With Worthless People


Psalms 26:11
Objectives
 Discuss:
• Create Table
• Create Table AS
• Alter Table
• Drop Table
• Global Temp Tables
• Local Temp Tables
• Table Views
Create Table
• In Oracle, CREATE TABLE statement is used to create a new table in the
database.
• To create a table, you have to name that table and define its columns and
datatype for each column.
Syntax:
• CREATE TABLE table_name
• (
• column1 datatype [ NULL | NOT NULL ],
• column2 datatype [ NULL | NOT NULL ],
• ...
• column_n datatype [ NULL | NOT NULL ]
• );
Create Table [Cont’d]
Parameters used in syntax
• table_name: It specifies the name of the table which you want to create.
• column1, column2, ... column n: It specifies the columns which you want to add in the
table. Every column must have a datatype.
• Note: Every column should either be defined as "NULL" or "NOT NULL". In the case, the
value is left blank; it is treated as "NULL" as default.
Example
• Here we are creating a table named customers. This table doesn't have any primary
key.
• CREATE TABLE customers
• (customer_id number(10) NOT NULL,
• customer_name varchar2(50) NOT NULL,
• city varchar2(50)
• );
Create Table [Cont’d]
• This table contains three columns:
• customer_id: It is the first column created as a number datatype (maximum 10 digits in
length) and cannot contain null values.
• customer_name: it is the second column created as a varchar2 datatype (50 maximum
characters in length) and cannot contain null values.
• city: This is the third column created as a varchar2 datatype. It can contain null values.
Oracle CREATE TABLE Example with primary key
• CREATE TABLE customers
• (customer_id number(10) NOT NULL,
• customer_name varchar2(50) NOT NULL,
• city varchar2(50),
• CONSTRAINT customers_pk PRIMARY KEY (customer_id)
• );
Oracle Tables [Cont’d]
• A primary key is a single field or combination of fields that contains a
unique record. It must be filled. None of the field of primary key can
contain a null value. A table can have only one primary key.
• Note: In Oracle, total number of columns cannot be more than 32.
CREATE TABLE AS Statement
• The CREATE TABLE AS statement is used to create a table from an existing table by
copying the columns of existing table.
• Note: If you create the table in this way, the new table will contain records from the
existing table.
Syntax:
• CREATE TABLE new_table
• AS (SELECT * FROM old_table);
CREATE TABLE AS Statement [Cont’d]
 Example 1: Create Table Example: copying all columns of another table
• In this example, we are creating a "newcustomers" table by copying all the columns from the
already existing table "Customers".
• CREATE TABLE newcustomers
• AS (SELECT * FROM customers WHERE customer_id < 5000);
• This table is named as "newcustomers" and having the same columns of "customers" table.
 Example 2: Create Table Example: copying selected columns of another table
Syntax:
CREATE TABLE new_table
• AS (SELECT column_1, column2, ... column_n
• FROM old_table);
CREATE TABLE AS Statement [Cont’d]
• Let's take an example: Consider that you have already created two
tables "regularcustomers" and "irregularcustomers".
• The table "regularcustomers" has three columns rcustomer_id,
rcustomer_name and rc_city.
• CREATE TABLE regularcustomers
• (RCUSTOMER_ID NUMBER(10,0) NOT NULL ENABLE,
• RCUSTOMER_NAME" VARCHAR2(50) NOT NULL ENABLE,
• RC_CITY" VARCHAR2(50)
• );
CREATE TABLE AS Statement [Cont’d]
• The second table "irregularcustomers" has also three columns
ircustomer_id, ircustomer_name and irc_city.
• CREATE TABLE irregularcustomers
• (IRCUSTOMER_ID NUMBER(10,0) NOT NULL ENABLE,
• IRCUSTOMER_NAME VARCHAR2(50) NOT NULL ENABLE,
• IRC_CITY VARCHAR2(50)
• );
CREATE TABLE AS Statement [Cont’d]
• In the following example, we will create a table name "newcustomers3"
form copying columns from both tables.
• Example:
• CREATE TABLE newcustomers3
• AS (SELECT regularcustomers.rcustomer_id, regularcustomers.rc_city,
irregularcustomers.ircustomer_name
• FROM regularcustomers, irregularcustomers
• WHERE regularcustomers.rcustomer_id = irregularcustomers.ircustomer_id
• AND regularcustomers.rcustomer_id < 5000);
Oracle ALTER TABLE Statement
• In Oracle, ALTER TABLE statement specifies how to: add, modify, drop or delete columns
in a table. It is also used to rename a table.
• 1. Add one column
• Below is how to add one column in a table
Syntax:
• ALTER TABLE table_name
• ADD column_name column-definition;
• Example:
• Consider that there is already an existing table customers. Now, add a new column
customer_age into the table customers.

Oracle ALTER TABLE Statement [Cont’d]
• ALTER TABLE customers
• ADD customer_age varchar2(50);
• Note: A new column "customer_age" will be added in customers table.
2. Adding Multiple columns
• Below is how to add multiple columns in the existing table
Syntax:
• ALTER TABLE table_name
• ADD (column_1 column-definition,
• column_2 column-definition,
• ...
• column_n column_definition);

Oracle ALTER TABLE Statement [Cont’d]
• Example
• ALTER TABLE customers
• ADD (customer_type varchar2(50),
• customer_address varchar2(50));
• Note: Two columns customer_type and customer_address will be added in the table
customers.
3. Modifying a column
• Below is how to modify column of a table
Syntax:
• ALTER TABLE table_name
• MODIFY column_name column_type;
Oracle ALTER TABLE Statement [Cont’d]
• Example:
• ALTER TABLE customers
• MODIFY customer_name varchar2(100) not null;
• Note: the column column_name in the customers table is modified to varchar2 (100) and forced the column
to not allow null values.
4. Modifying multiple columns
• Below is how to modify multiple columns of a table
Syntax:
• ALTER TABLE table_name
• MODIFY (column_1 column_type,
• column_2 column_type, …
• column_n column_type);
Oracle ALTER TABLE Statement [Cont’d]
• Example:
• ALTER TABLE customers
• MODIFY (customer_name varchar2(100) not null,
• city varchar2(100));
• Note: This will modify both the customer_name and city columns in the table.
5. Dropping a column
• Below is how to drop column of a table
Syntax:
• ALTER TABLE table_name
• DROP COLUMN column_name;


Oracle ALTER TABLE Statement [Cont’d]
• Example:
• ALTER TABLE customers
• DROP COLUMN customer_name;
• This will drop the customer_name column from the table.
6. Renaming a column
• Below is how to rename column of a table
Syntax:
• ALTER TABLE table_name
• RENAME COLUMN old_name to new_name;

Oracle ALTER TABLE Statement [Cont’d]
• Example:
• ALTER TABLE customers
• RENAME COLUMN customer_name to cname;
• This will rename the column customer_name into cname.
7. Renaming a table
• How to rename table
Syntax:
• ALTER TABLE table_name
• RENAME TO new_table_name;
Oracle ALTER TABLE Statement [Cont’d]
• Example:
• ALTER TABLE customers
• RENAME TO retailers;
• This will rename the customer table into "retailers" table.
Oracle DROP TABLE Statement

• Oracle DROP TABLE statement is used to remove or delete a table from the
Oracle database.
Syntax
• DROP [schema_name].TABLE table_name
• [ CASCADE CONSTRAINTS ]
• [ PURGE ];
Parameters
• schema_name: It specifies the name of the schema that owns the table.
• table_name: It specifies the name of the table which you want to remove from
the Oracle database.
Oracle DROP TABLE Statement [Cont’d]

• CASCADE CONSTRAINTS: It is optional. If specified, it will drop all


referential integrity constraints as well.
• PURGE: It is also optional. If specified, the table and its dependent
objects are placed in the recycle bin and can?t be recovered.
• Note: If there are referential integrity constraints on table_name and
you do not specify the CASCADE CONSTRAINTS option, the DROP TABLE
statement will return an error and Oracle will not drop the table.
Oracle DROP TABLE Statement [Cont’d]

• Example 1: DROP TABLE Example


• DROP TABLE customers;
• Note: This will drop the table named customers.
• Example 2: DROP TABLE Example with PURGE parameter
• DROP TABLE customers PURGE
• This statement will drop the table called customers and issue a PURGE so that
the space associated with the customers table is released and the customers
table is not placed in recycle bin. So, it is not possible to recover that table if
required.
Oracle Global Temporary Tables

• Temporary tables generally contain all of the features that ordinary tables have
like triggers, join cardinality, information about rows and block etc. The main
difference is that the temporary tables can't have foreign keys related to other
tables.
Syntax
• CREATE GLOBAL TEMPORARY TABLE table_name
• ( column1 datatype [ NULL | NOT NULL ],
• column2 datatype [ NULL | NOT NULL ],
• ...
• column_n datatype [ NULL | NOT NULL ]
• );

Oracle Global Temporary Tables [Cont’d]

Parameters
• table_name: The parameter table_name specifies the global temporary
table that you want to create.
• column1, column2, ... column_ n: It specifies the column that you want
create in the global temporary table.
• Note: Every column must have a datatype and should be defined as
NULL or NOTNULL. If the value is left blank, it is by default treated as
NULL.
Oracle Global Temporary Tables [Cont’d]

• Example:
• The following example specifies how to create a global temporary table
• CREATE GLOBAL TEMPORARY TABLE students
• ( student_id numeric(10) NOT NULL,
• student_name varchar2(50) NOT NULL,
• student_address varchar2(50)
• );
• Note: This will create a global temporary table called students
Oracle Local Temporary Tables

• In Oracle, local temporary tables are distinct within modules. These tables are defined
and scoped to the session in which you created it.
• Declare local temporary table
Syntax
• DECLARE LOCAL TEMPORARY TABLE table_name
• ( column1 datatype [ NULL | NOT NULL ],
• column2 datatype [ NULL | NOT NULL ],
• ...
• column_n datatype [ NULL | NOT NULL ]
• );
Oracle Local Temporary Tables [Cont’d]

Parameters
• table_name: The parameter table_name specifies the local temporary
table that you want to create.
• column1, column2,... column_ n: It specifies the column that you want
create in the local temporary table. Every column must have a datatype
and should be defined as NULL or NOTNULL. If the value is left blank, it
is by default treated as NULL.
Oracle View

• In Oracle, view is a virtual table that does not physically exist. It is stored in Oracle
data dictionary and do not store any data. It can be executed when called.
1. Creating a view in oracle using the Oracle CREATE VIEW
• A view is created by a query joining one or more tables.
Syntax:
• CREATE VIEW view_name AS
• SELECT columns
• FROM tables
• WHERE conditions;
Oracle View [Cont’d]
Parameters:
• view_name: It specifies the name of the Oracle VIEW that you want to create.
• Example:
• Let's take an example to create view. In this example, we are creating two tables
suppliers and orders first.
• Suppliers table:
• CREATE TABLE SUPPLIERS
• (SUPPLIER_ID NUMBER,
• SUPPLIER_NAME VARCHAR2(4000),
• SUPPLIER_ADDRESS VARCHAR2(4000)
• );
Oracle View [Cont’d]

• Orders table:
• CREATE TABLE "ORDERS"
• (ORDER_NO. NUMBER,
• QUANTITY NUMBER,
• PRICE NUMBER
• );
Oracle View [Cont’d

• Execute the following query to create a view name sup_orders.


• Create View Query:
• CREATE VIEW sup_orders AS
• SELECT suppliers.supplier_id, orders.quantity, orders.price
• FROM suppliers
• INNER JOIN orders
• ON suppliers.supplier_id = supplier_id
• WHERE suppliers.supplier_name = 'VOJO';
Output:
Oracle View [Cont’d

• You can now check the Oracle VIEW by this query:


• SELECT * FROM sup_orders;
• Output:
Oracle View [Cont’d

2. Oracle Update VIEW


•In Oracle, the CREATE OR REPLACE VIEW statement is used to modify
the definition of an Oracle VIEW without dropping it.
Syntax:
•CREATE OR REPLACE VIEW view_name AS
• SELECT columns
• FROM table
• WHERE conditions;
Oracle View [Cont’d

• Example:
• Execute the following query to update the definition of Oracle VIEW called
sup_orders without dropping it.
• CREATE or REPLACE VIEW sup_orders AS
• SELECT suppliers.supplier_id, orders.quantity, orders.price
• FROM suppliers
• INNER JOIN orders
• ON suppliers.supplier_id = supplier_id
• WHERE suppliers.supplier_name = 'HCL';

Oracle View [Cont’d

• You can now check the Oracle VIEW by this query:


• SELECT * FROM sup_orders;
• Output:

Oracle View [Cont’d

3. Oracle DROP VIEW


•The DROP VIEW statement is used to remove or delete the VIEW completely.
Syntax:
•DROP VIEW view_name;
•Example:
•DROP VIEW sup_orders;
Summary
• Create Table
• Create Table AS
• Alter Table
• Drop Table
• Global Temp Tables
• Local Temp Tables
• Table Views

You might also like