Understanding SQL DML Statements

1. What Are DML Statements?

DML (Data Manipulation Language) statements are statements to change data values in database tables. The are 3 primary DML statements:

◉ INSERT - Inserting new rows into database tables.
◉ UPDATE - Updating existing rows in database tables .
◉ DELETE - Deleting existing rows from database tables.

2. How To Create a Testing Table?

If you want to practice DML statements, you should create a testing table as shown in the script below:

CREATE TABLE fyi_links (id NUMBER(4) PRIMARY KEY,
  url VARCHAR2(80) NOT NULL,
  notes VARCHAR2(1024),
  counts NUMBER,
  created DATE DEFAULT (sysdate));
Table created.

You should keep this table for to practice other tutorial exercises presented in this collection.

3. How To Set Up SQL*Plus Output Format?

If you want to practice SQL statements with SQL*Plus, you need to set up your SQL*Plus output formatting parameter properly. The following SQL*Plus commands shows you some examples:

COLUMN id FORMAT 9999;
COLUMN url FORMAT A24;
COLUMN notes FORMAT A12;
COLUMN counts FORMAT 999999;
SET NULL 'NULL';

4. How To Insert a New Row into a Table?

To insert a new row into a table, you should use the INSERT INTO statement with values specified for all columns as shown in the following example:

INSERT INTO fyi_links VALUES (101,
  'http://dev.fyicenter.com',
  NULL,
  0,
  '30-Apr-2006');
1 row created.

SELECT * FROM fyi_links;

   ID URL                                NOTES COUNTS CREATED
----- ------------------------      -------- ------- ---------
  101 http://dev.fyicenter.com NULL      0     30-Apr-06

5. How To Specify Default Values in INSERT Statement?

If a column is defined with a default value in a table, you can use the key word DEFAULT in the INSERT statement to take the default value for that column. The following tutorial exercise gives a good example:

INSERT INTO fyi_links VALUES (102,
  'http://dba.fyicenter.com',
  NULL,
  0,
  DEFAULT);
1 row created.

SELECT * FROM fyi_links;

   ID URL                             NOTES     COUNTS CREATED
----- ------------------------     -------- ------- ---------
  101 http://dev.fyicenter.com NULL        0   30-Apr-06
  102 http://dba.fyicenter.com NULL        0   07-MAY-06

6. How To Omit Columns with Default Values in INSERT Statement?

If you don't want to specify values for columns that have default values, or you want to specify values to columns in an order different than how they are defined, you can provide a column list in the INSERT statement. If a column is omitted in the column, Oracle applies 3 rules:

◉ If default value is defined for the column, that default value will be used.
◉ If no default value is defined for the column and NULL is allowed, NULL will be used.
◉ If no default value is defined for the column and NULL is not allowed, error will be returned.

The following tutorial exercise gives a good example:

INSERT INTO fyi_links (url, id)
  VALUES ('http://sqa.fyicenter.com', 103);
1 row created.

SELECT * FROM fyi_links;
   ID URL                           NOTES    COUNTS CREATED
----- ------------------------   -------- ------- ---------
  101 http://dev.fyicenter.com NULL        0     30-Apr-06
  102 http://dba.fyicenter.com NULL        0     07-MAY-06
  103 http://sqa.fyicenter.com NULL     NULL    07-MAY-06

7. How To Insert Multiple Rows with One INSERT Statement?

If you want to insert multiple rows with a single INSERT statement, you can use a subquery instead of the VALUES clause. Rows returned from the subquery will be inserted the target table.

The following tutorial exercise gives a good example:

INSERT INTO fyi_links
  SELECT department_id, department_name||'.com', NULL, NULL,
    SYSDATE FROM departments WHERE department_id >= 250;
3 row created.

SELECT * FROM fyi_links;

   ID URL                         NOTES    COUNTS CREATED
----- ------------------------ -------- ------- ---------
  101 http://dev.fyicenter.com NULL        0    30-Apr-06
  102 http://dba.fyicenter.com NULL        0    07-MAY-06
  103 http://sqa.fyicenter.com NULL    NULL   07-MAY-06 
  250 Retail Sales.com           NULL     NULL    07-MAY-06
  260 Recruiting.com             NULL     NULL    07-MAY-06
  270 Payroll.com                  NULL     NULL     07-MAY-06

8. How To Update Values in a Table?

If you want to update some values in one row or multiple rows in a table, you can use the UPDATE statement. The script below shows a good example:

UPDATE fyi_links SET counts = 999, notes = 'Good site.'
  WHERE id = 101;
1 row updated.

SELECT * FROM fyi_links WHERE id = 101;

  ID URL                           NOTES      COUNTS CREATED
---- ------------------------   ---------- ------ ---------
 101 http://dev.fyicenter.com Good site. 999   07-MAY-06

9. How To Update Values on Multiple Rows?

If the WHERE clause in an UPDATE matches multiple rows, the SET clause will be applied to all matched rows. This rule allows you to update values on multiple rows in a single UPDATE statement. Here is a good example:

UPDATE fyi_links SET counts = 9, notes = 'Wrong URL'
  WHERE id >= 250;
3 rows updated.

SELECT * FROM fyi_links WHERE id >= 250;

   ID URL                      NOTES      COUNTS CREATED
----- -------------------- ------------ ------- ---------
  250 Retail Sales.com     Wrong URL     9   07-MAY-06
  260 Recruiting.com       Wrong URL     9   07-MAY-06
  270 Payroll.com            Wrong URL     9   07-MAY-06

This statement updated 3 rows with the same new values on all 3 rows.

10. How To Use Existing Values in UPDATE Statements?

If a row matches the WHERE clause in a UPDATE statement, existing values in this row can be used in expressions to provide new values in the SET clause. Existing values are represented by columns in the expressions. The tutorial exercise below shows a good example:

UPDATE fyi_links SET id = 1000 + id, counts = id*2
  WHERE id >= 250;
3 rows updated.

SELECT * FROM fyi_links WHERE id >= 250;

   ID URL                      NOTES       COUNTS CREATED
----- -------------------- ------------ ------- ---------
 1250 Retail Sales.com  Wrong URL     500   07-MAY-06
 1260 Recruiting.com    Wrong URL     520   07-MAY-06
 1270 Payroll.com         Wrong URL     540   07-MAY-06

This statement increased values in the id column by 1000.

11. How To Use Values from Other Tables in UPDATE Statements?

If you want to update values in one with values from another table, you can use a subquery in the SET clause. The subquery should return only one row for each row in the update table that matches the WHERE clause. The tutorial exercise below shows a good example:

UPDATE fyi_links SET (notes, created) =
  (SELECT last_name, hire_date FROM employees
    WHERE employee_id = id)
  WHERE id < 110;
3 rows updated.

SELECT * FROM fyi_links WHERE id < 110;

  ID URL                          NOTES   COUNTS CREATED
---- ------------------------  --------- ------- ---------
 101 http://dev.fyicenter.com Kochhar  999    21-SEP-89
 102 http://dba.fyicenter.com De Haan  0        13-JAN-93
 103 http://sqa.fyicenter.com Hunold    NULL 03-JAN-90

12. What Happens If the UPDATE Subquery Returns Multiple Rows?

If a subquery is used in a UPDATE statement, it must return exactly one row for each row in the update table that matches the WHERE clause. If it returns multiple rows, Oracle server will give you an error message. To test this out, you can try the following tutorial exercise:

UPDATE fyi_links SET (notes, created) =
  (SELECT last_name, hire_date FROM employees
    WHERE employee_id < id)
  WHERE id < 110;
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row

The problem is the criteria in the subquery: "employee_id < id"

13. How To Delete an Existing Row from a Table?

If you want to delete an existing row from a table, you can use the DELETE statement with a WHERE clause to identify that row. Here is good sample of DELETE statements:

INSERT INTO fyi_links (url, id)
  VALUES ('http://www.myspace.com', 301);
1 row created.

SELECT * FROM fyi_links WHERE id = 301;

   ID URL                         NOTES   COUNTS CREATED
----- ------------------------ -------- ------- ---------
  301 http://www.myspace.com  NULL   NULL  07-MAY-06 

DELETE FROM fyi_links WHERE id = 301;
1 row deleted.

SELECT * FROM fyi_links WHERE id = 301;
no rows selected

14. How To Delete Multiple Rows from a Table?

You can delete multiple rows from a table in the same way as deleting a single row, except that the WHERE clause will match multiple rows. The tutorial exercise below deletes 3 rows from the fyi_links table:

SELECT * FROM fyi_links WHERE id >= 250;

   ID URL                      NOTES     COUNTS CREATED
----- --------------------- ----------- ------- ---------
 1250 Retail Sales.com    Wrong URL     500  07-MAY-06
 1260 Recruiting.com      Wrong URL     520  07-MAY-06
 1270 Payroll.com           Wrong URL     540  07-MAY-06

DELETE FROM fyi_links WHERE id >= 250;
3 row deleted.

SELECT * FROM fyi_links WHERE id >= 250;
no rows selected

15. How To Delete All Rows from a Table?

If you want to delete all rows from a table, you have two options:

◉ Use the DELETE statement with no WHERE clause.
◉ Use the TRUNCATE TABLE statement.

The TRUNCATE statement is more efficient the DELETE statement. The tutorial exercise shows you a good example of TRUNCATE statement:

SELECT COUNT(*) FROM fyi_links;
  COUNT(*)
----------
         3

TRUNCATE TABLE fyi_links;
Table truncated.

SELECT COUNT(*) FROM fyi_links;
  COUNT(*)
----------
         0

0 comments:

Post a Comment