Data Manipulation in Oracle
Data Manipulation in Oracle
Data Manipulation in Oracle
Unit 2
Data Manipulation Language (DML)
• Data Manipulation Language (DML)
statements access and manipulate data stored
in the Oracle database. You can use them to
insert, update, delete, and read data. Control
statements are related, since they are used to
control how Oracle operates when accessing
data in the database
Create a table: it is not DML, it is DDL
• CREATE TABLE customers
• ( customer_id number(10) NOT NULL,
• customer_name varchar2(50) NOT NULL,
• city varchar2(50),
• CONSTRAINT customers_pk PRIMARY KEY (c
ustomer_id)
• );
What is Primary key
• CREATE TABLE new_table
• AS (SELECT column_1, column2, ... column_n
• FROM old_table);
• CREATE TABLE newcustomers2
• AS (SELECT customer_id, customer_name
• FROM customers
• 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
• CREATE TABLE new_table
• AS (SELECT column_1, column2, ... column_n
• FROM old_table_1, old_table_2, ... old_table_n);
• 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 N
ULL ENABLE,
• "RCUSTOMER_NAME" VARCHAR2(50) NOT
NULL ENABLE,
• "RC_CITY" VARCHAR2(50)
• )
• 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 ENA
BLE,
• "IRCUSTOMER_NAME" VARCHAR2(50) NOT NULL EN
ABLE,
• "IRC_CITY" VARCHAR2(50)
• )
• In the following example, we will create a table name
"newcustomers3" form copying columns from both
tables.
• CREATE TABLE newcustomers3
• AS (SELECT regularcustomers.rcustomer_id, regularcust
omers.rc_city, irregularcustomers.ircustomer_name
• FROM regularcustomers, irregularcustomers
• WHERE regularcustomers.rcustomer_id = irregularcus
tomers.ircustomer_id
• AND regularcustomers.rcustomer_id < 5000);
Modify/Alter a Table
• 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:
• ALTER TABLE table_name
• ADD column_name column-definition;
Example:
• Consider that already existing table
customers.
• Now, add a new column customer_age into
the table customers.
• ALTER TABLE customers
• 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:
• ALTER TABLE table_name
• ADD (column_1 column-definition,
• column_2 column-definition,
• ...
• column_n column_definition);
How to modify column of a table
• Syntax:
• ALTER TABLE table_name
• MODIFY column_name column_type;
Example:
• ALTER TABLE customers
• ADD (customer_type varchar2(50),
• customer_address varchar2(50));
• Syntax:
• ALTER TABLE table_name
• MODIFY (column_1 column_type,
• column_2 column_type,
• ...
• column_n column_type);
Example
• ALTER TABLE customers
• MODIFY (customer_name varchar2(100) not
null,
• city varchar2(100));
• This will modify both the customer_name and
city columns in the table.
How to drop column of a table
• Syntax:
• ALTER TABLE table_name
• DROP COLUMN column_name;
Example
• ALTER TABLE customers
• DROP COLUMN customer_name;
• This will drop the customer_name column
from the table.
How to rename column of a table
• Syntax
• ALTER TABLE table_name
• RENAME COLUMN old_name to new_name;
Example
• ALTER TABLE customers
• RENAME COLUMN customer_name to cname
;
• This will rename the column customer_name
into cname
How to Rename table
• Syntax;
• ALTER TABLE table_name
• RENAME TO new_table_name;
Example
• ALTER TABLE customers
• RENAME TO retailers;
• This will rename the customer table into
"retailers" table.
Oracle DROP TABLE
• Oracle DROP TABLE statement is used to remove or delete a table from the Oracle
database. Syntax
• DROP TABLE customers;
• This will drop the table named customers.
• 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 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
• SELECT expressions
• FROM tables
• 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
• SELECT *
• FROM customers;
Select Example: select specific fields
• Example
• SELECT age, address, salary
• FROM customers
• WHERE age < 25
• AND salary > '20000'
• ORDER BY age ASC, salary DESC;
Select Example: select fields from multiple
tables (JOIN)
• SELECT customers.name, courses.trainer
• FROM courses
• INNER JOIN customers
• ON courses.course_id = course_id
• ORDER BY name;
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):
• INSERT INTO table
• (column1, column2, ... column_n )
• VALUES
• (expression1, expression2, ... expression_n );
Syntax: (Inserting multiple records using a SELECT
statement)
• INSERT INTO table
• (column1, column2, ... column_n )
• SELECT expression1, expression2, ... expressio
n_n
• FROM source_table
• WHERE conditions;
• Parameters:
• 1) table: The table to insert the records into.
• ) 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
• INSERT ALL
• INTO table_name (column1, column2, colum
n_n) VALUES (expr1, expr2, expr_n)
• INTO table_name(column1, column2, column
_n) VALUES (expr1, expr2, expr_n)
• INTO table_name (column1, column2, colum
n_n) VALUES (expr1, expr2, expr_n)
• SELECT * FROM dual;
Parameters
• In the following example, we are going to insert records into the both "suppliers"
and "customers" tables.
• INSERT ALL
• INTO suppliers (supplier_id, supplier_name) VALUES (30, 'Google')
• INTO suppliers (supplier_id, supplier_name) VALUES (31, 'Microsoft')
• INTO customers (age, name, address) VALUES (29, 'Luca Warsi', 'New York')
• SELECT * FROM dual;
• Output
• 3 row(s) inserted.
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:
• UPDATE table
• SET column1 = expression1,
• column2 = expression2,
• ...
• column_n = expression_n
• WHERE conditions;
Update Table by selecting records from
another table
• UPDATE table1
• SET column1 = (SELECT expression1
• FROM table2
• WHERE conditions)
• 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)
• UPDATE suppliers
• SET supplier_name = 'Kingfisher'
• WHERE supplier_id = 2;
• This example will update the supplier_name
as "Kingfisher" where "supplier_id" is 2.
Oracle Update Example: (Update multiple columns)
• UPDATE customers
• SET name = (SELECT supplier_name
• FROM suppliers
• WHERE suppliers.supplier_name = c
ustomers.name)
• WHERE age < 25;
Oracle DELETE Statement
• DELETE FROM customers
• WHERE name = 'Sohan';
• This statement will delete all records from the
customer table where name is "Sohan".
Oracle Delete Example: On multiple
condition
• DELETE FROM customers
• WHERE last_name = 'Maurya'
• 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.