Manipulating Data -DML (INSERT, UPDATE, DELECT, MERGE)
DML Manipulation Language
A transaction consists of a collection of DML statements that form a logical unit of work. Every table has INSERT, UPDATE and DELETE privileges associated with it. These privileges are automatically granted to the creator of the table, but in general explicitly granted to others.
INSERT Adds new rows - one row at a time
use column list for clarity and no single quotes for numeric values - SYSDATE is a function so does not require single quotes , must consider constraints.
1. The basic INSERT statement - insert a new row containing values for each column INSERT INTO employees (employee_id, first_name, last_name, hire_date) VALUES (124, 'James', 'Wells', SYSDATE); The basic INSERT statement without using ANY of the column names
- column list is not needed, but you must list the values in the correct column order and a value must be declared for each column -use ' ' for blank CHAR and DATES and NULL for numbers
INSERT INTO employees VALUES (124, 'James', ' ', NULL); 2. The basic INSERT statement listing only those columns with data to be inserted
-this is the implicit way to have NULL values entered in those columns not listed -make sure the column can have a NULL value
INSERT INTO employees (employee_id, last_name) VALUES (124, 'Wells'); 4. INSERT with Specific Date Values INSERT INTO employees (employee_id, first_name, last_name, department_id, hire_date) VALUES (125, ' ', Jones, NULL, TO_DATE('February 19, 1989', 'Month DD, RRRR');
If the date must be entered in a format other than the default DD-MON-RR, you must use the TO_DATE datatype conversion. Also, if you enter '04-APR-98' only, the year is interpreted to be 2098.
5. INSERT INTO Using a Subquery in Place of the Table Name INSERT INTO
(SELECT employee_id, last_name, email, hire_date, job_id, salary department_id FROM employees WHERE department_id = 50 WITH CHECK OPTION) VALUES ( 99999, "Taylor', 'DTAYLOR', TO_DATE('07-JUN-99', 'DD-MON-RR'), 'ST_CLERK', 5000, 50);
**check option prohibits changes to rows that are not in the subquery. If department_id was left out of the SELECT list, an error occurs because NULL would be entered. It also keeps users of VIEWS from updating rows they don't have permission to SELECT.
6. INSERTING ROWS FROM ANOTHER TABLE USING A SUBQUERY INSERT INTO sales (id, name, salary, commission_pct) **NO VALUES CLAUSE (SELECT employee_id, salary, commission_pct FROM employees WHERE job_id LIKE '%REP%'); INSERT INTO copy_emp SELECT * FROM employees 7. CREATING A SCRIPT THAT PROMPTS THE USER FOR VALUES TO INSERT INSERT INTO departments (department_id, department_name, location_id) VALUES (&department_id, &department_name, &location_id);