Chapter 5. Data Manipulation

Table of Contents
5.1. Inserting Data
5.2. Updating Data
5.3. Deleting Data

The previous chapter discussed how to create tables and other structures to hold your data. Now it is time to fill the tables with data. This chapter covers how to insert, update, and delete table data. We will also introduce ways to effect automatic data changes when certain events occur: triggers and rewrite rules. The chapter after this will finally explain how to extract your long-lost data back out of the database.

5.1. Inserting Data

When a table is created, it contains no data. The first thing to do before a database can be of much use is to insert data. Data is conceptually inserted one row at a time. Of course you can also insert more than one row, but there is no way to insert less than one row at a time. Even if you know only some column values, a complete row must be created.

To create a new row, use the INSERT command. The command requires the table name and a value for each of the columns of the table. For example, consider the emp table from Chapter 4:

CREATE TABLE emp (
    empno           NUMBER(4) NOT NULL CONSTRAINT emp_pk PRIMARY KEY,
    ename           VARCHAR2(10),
    job             VARCHAR2(9),
    mgr             NUMBER(4),
    hiredate        DATE,
    sal             NUMBER(7,2) CONSTRAINT emp_sal_ck CHECK (sal > 0),
    comm            NUMBER(7,2),
    deptno          NUMBER(2) CONSTRAINT emp_ref_dept_fk
                        REFERENCES dept(deptno)
);

An example command to insert a row would be:

INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,'17-DEC-80',800,NULL,20);

The data values are listed in the order in which the columns appear in the table, separated by commas. Usually, the data values will be literals (constants), but scalar expressions are also allowed.

The above syntax has the drawback that you need to know the order of the columns in the table. To avoid that you can also list the columns explicitly. For example, both of the following commands have the same effect as the one above:

INSERT INTO emp (empno, ename, job, mgr, hiredate, sal, deptno) VALUES (7369,'SMITH','CLERK',7902,'17-DEC-80',800,20);
INSERT INTO emp (ename, empno, mgr, job, sal, comm, deptno, hiredate) VALUES('SMITH',7369,7902,'CLERK',800,NULL,20,'17-DEC-80');

Many users consider it good practice to always list the column names.

If you don't have values for all the columns, you can omit some of them. In that case, the columns will be filled with their default values.

For clarity, you can also request default values explicitly, for individual columns or for the entire row given that none of the default values for any of the columns in that table have a not null constraints on them.

INSERT INTO emp (ename, empno, mgr, job, sal, comm, deptno, hiredate) VALUES('SMITH',7369,7902,'CLERK',800,DEFAULT,20,'17-DEC-80');

Tip: To do "bulk loads", that is, inserting a lot of data, take a look at the COPY command. It is not as flexible as the INSERT command, but is more efficient.