Oracle Tutorial - Partie 1
Oracle Tutorial - Partie 1
Oracle tutorial provides basic and advanced concepts of Oracle. Our Oracle tutorial is
designed for beginners and professionals.
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.
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.
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.
To create a table, you have to name that table and define its columns and datatype for
each column.
Syntax:
Syntax:
This table is named as "newcustomers" and having the same columns of "customers"
table.
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.
Let's take an example: Consider that you have already created two tables
"regularcustomers" and "irregularcustomers".
In the following example, we will create a table name "newcustomers3" form copying
columns from both tables.
Example:
Example:
Consider that already existing table customers. Now, add a new column customer_age
into the table customers.
Example
Example:
Example:
Example:
Example:
Example:
Syntax
table_name: It specifies the name of the table which you want to remove from the
Oracle database.
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.
Syntax
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
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.
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:
Syntax:
Execute the following query to update the definition of Oracle VIEW called sup_orders
without dropping it.
Output:
Syntax:
Example:
Oracle Queries
You can execute many queries in oracle database such as insert, update, delete, alter
table, drop, create and select.
More Details...
2) Oracle Insert Query
Oracle insert query is used to insert records into table. For example:
More Details...
More Details...
More Details...
More Details...
More Details...
More Details...
More Details...
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.
1. SELECT *
2. FROM customers;
output
Select Example: select specific fields
Example
output
Oracle Insert Statement
In Oracle, INSERT statement is used to add a single record or multiple records into the
table.
Parameters:
1) table: The table to insert the records into.
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:
5) conditions:
Consider here the already created suppliers table. Add a new row where the value of
supplier_id is 23 and supplier_name is Flipkart.
In this method, we insert values to the "suppliers" table from "customers" table. Both
tables are already created with their respective columns.
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
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.
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.
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.
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:
3) conditions:It specifies the conditions that must be fulfilled for execution of UPDATE
stateme.
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.
Syntax
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.
This statement will delete all records from the customer table where name is "Sohan".
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
Parameters
1) schema_name: This parameter specifies the name of the schema that the table
belongs to. It is optional.
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.