Based on the SQLD exam syllabusReport an error
INSERT (add), UPDATE (modify), DELETE (remove), and MERGE (update if it exists, insert if it does not).VALUES (…, DEFAULT).DELETE is DML, so it can be rolled back. TRUNCATE is DDL, so it cannot be undone, and it keeps the structure. DROP removes the structure too.MERGE INTO target USING source ON (condition) handles rows that match the condition with WHEN MATCHED THEN UPDATE and rows that do not match with WHEN NOT MATCHED THEN INSERT.The DML examples do not change the original EMP and DEPT. In each block, we make a copy with CREATE TABLE … AS SELECT and drop it at the end with DROP TABLE … PURGE.
Table created. and the last line is Table dropped.SET FEEDBACK ON). It prints the number of rows processed, such as 1 row created. or 3 rows updated.. Blank lines between statements were omitted.INSERT INTO table [(column1, column2, …)] VALUES (value1, value2, …);
INSERT INTO table [(column1, column2, …)] SELECT …;CREATE TABLE T_DEPT AS SELECT * FROM DEPT;
INSERT INTO T_DEPT (DEPTNO, DNAME, LOC) VALUES (50, 'EDUCATION', 'SEOUL');
INSERT INTO T_DEPT VALUES (60, 'IT', NULL);
INSERT INTO T_DEPT (DEPTNO, DNAME) VALUES (70, 'HR');
SELECT * FROM T_DEPT ORDER BY DEPTNO;
DROP TABLE T_DEPT PURGE;Table created.
1 row created.
1 row created.
1 row created.
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 EDUCATION SEOUL
60 IT
70 HR
7 rows selected.
Table dropped.| How you write it | Rule |
|---|---|
| With a column list (50) | Give values in the order of the columns you listed. It may differ from the table's definition order |
| Without a column list (60) | Give values for all columns in definition order. Write NULL explicitly for any column you want to leave empty |
| Only some columns listed (70) | The omitted LOC gets its DEFAULT value, or NULL if there is none |
If you define a DEFAULT on a column, that value goes in when an INSERT gives no value for it.
CREATE TABLE T_DEPT (DEPTNO NUMBER(2), DNAME VARCHAR2(14), LOC VARCHAR2(13) DEFAULT 'SEOUL');
INSERT INTO T_DEPT (DEPTNO, DNAME) VALUES (50, 'EDUCATION');
INSERT INTO T_DEPT VALUES (60, 'IT', DEFAULT);
INSERT INTO T_DEPT VALUES (70, 'HR', NULL);
INSERT INTO T_DEPT VALUES (80, 'SUPPORT');
INSERT INTO T_DEPT (DEPTNO, DNAME) VALUES (90, 'SALES', 'BUSAN');
SELECT * FROM T_DEPT ORDER BY DEPTNO;
DROP TABLE T_DEPT PURGE;Table created.
1 row created.
1 row created.
1 row created.
INSERT INTO T_DEPT VALUES (80, 'SUPPORT')
*
ERROR at line 1:
ORA-00947: not enough values
INSERT INTO T_DEPT (DEPTNO, DNAME) VALUES (90, 'SALES', 'BUSAN')
*
ERROR at line 1:
ORA-00913: too many values
DEPTNO DNAME LOC
---------- -------------- -------------
50 EDUCATION SEOUL
60 IT SEOUL
70 HR
3 rows selected.
Table dropped.DEFAULT keyword, so both received the default SEOUL.If you use SELECT instead of VALUES, the entire query result goes in at once. Do not use VALUES or parentheses.
CREATE TABLE EMP_T AS SELECT EMPNO, ENAME, SAL FROM EMP WHERE 1 = 2;
INSERT INTO EMP_T SELECT EMPNO, ENAME, SAL FROM EMP WHERE DEPTNO = 10;
INSERT INTO EMP_T (EMPNO, ENAME) SELECT EMPNO, ENAME FROM EMP WHERE JOB = 'ANALYST';
SELECT * FROM EMP_T ORDER BY EMPNO;
DROP TABLE EMP_T PURGE;Table created.
3 rows created.
2 rows created.
EMPNO ENAME SAL
---------- ---------- ----------
7782 CLARK 2450
7788 SCOTT
7839 KING 5000
7902 FORD
7934 MILLER 1300
5 rows selected.
Table dropped.WHERE 1 = 2 is always false, so it creates an empty table: the structure is copied, but no rows.3 rows created.UPDATE table SET column1 = value1 [, column2 = value2 …] [WHERE condition];CREATE TABLE EMP_T AS SELECT * FROM EMP;
UPDATE EMP_T SET SAL = SAL * 1.1, COMM = 100 WHERE DEPTNO = 10;
UPDATE EMP_T SET SAL = (SELECT MAX(SAL) FROM EMP WHERE DEPTNO = 20) WHERE ENAME = 'SMITH';
UPDATE EMP_T SET (JOB, SAL) = (SELECT JOB, SAL FROM EMP WHERE ENAME = 'SCOTT') WHERE ENAME = 'ADAMS';
UPDATE EMP_T SET COMM = 0 WHERE ENAME = 'NOBODY';
SELECT ENAME, JOB, SAL, COMM FROM EMP_T WHERE DEPTNO IN (10, 20) ORDER BY DEPTNO, ENAME;
UPDATE EMP_T SET COMM = NULL;
DROP TABLE EMP_T PURGE;Table created.
3 rows updated.
1 row updated.
1 row updated.
0 rows updated.
ENAME JOB SAL COMM
---------- --------- ---------- ----------
CLARK MANAGER 2695 100
KING PRESIDENT 5500 100
MILLER CLERK 1430 100
ADAMS ANALYST 3000
FORD ANALYST 3000
JONES MANAGER 2975
SCOTT ANALYST 3000
SMITH CLERK 3000
8 rows selected.
14 rows updated.
Table dropped.SAL = SAL * 1.1 is the value before the change.SET (JOB, SAL) = (SELECT JOB, SAL …), changes several columns with a single subquery.0 rows updated.DELETE FROM table [WHERE condition]; removes the rows that match the condition. In Oracle you may omit FROM.
CREATE TABLE EMP_T AS SELECT * FROM EMP;
DELETE FROM EMP_T WHERE DEPTNO = 30;
SELECT COUNT(*) FROM EMP_T;
ROLLBACK;
SELECT COUNT(*) FROM EMP_T;
DELETE EMP_T;
TRUNCATE TABLE EMP_T;
ROLLBACK;
SELECT COUNT(*) FROM EMP_T;
DROP TABLE EMP_T PURGE;
SELECT COUNT(*) FROM EMP_T;Table created.
6 rows deleted.
COUNT(*)
----------
8
1 row selected.
Rollback complete.
COUNT(*)
----------
14
1 row selected.
14 rows deleted.
Table truncated.
Rollback complete.
COUNT(*)
----------
0
1 row selected.
Table dropped.
SELECT COUNT(*) FROM EMP_T
*
ERROR at line 1:
ORA-00942: table or view "SCOTT"."EMP_T" does not existDELETE EMP_T was committed along with it, so even after ROLLBACK there are 0 rows."SCOTT"."EMP_T" notation in the error message is the 23ai format; earlier versions show only table or view does not exist.| Aspect | DELETE | TRUNCATE | DROP |
|---|---|---|---|
| Type | DML | DDL | DDL |
| What it removes | Rows matching the condition (WHERE allowed) | All rows (no WHERE) | Rows + table structure |
| ROLLBACK | Possible (before COMMIT) | Not possible (auto-commit) | Not possible (auto-commit) |
| Table structure | Remains | Remains | Gone |
| Storage space | Left as is | Returned to initial size | Returned |
| Speed | Slow, because it logs each row | Fast | Fast |
BEGIN TRAN can be undone with ROLLBACK. The "not possible" in the table above is for Oracle.⚠️ Trap: If the requirement is "delete all rows but be able to undo it," the answer is a DELETE without WHERE, not TRUNCATE. If it is "empty it quickly but keep the structure," it is TRUNCATE; if "the table is no longer needed," it is DROP.
When you reflect source data into a target table, you sometimes need to UPDATE rows that already exist and INSERT rows that do not. Instead of writing two separate statements, you handle it with a single MERGE statement.
MERGE INTO target_table alias
USING source (table, view, or subquery) alias
ON (condition joining target and source)
WHEN MATCHED THEN
UPDATE SET column = value [, …]
WHEN NOT MATCHED THEN
INSERT (column, …) VALUES (value, …);T_BONUS holds CLARK, KING, and MILLER from department 10 with a bonus of 0. We MERGE the 4 MANAGER and PRESIDENT employees (JONES, BLAKE, CLARK, KING) in as the source.
CREATE TABLE T_BONUS AS SELECT EMPNO, ENAME, SAL AS BONUS FROM EMP WHERE DEPTNO = 10;
UPDATE T_BONUS SET BONUS = 0;
MERGE INTO T_BONUS B USING (SELECT EMPNO, ENAME, SAL FROM EMP WHERE JOB IN ('MANAGER', 'PRESIDENT')) E ON (B.EMPNO = E.EMPNO) WHEN MATCHED THEN UPDATE SET B.BONUS = E.SAL * 0.1 WHEN NOT MATCHED THEN INSERT (EMPNO, ENAME, BONUS) VALUES (E.EMPNO, E.ENAME, E.SAL * 0.05);
SELECT * FROM T_BONUS ORDER BY EMPNO;
DROP TABLE T_BONUS PURGE;Table created.
3 rows updated.
4 rows merged.
EMPNO ENAME BONUS
---------- ---------- ----------
7566 JONES 148.75
7698 BLAKE 142.5
7782 CLARK 245
7839 KING 500
7934 MILLER 0
5 rows selected.
Table dropped.| Source row | ON result | Action | Bonus |
|---|---|---|---|
| CLARK, KING | Exists in the target (MATCHED) | UPDATE | 10% of salary |
| JONES, BLAKE | Not in the target (NOT MATCHED) | INSERT | 5% of salary |
| (MILLER, not in the source) | Not in the source | Left alone | Still 0 |
4 rows merged. is the sum of 2 UPDATE rows and 2 INSERT rows.UPDATE SET … and INSERT (…) VALUES (…).CREATE TABLE T_BONUS AS SELECT EMPNO, ENAME, SAL AS BONUS FROM EMP WHERE DEPTNO = 10;
MERGE INTO T_BONUS B USING (SELECT EMPNO, SAL FROM EMP WHERE DEPTNO = 10) E ON (B.EMPNO = E.EMPNO) WHEN MATCHED THEN UPDATE SET B.EMPNO = E.EMPNO;
MERGE INTO T_BONUS B USING (SELECT EMPNO, ENAME FROM EMP WHERE DEPTNO = 20) E ON (B.EMPNO = E.EMPNO) WHEN NOT MATCHED THEN INSERT (EMPNO, ENAME, BONUS) VALUES (E.EMPNO, E.ENAME, 0);
SELECT COUNT(*) FROM T_BONUS;
DROP TABLE T_BONUS PURGE;Table created.
MERGE INTO T_BONUS B USING (SELECT EMPNO, SAL FROM EMP WHERE DEPTNO = 10) E ON (B.EMPNO = E.EMPNO) WHEN MATCHED THEN UPDATE SET B.EMPNO = E.EMPNO
*
ERROR at line 1:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "B"."EMPNO"
5 rows merged.
COUNT(*)
----------
8
1 row selected.
Table dropped.UPDATE SET … WHERE condition and DELETE WHERE condition to the MATCHED clause.Tomorrow we cover TCL (COMMIT, ROLLBACK, SAVEPOINT), which confirms or undoes DML changes, and transactions.
VALUES.Click a choice to reveal the answer and explanation.
Question 1
You ran INSERT INTO T_DEPT (DEPTNO, DNAME) VALUES (50, 'EDUCATION'); on a table created with CREATE TABLE T_DEPT (DEPTNO NUMBER(2), DNAME VARCHAR2(14), LOC VARCHAR2(13) DEFAULT 'SEOUL');. What is the LOC value of this row?
Question 2
What is the result of running INSERT INTO T_DEPT VALUES (80, 'SUPPORT'); on T_DEPT(DEPTNO, DNAME, LOC), a copy of DEPT?
Question 3
Which statement about Oracle's DELETE, TRUNCATE, and DROP is correct?
Question 4
T_BONUS has 3 rows: CLARK, KING, and MILLER. After running MERGE INTO T_BONUS B USING (…) E ON (B.EMPNO = E.EMPNO) WHEN MATCHED THEN UPDATE SET … WHEN NOT MATCHED THEN INSERT … with a source of 4 rows (JONES, BLAKE, CLARK, KING), how many rows does T_BONUS have?
Question 5
You ran UPDATE EMP_T SET COMM = 0 WHERE ENAME = 'NOBODY'; on EMP_T, a copy of EMP. No employee is named NOBODY. What is the result in SQL*Plus?