Insert
It is possible to write the INSERT INTO
statement in two ways.
Inserting Partial Rows. We need to specifies both the column names and the values to be inserted:
Inserting Complete Rows. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table.
Inserting Partial Rows
You may omit columns from an INSERT operation if the table definition so allows. One of the following conditions must exist:
The column is defined as allowing NULL values (no value at all).
A default value is specified in the table definition. This means the default value will be used if no value is specified.
To insert partial rows, just specify the columns names that have a value and then specify the corresponding values in VALUES
clause.
Inserting Complete Rows
This is not safe because the order might be different. The following is recommended:
As a rule, never use INSERT without explicitly specifying the column list.
Inserting Retrieved Data
To insert retrieved data from another table, use INSERT INTO SELECT
which is made up of an INSERT INTO
statement and a SELECT
statement.
Suppose you want to merge a list of customers from another table into your Customers table. You can do the following:
Last updated