Sei sulla pagina 1di 1

50 Chapter 2: Inserting

2.2.1 INSERT All Values


The simplest form of the INSERT statement is the one where you specify values
for each and every column in a single row.
<insert_all_values> ::= INSERT [ INTO ]
[ <owner_name> "." ] <target_table_name>
[ <on_existing> ]
VALUES "(" <all_values_list> ")"
<owner_name> ::= <identifier>
<target_table_name> ::= <identifier>
<identifier> ::= see <identifier> in Chapter 1, Creating
<on_existing> ::= ON EXISTING ERROR -- default
| ON EXISTING UPDATE
| ON EXISTING SKIP
<all_values_list> ::= <value_list> -- for all the columns in the table
<value_list> ::= <value> { "," <value> }
<value> ::= <expression>
| DEFAULT
<expression> ::= see <expression> in Chapter 3, Selecting

Note: You can insert rows into a view if that view qualifies as an updatable
view and it involves only one table. For more information about updatable views,
see Section 3.23, CREATE VIEW.

The expressions in the VALUES list must appear in the same order as the col-
umns to which they apply appear in the CREATE TABLE. Also, you must
specify a value for every single column in the table.

Note: The ALTER TABLE command can be used to append columns to an


existing table. These new columns are placed after all the columns that were
originally defined in the CREATE TABLE, and any other columns that were
appended by previous ALTER TABLE commands. When you see a mention of the
order of columns in the CREATE TABLE it should be interpreted as shorthand for
the order of columns as listed in the original CREATE TABLE and appended by
subsequent ALTER TABLE commands. The various ALTER commands are very
powerful and useful but for reasons of space they arent discussed in this book.

In the following example the value 1 is placed in key_1, 'first row' goes in
non_key_1, and '2003 09 29 13:21' is placed in last_updated:
CREATE TABLE t1 (
key_1 INTEGER NOT NULL DEFAULT AUTOINCREMENT,
non_key_1 VARCHAR ( 100 ) NOT NULL,
last_updated TIMESTAMP NOT NULL DEFAULT TIMESTAMP,
PRIMARY KEY ( key_1 ) );

INSERT t1 VALUES ( 1, 'first row', '2003 09 29 13:21' );

Tip: To see the order of columns in a table called t1, run this command in
ISQL: SELECT * FROM t1 WHERE 1 = 0. It will display the column names without
retrieving any data. Dont worry about performance this query will always run
quickly no matter how big the table is because the database engine knows that
WHERE 1 = 0 means no rows will ever be returned.

Potrebbero piacerti anche