|
Home >> FAQs/Tutorials >> Oracle DBA FAQ
Oracle DBA FAQ - Understanding SQL DML Statements
By: FYIcenter.com
Part:
1
2
3
(Continued from previous part...)
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
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
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
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.
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.
(Continued on next part...)
Part:
1
2
3
|