PSORC-Oracle SQL 1-Tables and Views
PSORC-Oracle SQL 1-Tables and Views
•
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]
• 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
• 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