Sunteți pe pagina 1din 17

dml

insert,update,delete

https://www.techonthenet.com
• Mampu menjelaskan dan menggunakan perintah Insert, Update, dan
Delete.

https://www.techonthenet.com
#insert

https://www.techonthenet.com
Insert
• The Oracle INSERT statement is used to insert a single
record or multiple records into a table in Oracle.
• INSERT INTO table (column1, column2, ... column_n )
VALUES (expression1, expression2, ... expression_n );
• INSERT INTO table (column1, column2, ... column_n )
SELECT expression1, expression2, ... expression_n
FROM source_table [WHERE conditions];

https://www.techonthenet.com
• INSERT INTO suppliers (supplier_id, supplier_name)
VALUES (5000, 'Apple');
• INSERT INTO suppliers (supplier_id, supplier_name)
SELECT account_no, name FROM customers WHERE
customer_id > 5000;
• INSERT ALL INTO suppliers (supplier_id,
supplier_name) VALUES (1000, 'IBM')
INTO suppliers (supplier_id, supplier_name)
VALUES (2000, 'Microsoft')
INTO suppliers (supplier_id, supplier_name) VALUES
(3000, 'Google') SELECT * FROM dual;

https://www.techonthenet.com
Update
• The Oracle UPDATE statement is used to update
existing records in a table in an Oracle database.
There are 2 syntaxes for an update query in Oracle
depending on whether you are performing a
traditional update or updating one table with data
from another table.

https://www.techonthenet.com
#syntax
• UPDATE table SET column1 = expression1, column2 = expression2, ...
column_n = expression_n [WHERE conditions];

• UPDATE table1 SET column1 = (SELECT expression1 FROM table2


WHERE conditions) [WHERE conditions];

https://www.techonthenet.com
#example
• UPDATE customers SET last_name = 'Anderson' WHERE customer_id =
5000;

• UPDATE customers SET state = 'California', customer_rep = 32 WHERE


customer_id > 100;

• UPDATE customers SET c_details = (SELECT contract_date FROM


suppliers WHERE suppliers.supplier_name =
customers.customer_name) WHERE customer_id < 1000;

https://www.techonthenet.com
2#example
• UPDATE suppliers SET supplier_name = (SELECT
customers.customer_name FROM customers WHERE
customers.customer_id = suppliers.supplier_id) WHERE EXISTS
(SELECT customers.customer_name FROM customers WHERE
customers.customer_id = suppliers.supplier_id);

https://www.techonthenet.com
CREATE TABLE suppliers ( supplier_id number(10) not
null, supplier_name varchar2(50) not null, city
varchar2(50), CONSTRAINT suppliers_pk PRIMARY KEY
(supplier_id) );
INSERT INTO suppliers (supplier_id, supplier_name,
city) VALUES (5001, 'Microsoft', 'Chicago');
INSERT INTO suppliers (supplier_id, supplier_name,
city) VALUES (5002, 'IBM', 'Chicago');
INSERT INTO suppliers (supplier_id, supplier_name,
city) VALUES (5003, 'Red Hat', 'Detroit');
INSERT INTO suppliers (supplier_id, supplier_name,
city) VALUES (5004, 'NVIDIA', 'New York');

https://www.techonthenet.com
Lakukan Update
• UPDATE suppliers SET city = 'San Francisco' WHERE supplier_name =
'IBM';

https://www.techonthenet.com
delete
• The Oracle DELETE statement is used to delete a single record or
multiple records from a table in Oracle.

• DELETE FROM table [WHERE conditions];

https://www.techonthenet.com
#example
• DELETE FROM customers WHERE last_name = 'Smith';
• DELETE FROM customers WHERE last_name = 'Anderson' AND
customer_id > 25;

https://www.techonthenet.com
#example
• DELETE FROM suppliers WHERE EXISTS ( SELECT
customers.customer_name FROM customers WHERE
customers.customer_id = suppliers.supplier_id AND customer_id > 25
);
• DELETE FROM TableA WHERE NOT EXISTS ( SELECT * FROM TableB
WHERE TableA.field1 = TableB.fieldx AND TableA.field2 = TableB.fieldz
);

https://www.techonthenet.com
between
• DELETE FROM contacts WHERE contact_id >= 5000 AND contact_id <
6000.

• DELETE FROM contacts WHERE contact_id BETWEEN 5000 AND 5999;

https://www.techonthenet.com
truncate
• The TRUNCATE TABLE statement is used to remove all records from a
table in Oracle. It performs the same function as a DELETE statement
without a WHERE clause.
• Warning: If you truncate a table, the TRUNCATE TABLE statement can
not be rolled back.
• TRUNCATE TABLE customers;

https://www.techonthenet.com

S-ar putea să vă placă și