Adding rows to a table

In Chapter 3, you learned to use the Sybase Central table editor to update a table. Now let's use the INSERT statement to add data about a new department.

Suppose that a new eastern sales department is created, with the same manager as the current Sales department. You can add this information to the database using the following INSERT statement in Interactive SQL:

INSERT
INTO department ( dept_id, dept_name, dept_head_id )
VALUES ( 220, 'Eastern Sales', 902 )

If you make a mistake and forget to specify one of the columns, Sybase IQ reports the following error:

Error at line 1
The insert VALUES do not match the column list.

NoteCase sensitivity may matter when inserting values.

The NULL value is a special value used to indicate that something is either not known or not applicable. Some columns are allowed to contain the NULL value, and others are not.

To check whether a column in a table allows the NULL value, use the sp_iqcolumn stored procedure in Interactive SQL. For example, type the following:

sp_iqcolumn employee
Shown is Sybase Central screen with an example of using the insert statement to add data

In the nulls column, a “Y” value indicates that nulls are allowed.

A short form for INSERT

You can use a short form to enter values for all the columns in a table in the order they appear when you SELECT * from the table (the order in which they were created). The following is equivalent to the previous INSERT command:

INSERT
INTO department
VALUES ( 220, 'Eastern Sales', 902 )

NoteYou should use this form of INSERT with caution; it will not work as expected if you ever change the order of the columns in the table or if you add or remove a column from the table.