September 11th, 2008

INSERT – Multiple Rows

Posted by admin in E. INSERT, UPDATE, DELETE

To INSERT multiple rows into a table, the column names must be specified or the field values must be in exactly the same order as the columns are defined in the table.
All notes in the One Row topic apply to Multiple Rows.

Positional Insert:     INSERT INTO table
                              subselect;

This command copies all rows and column values from the subselect to the INSERT table.

                                                                       or

Named Columns:          INSERT INTO table
                               (column1, column2, column3, ...)
                               subselect;

This command copies column values from the subselect to the INSERT table.

> The process of adding data to a new table is called “populating” the table.
> A subselect is a SELECT statement as described previously that has been attached to another SQL command.
> When column names are specified, SQL ignores the column names in the subselect and uses column position to match with the column names specified in the INSERT table.
> An omitted column is given its default value or null.
> The table in the subselect remains unchanged.
> You can also INSERT rows through a VIEW.
> With some RDBMS systems, the INTO keyword is optional. But you should include the word INTO for portability.
> The SQL2 standard allows UNION and JOIN in the subselect.

September 10th, 2008

INSERT – One Row

Posted by admin in E. INSERT, UPDATE, DELETE

To INSERT a single row into a table, the column names must be specified, or the field values must be in the same exact order as the columns are defined in the table.

Positional INSERT:   INSERT INTO table
                     VALUES (value1, value2, NULL, ...);

                                                                      or

Named Columns:       INSERT INTO table
                            (column1, column2, column3, ...)
                     VALUES (value1,  value2,  NULL,    ...);

> The columns not specified in the positional INSERT must be created as NULL or NOT NULL with DEFAULT.
> The values specified must be compatible with the respective column’s data type or be convertible to that data type.
> Character and datetime data types must be enclosed by quotation marks.
> Numeric data and the keyword NULL must not be enclosed with quotation marks.
> A value of two single quotes (”) with no spaces between the quotes also symbolizes a NULL.
> These statements do not return a result, but your RDBMS will normally return a message showing that the statement executed successfully and it should show the number of rows affected by the change.
> An inserted value can not violate a CHECK constraint. See Check Constraint.
> An inserted foreign key value must contain NULL (if it is allowed) or an existing key value from the primary or unique key referenced by the foreign key. See Primary Keys and Foreign Keys.

September 9th, 2008

INSERT Syntax

Posted by admin in E. INSERT, UPDATE, DELETE

Every database starts out empty.
You must first create the table definition using SQL’s Data Definition Language, which we will cover in a future section.
When you have defined the table, it still contains no data.
You must INSERT data into the table to make it useful.
The INSERT statement adds one or more rows to a table.

      INSERT INTO table
           [(COLUMN1, COLUMN2, ...)]
     [VALUES ([value1, value2, ...
            | hostvariable, ...
            | expression, ...
            |NULL, ...
            | specialregister, ...)
            | subselect];

> This is the only way to load data into tables per the SQL standard.
However, many vendors have their own tools for loading tables.
For example, Oracle has a utility called SQL *Loader.

Go on and we will cover the details.

« Previous PageNext Page »