SQL - DML
SQL - DML
1
Data Manipulation Language
• When you want to add, update, or delete data in the database, you
execute a DML statement.
2
The Insert Statement
• Add new rows to a table by using the INSERT statement.
• Only one row is inserted at a time.
4
Insert multiple records
• Use the following syntax in order to insert multiple records within one
statement:
INSERT ALL
INTO productlines (productline, textdescription) VALUES('Classic Bikes','At least 25 years old’)
INTO productlines (productline, textdescription) VALUES('Vintage Bike', 'Customized and old’)
SELECT * FROM dual;
5
Inserting Special Values
• Insert special values by using SQL functions
• The SYSDATE function records the current date and time.
• The USER function records the current username.
6
Copying Rows from Another Table
• Write your INSERT statement with a subquery.
• Use subquery in place of VALUES.
• Match the number of columns and data types in the INSERT clause to those in the
subquery.
UPDATE dbs211_managers
SET reportsto = DEFAULT
WHERE employeenumber = 36;
8
The UPDATE statement
• You can modify existing rows by using the UPDATE statement.
• You can update more than one row at a time.
• Use the primary key to identify a single row.
• Use WHERE clause to update specific row(s).
UPDATE dbs211_customers
SET firstname = 'Farhad’,
lastname = 'Pourhadi’
WHERE customernumber = 103;
UPDATE dbs211_managers
SET reportsto = 1; 9
Integrity Constraint Error
• If you attempt to update a record with a value that is tied to an integrity constraint,
you will get an error.
UPDATE dbs211_customers
SET salesrepemployeenumber = 1703
WHERE customernumber = 103;
Error report -
ORA-02291: integrity constraint (SYSTEM.CUST_SALESREP_FK) violated - parent key not found
10
The DELETE Statement
• You can remove existing rows by using the DELETE statement.
• Delete specific rows by specifying the WHERE clause.
11
Integrity Constraint Error
• If you attempt to delete a record with a value that is tied to an integrity
constraint, you will get an error.
Error report -
ORA-02292: integrity constraint (SYSTEM.EMP_RTEMP_FK) violated - child record found
12