5.1 Lectures DML and DDL
5.1 Lectures DML and DDL
5.1 Lectures DML and DDL
MSIT 2K23
Contents
• Transforming Data
3
Data Transforming: IF
• The IF() function returns a value if a condition is
TRUE, or another value if a condition is FALSE.
Data Transforming: Case
• The CASE statement goes through conditions
and return a value when the first condition is
met (like an IF-THEN-ELSE statement). So, once
a condition is true, it will stop reading and return
the result.
• If no conditions are true, it will return the value
in the ELSE clause.
• If there is no ELSE part and no conditions are
true, it returns NULL.
Data Transforming: Case
Data Transforming: IFNULL
• The IFNULL() function returns a specified value if
the expression is NULL.
• If the expression is NOT NULL, this function
returns the expression.
Data Transforming: IFNULL
• The IFNULL() function returns a specified value if
the expression is NULL.
• If the expression is NOT NULL, this function
returns the expression.
Data Transforming: NULLIF
• The NULLIF() function compares two expressions
and returns NULL if they are equal. Otherwise,
the first expression is returned.
Data Transforming: NULLIF
• The NULLIF() function compares two expressions
and returns NULL if they are equal. Otherwise,
the first expression is returned.
Data Transforming: COALESCE()
• The COALESCE() function returns the first non-
null value in a list.
Creating Data Storage Objects
12
Database Objects
Object Description
Table Basic unit of storage; composed of rows
and columns
• You specify:
• Table name
• Column name, column data type, and column size
Referencing Another User’s Tables
DESCRIBE dept
Tables in the Oracle Database
• User Tables:
• Are a collection of tables created and maintained by
the user
• Contain user information
• Data Dictionary:
• Is a collection of tables created and maintained by
the Oracle Server
• Contain database information
Querying the Data Dictionary
“Add a new
column to
the DEPT80
table.”
DEPT80
Adding a Column
• You use the ADD clause to add columns.
ALTER TABLE dept80
ADD (job_id VARCHAR2(9));
Table altered.
31
Data Manipulation Language
4 rows created.
UPDATE copy_emp
SET department_id = 110;
22 rows updated.
Problem
Update employee 114’s job and salary to match that of employee 205.
Updating Two Columns with a
Subquery
Update employee 114’s job and salary to match that of
employee 205.
UPDATE employees
SET job_id = (SELECT job_id
FROM employees
WHERE employee_id = 205),
salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 114;
1 row updated.
Problem
Updates the COPY_EMP table based on the values from the EMPLOYEES table. It
changes the department number of all employees with employee 200’s job ID to
employee 100’s current department number.
Updating Rows Based
on Another Table
Use subqueries in UPDATE statements to update
rows in a table based on values from another table.
UPDATE copy_emp
SET department_id = (SELECT department_id
FROM employees
WHERE employee_id = 100)
WHERE job_id = (SELECT job_id
FROM employees
WHERE employee_id = 200);
1 row updated.
Updating Rows:
Integrity Constraint Error
UPDATE employees
SET department_id = 55
WHERE department_id = 110;
UPDATE employees
*
ERROR at line 1:
ORA-02291: integrity constraint (HR.EMP_DEPT_FK)
violated - parent key not found
Removing a Row from a Table
DEPARTMENTS