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

Oracle Queries

Uploaded by

giriraj
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)
24 views

Oracle Queries

Uploaded by

giriraj
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

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;

2) Oracle Insert Query


Oracle insert query is used to insert records into table. For example:

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

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;

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;

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;

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

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);

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