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

Oracle Tutorial - Partie 1

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

Oracle Tutorial - Partie 1

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

Oracle Tutorial

Oracle tutorial provides basic and advanced concepts of Oracle. Our Oracle tutorial is
designed for beginners and professionals.

Oracle is a relational database management system. It is widely used in enterprise


applications.

Our Oracle tutorial includes all topics of Oracle database such as insert record, update
record, delete record, select record, create table, drop table etc. There are also given
Oracle interview questions to help you better understand the Oracle database.

What is Oracle
Oracle database is a relational database management system. It is known as Oracle
database, OracleDB or simply Oracle. It is produced and marketed by Oracle Corporation.

Oracle database is the first database designed for enterprise grid computing. The
enterprise grid computing provides the most flexible and cost effective way to manage
information and applications.

Different editions of Oracle database


Following are the four editions of the Oracle database.

o Enterprise Edition: It is the most robust and secure edition. It offers all
features, including superior performance and security.
o Standard Edition: It provides the base functionality for users that do not require
Enterprise Edition's robust package.
o Express Edition (XE): It is the lightweight, free and limited Windows and Linux
edition.
o Oracle Lite: It is designed for mobile devices.

The Oracle Corporation


Oracle Corporation is the largest software company in the field of database business. Its
relational database was the first to support SQL which has since become the industry
standard.

Oracle database is one of the most trusted and widely used relational database engines.
The biggest rival of Oracle database is Microsoft's SQL Server.

History of Oracle
Oracle was originally developed by Lawrence Ellison (Larry Ellision) and his two friends
and former co-worker in 1977. Oracle DB runs on the most major platforms like
Windows, UNIX, Linux and Mac OS.

Oracle 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:

1. CREATE TABLE table_name


2. (
3. column1 datatype [ NULL | NOT NULL ],
4. column2 datatype [ NULL | NOT NULL ],
5. ...
6. column_n datatype [ NULL | NOT NULL ]
7. );

Parameters used in syntax


o table_name: It specifies the name of the table which you want to create.
o column1, column2, ... column n: It specifies the columns which you want to
add in the table. Every column must have a datatype. 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.
Oracle CREATE TABLE Example
Here we are creating a table named customers. This table doesn't have any primary key.

1. CREATE TABLE customers


2. ( customer_id number(10) NOT NULL,
3. customer_name varchar2(50) NOT NULL,
4. city varchar2(50)
5. );

This table contains three columns

o customer_id: It is the first column created as a number datatype (maximum 10


digits in length) and cannot contain null values.
o customer_name: it is the second column created as a varchar2 datatype (50
maximum characters in length) and cannot contain null values.
o city: This is the third column created as a varchar2 datatype. It can contain null
values.

Oracle CREATE TABLE Example with primary key


1. CREATE TABLE customers
2. ( customer_id number(10) NOT NULL,
3. customer_name varchar2(50) NOT NULL,
4. city varchar2(50),
5. CONSTRAINT customers_pk PRIMARY KEY (customer_id)
6. );

What is Primary key


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.

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:

1. CREATE TABLE new_table


2. AS (SELECT * FROM old_table);

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

1. CREATE TABLE newcustomers


2. AS (SELECT * FROM customers WHERE customer_id < 5000);
Table created.

This table is named as "newcustomers" and having the same columns of "customers"
table.

Create Table Example: copying selected columns of


another table
Syntax:

1. CREATE TABLE new_table


2. AS (SELECT column_1, column2, ... column_n
3. FROM old_table);

Let's take an example:

1. CREATE TABLE newcustomers2


2. AS (SELECT customer_id, customer_name
3. FROM customers
4. WHERE customer_id < 5000);

The above example will create a new table called "newcustomers2". This table includes
the specified columns customer_id and customer_name from the customers table.

Create Table Example: copying selected columns


from multiple tables
Syntax:
1. CREATE TABLE new_table
2. AS (SELECT column_1, column2, ... column_n
3. FROM old_table_1, old_table_2, ... old_table_n);

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.

1. CREATE TABLE "regularcustomers"


2. ( "RCUSTOMER_ID" NUMBER(10,0) NOT NULL ENABLE,
3. "RCUSTOMER_NAME" VARCHAR2(50) NOT NULL ENABLE,
4. "RC_CITY" VARCHAR2(50)
5. )
6. /

The second table "irregularcustomers" has also three columns ircustomer_id,


ircustomer_name and irc_city.

1. CREATE TABLE "irregularcustomers"


2. ( "IRCUSTOMER_ID" NUMBER(10,0) NOT NULL ENABLE,
3. "IRCUSTOMER_NAME" VARCHAR2(50) NOT NULL ENABLE,
4. "IRC_CITY" VARCHAR2(50)
5. )
6. /

In the following example, we will create a table name "newcustomers3" form copying
columns from both tables.

Example:

1. CREATE TABLE newcustomers3


2. AS (SELECT regularcustomers.rcustomer_id, regularcustomers.rc_city, irregular
customers.ircustomer_name
3. FROM regularcustomers, irregularcustomers
4. WHERE regularcustomers.rcustomer_id = irregularcustomers.ircustomer_id
5. 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.

How to add column in a table


Syntax:
1. ALTER TABLE table_name
2. ADD column_name column-definition;

Example:

Consider that already existing table customers. Now, add a new column customer_age
into the table customers.

1. ALTER TABLE customers


2. ADD customer_age varchar2(50);

Now, a new column "customer_age" will be added in customers table.

How to add multiple columns in the existing table


Syntax:

1. ALTER TABLE table_name


2. ADD (column_1 column-definition,
3. column_2 column-definition,
4. ...
5. column_n column_definition);

Example

1. ALTER TABLE customers


2. ADD (customer_type varchar2(50),
3. customer_address varchar2(50));
Now, two columns customer_type and customer_address will be added in the
table customers.

How to modify column of a table


Syntax:

1. ALTER TABLE table_name


2. MODIFY column_name column_type;

Example:

1. ALTER TABLE customers


2. MODIFY customer_name varchar2(100) not null;
Now the column column_name in the customers table is modified
to varchar2 (100) and forced the column to not allow null values.

How to modify multiple columns of a table


Syntax:

1. ALTER TABLE table_name


2. MODIFY (column_1 column_type,
3. column_2 column_type,
4. ...
5. column_n column_type);

Example:

1. ALTER TABLE customers


2. MODIFY (customer_name varchar2(100) not null,
3. city varchar2(100));
This will modify both the customer_name and city columns in the table.

How to drop column of a table


Syntax:

1. ALTER TABLE table_name


2. DROP COLUMN column_name;

Example:

1. ALTER TABLE customers


2. DROP COLUMN customer_name;
This will drop the customer_name column from the table.

How to rename column of a table


Syntax:

1. ALTER TABLE table_name


2. RENAME COLUMN old_name to new_name;

Example:

1. ALTER TABLE customers


2. RENAME COLUMN customer_name to cname;
This will rename the column customer_name into cname.

How to rename table


Syntax:

1. ALTER TABLE table_name


2. RENAME TO new_table_name;

Example:

1. ALTER TABLE customers


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

1. DROP [schema_name].TABLE table_name


2. [ CASCADE CONSTRAINTS ]
3. [ 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.

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.

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.

DROP TABLE Example


1. DROP TABLE customers;
This will drop the table named customers.

DROP TABLE Example with PURGE parameter


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

1. CREATE GLOBAL TEMPORARY TABLE table_name


2. ( column1 datatype [ NULL | NOT NULL ],
3. column2 datatype [ NULL | NOT NULL ],
4. ...
5. column_n datatype [ NULL | NOT NULL ]
6. );

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

Example
The following example specifies how to create a global temporary table

1. CREATE GLOBAL TEMPORARY TABLE students


2. ( student_id numeric(10) NOT NULL,
3. student_name varchar2(50) NOT NULL,
4. student_address varchar2(50)
5. );
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
1. DECLARE LOCAL TEMPORARY TABLE table_name
2. ( column1 datatype [ NULL | NOT NULL ],
3. column2 datatype [ NULL | NOT NULL ],
4. ...
5. column_n datatype [ NULL | NOT NULL ]
6. );

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.

A view is created by a query joining one or more tables.

Oracle CREATE VIEW


Syntax:

1. CREATE VIEW view_name AS


2. SELECT columns
3. FROM tables
4. WHERE conditions;

Parameters:

o 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:

1.
2. CREATE TABLE "SUPPLIERS"
3. ( "SUPPLIER_ID" NUMBER,
4. "SUPPLIER_NAME" VARCHAR2(4000),
5. "SUPPLIER_ADDRESS" VARCHAR2(4000)
6. )
7. /
8.

Orders table:

1. CREATE TABLE "ORDERS"


2. ( "ORDER_NO." NUMBER,
3. "QUANTITY" NUMBER,
4. "PRICE" NUMBER
5. )
6. /

Execute the following query to create a view name sup_orders.

Create View Query:

1. CREATE VIEW sup_orders AS


2. SELECT suppliers.supplier_id, orders.quantity, orders.price
3. FROM suppliers
4. INNER JOIN orders
5. ON suppliers.supplier_id = supplier_id
6. WHERE suppliers.supplier_name = 'VOJO';
Output:
View created.
0.21 seconds

You can now check the Oracle VIEW by this query:

1. SELECT * FROM sup_orders;


Output:
SUPPLIER_ID QUANTITY PRICE
3 35 70
3 26 125
3 18 100
3 rows returned in 0.00 seconds

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:

1. CREATE OR REPLACE VIEW view_name AS


2. SELECT columns
3. FROM table
4. WHERE conditions;
Example:

Execute the following query to update the definition of Oracle VIEW called sup_orders
without dropping it.

1. CREATE or REPLACE VIEW sup_orders AS


2. SELECT suppliers.supplier_id, orders.quantity, orders.price
3. FROM suppliers
4. INNER JOIN orders
5. ON suppliers.supplier_id = supplier_id
6. WHERE suppliers.supplier_name = 'HCL';

You can now check the Oracle VIEW by this query:

1. SELECT * FROM sup_orders;

Output:

SUPPLIER_ID QUANTITY PRICE


1 35 70
1 26 125
1 18 100
row(s) 1 - 3 of 3

Oracle DROP VIEW


The DROP VIEW statement is used to remove or delete the VIEW completely.

Syntax:

1. DROP VIEW view_name;

Example:

1. DROP VIEW sup_orders;

Oracle Queries
You can execute many queries in oracle database such as insert, update, delete, alter
table, drop, create and select.

1) Oracle Select Query


Oracle select query is used to fetch records from database. For example:

1. SELECT * from customers;

More Details...
2) Oracle Insert Query
Oracle insert query is used to insert records into table. For example:

1. insert into customers values(101,'rahul','delhi');

More Details...

3) Oracle Update Query


Oracle update query is used to update records of a table. For example:

1. update customers set name='bob', city='london' where id=101;

More Details...

4) Oracle Delete Query


Oracle update query is used to delete records of a table from database. For example:

1. delete from customers where id=101;

More Details...

5) Oracle Truncate Query


Oracle update query is used to truncate or remove records of a table. It doesn't remove
structure. For example:

1. truncate table customers;

More Details...

6) Oracle Drop Query


Oracle drop query is used to drop a table or view. It doesn't have structure and data. For
example:

1. drop table customers;

More Details...

7) Oracle Create Query


Oracle create query is used to create a table, view, sequence, procedure and function.
For example:
1. CREATE TABLE customers
2. ( id number(10) NOT NULL,
3. name varchar2(50) NOT NULL,
4. city varchar2(50),
5. CONSTRAINT customers_pk PRIMARY KEY (id)
6. );

More Details...

8) Oracle Alter Query


Oracle alter query is used to add, modify, delete or drop colums of a table. Let's see a
query to add column in customers table:

1. ALTER TABLE customers


2. ADD age varchar2(50);

More Details...

Oracle SELECT Statement


The Oracle SELECT statement is used to retrieve data from one or more than one tables,
object tables, views, object views etc.

Syntax

1. SELECT expressions
2. FROM tables
3. WHERE conditions;

Parameters
1) expressions: It specifies the columns or calculations that you want to retrieve.

2) tables:This parameter specifies the tables that you want to retrieve records from.
There must be at least one table within the FROM clause.

3) conditions: It specifies the conditions that must be followed for selection.

Select Example: select all fields


Let's take an example to select all fields from an already created table named customers

1. SELECT *
2. FROM customers;

output
Select Example: select specific fields
Example

1. SELECT age, address, salary


2. FROM customers
3. WHERE age < 25
4. AND salary > '20000'
5. ORDER BY age ASC, salary DESC;

Select Example: select fields from multiple tables


(JOIN)
1. SELECT customers.name, courses.trainer
2. FROM courses
3. INNER JOIN customers
4. ON courses.course_id = course_id
5. ORDER BY name;

output
Oracle Insert Statement
In Oracle, INSERT statement is used to add a single record or multiple records into the
table.

Syntax: (Inserting a single record using the Values keyword):

1. INSERT INTO table


2. (column1, column2, ... column_n )
3. VALUES
4. (expression1, expression2, ... expression_n );

Syntax: (Inserting multiple records using a SELECT statement):

1. INSERT INTO table


2. (column1, column2, ... column_n )
3. SELECT expression1, expression2, ... expression_n
4. FROM source_table
5. WHERE conditions;

Parameters:
1) table: The table to insert the records into.

2) column1, column2, ... column_n:

The columns in the table to insert values.

3) expression1, expression2, ... expression_n:

The values to assign to the columns in the table. So column1 would be assigned the
value of expression1, column2 would be assigned the value of expression2, and so on.
4) source_table:

The source table when inserting data from another table.

5) conditions:

The conditions that must be met for the records to be inserted.

Oracle Insert Example: By VALUE keyword


It is the simplest way to insert elements to a database by using VALUE keyword.

See this example:

Consider here the already created suppliers table. Add a new row where the value of
supplier_id is 23 and supplier_name is Flipkart.

See this example:


1. INSERT INTO suppliers
2. (supplier_id, supplier_name)
3. VALUES
4. (50, 'Flipkart');
Output:
1 row(s) inserted.
0.02 seconds

Oracle Insert Example: By SELECT statement


This method is used for more complicated cases of insertion. In this method insertion is
done by SELECT statement. This method is used to insert multiple elements.

See this example:

In this method, we insert values to the "suppliers" table from "customers" table. Both
tables are already created with their respective columns.

Execute this query:


1. INSERT INTO suppliers
2. (supplier_id, supplier_name)
3. SELECT age, address
4. FROM customers
5. WHERE age > 20;
Output:
4 row(s) inserted.

0.00 seconds

You can even check the number of rows that you want to insert by following statement:
1. SELECT count(*)
2. FROM customers
3. WHERE age > 20;
Output:
Count(*)
4

Oracle INSERT ALL statement


The Oracle INSERT ALL statement is used to insert multiple rows with a single INSERT
statement. You can insert the rows into one table or multiple tables by using only one
SQL command.

Syntax

1. INSERT ALL
2. INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_
n)
3. INTO table_name(column1, column2, column_n) VALUES (expr1, expr2, expr_
n)
4. INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_
n)
5. SELECT * FROM dual;

Parameters
1) table_name: it specifies the table in which you want to insert your records.

2) column1, column2, column_n: this specifies the columns in the table to insert
values.

3) expr1, expr2, expr_n: this specifies the values to assign to the columns in the table.

Oracle INSERT ALL Example


This example specifies how to insert multiple records in one table. Here we insert three
rows into the "suppliers" table.

1. INSERT ALL
2. INTO suppliers (supplier_id, supplier_name) VALUES (20, 'Google')
3. INTO suppliers (supplier_id, supplier_name) VALUES (21, 'Microsoft')
4. INTO suppliers (supplier_id, supplier_name) VALUES (22, 'Apple')
5. SELECT * FROM dual;

Output

3 row(s) inserted.
0.02 seconds
This is totally equivalent to the following three INSERT statements.

1. INSERT INTO suppliers (supplier_id, supplier_name) VALUES (1000, 'Google');


2. INSERT INTO suppliers (supplier_id, supplier_name) VALUES (2000, 'Microsoft')
;
3. INSERT INTO suppliers (supplier_id, supplier_name) VALUES (3000, 'Apple');

Oracle INSERT ALL Example: (Insert into multiple


tables)
The INSERT ALL statement can also be used to insert multiple rows into more than one
table by one command only.

In the following example, we are going to insert records into the both "suppliers" and
"customers" tables.

1. INSERT ALL
2. INTO suppliers (supplier_id, supplier_name) VALUES (30, 'Google')
3. INTO suppliers (supplier_id, supplier_name) VALUES (31, 'Microsoft')
4. INTO customers (age, name, address) VALUES (29, 'Luca Warsi', 'New York')
5. SELECT * FROM dual;

Output

3 row(s) inserted.
0.03 seconds

Here, total 3 rows are inserted, 2 rows are inserted into the suppliers table and one row
into the customers table.

Oracle UPDATE Statement


In Oracle, UPDATE statement is used to update the existing records in a table. You can
update a table in 2 ways.

Traditional Update table method


Syntax:

1. UPDATE table
2. SET column1 = expression1,
3. column2 = expression2,
4. ...
5. column_n = expression_n
6. WHERE conditions;
Update Table by selecting rocords from another table
Syntax:

1. UPDATE table1
2. SET column1 = (SELECT expression1
3. FROM table2
4. WHERE conditions)
5. WHERE conditions;

Parameters:
1) column1, column2, ... column_n:

It specifies the columns that you want to update.

2) expression1, expression2, ...expression_n:

This specifies the values to assign to the column1, column2, ?. column_n.

3) conditions:It specifies the conditions that must be fulfilled for execution of UPDATE
stateme.

Oracle Update Example: (Update single column)


1. UPDATE suppliers
2. SET supplier_name = 'Kingfisher'
3. WHERE supplier_id = 2;

This example will update the supplier_name as "Kingfisher" where "supplier_id" is 2.

Oracle Update Example: (Update multiple columns)


The following example specifies how to update multiple columns in a table. In this
example, two columns supplier_name and supplier_address is updated by a single
statement.

1. UPDATE suppliers
2. SET supplier_address = 'Agra',
3. supplier_name = 'Bata shoes'
4. WHERE supplier_id = 1;

Output:

1 row(s) updated.
0.06 seconds
Oracle Update Example: (By selecting records from
another table)
1. UPDATE customers
2. SET name = (SELECT supplier_name
3. FROM suppliers
4. WHERE suppliers.supplier_name = customers.name)
5. WHERE age < 25;

Output:

2 row(s) updated.
0.02 seconds

Here, the customers table is updated by fetching the data from "suppliers" table.

Oracle DELETE Statement


In Oracle, DELETE statement is used to remove or delete a single record or multiple
records from a table.

Syntax

1. DELETE FROM table_name


2. WHERE conditions;

Parameters
1) table_name: It specifies the table which you want to delete.

2) conditions: It specifies the conditions that must met for the records to be deleted.

Oracle Delete Example: On one condition


1. DELETE FROM customers
2. WHERE name = 'Sohan';

This statement will delete all records from the customer table where name is "Sohan".

Oracle Delete Example: On multiple conditions


1. DELETE FROM customers
2. WHERE last_name = 'Maurya'
3. AND customer_id > 2;
This statement will delete all records from the customers table where the last_name is
"Maurya" and the customer_id is greater than 2.

Oracle TRUNCATE TABLE


In Oracle, TRUNCATE TABLE statement is used to remove all records from a table. It
works same as DELETE statement but without specifying a WHERE clause. It is generally
used when you don?t have to worry about rolling back

Once a table is truncated, it can?t be rolled back. The TRUNCATE TABLE statement does
not affect any of the table?s indexes, triggers or dependencies.

Syntax

1. TRUNCATE TABLE [schema_name.]table_name

Parameters
1) schema_name: This parameter specifies the name of the schema that the table
belongs to. It is optional.

2) table_name: It specifies the table that you want to truncate.

Oracle TRUNCATE Table Example


Consider a table named "customers" and execute the following query to truncate this

1. TRUNCATE TABLE customers;

Output

Table truncated.
1.11 seconds

Now check the customers table, you will find that there is no data available in that table.
It is equally similar to DELETE TABLE statement in Oracle.

Oracle DELETE Table Example


1. DELETE TABLE customers;

TRUNCATE TABLE vs DELETE TABLE


Both the statements will remove the data from the "customers" table but the main
difference is that you can roll back the DELETE statement whereas you can't roll back the
TRUNCATE TABLE statement.

You might also like