SQL request for adding. Transact-SQL - Insert data. The simplest SQL requests

The INSERT operator inserts new entries to the table. In this case, the values \u200b\u200bof the columns may be literal constants, or be the result of subquery. In the first case, a separate INSERT operator is used to insert each string; In the second case, so many lines will be inserted as the subquery is returned.

The syntax of the operator is as follows:

    INSERT INTO [(, ...)]

    (Values \u200b\u200b(, ...))

  1. | Default Values)

As can be seen from the presented syntax, the list of columns is not mandatory (they say square brackets in the syntax description). In the event that it is missing, the list of inserted values \u200b\u200bmust be complete, that is, provide values \u200b\u200bfor all columns of the table. In this case, the order of values \u200b\u200bmust correspond to the order specified by the CREATE TABLE statement for the table in which lines are inserted. In addition, these values \u200b\u200bshould include the same data type as the columns in which they are entered. As an example, consider the insertion of the string in the Product table created by the following Create Table operator:

    Create Table Product.

    maker Char (1) not NULL,

    model Varchar (4) Not NULL,

    type Varchar (7) Not NULL

Let it be necessary to add to this table model PC 1157 manufacturer B. This can be done by the following operator:

    INSERT INTO PRODUCT

    Values \u200b\u200b("B", 1157, "PC");

If you set a list of columns, you can change the "natural" order of their following:

    INSERT INTO PRODUCT (Type, Model, Maker)

    Values \u200b\u200b("PC", 1157, "B");

It would seem that this is a completely unnecessary possibility that makes the design only more cumbersome. However, it becomes winning if the columns have default values. Consider the following structure of the table:

    Create Table Product_d.

    maker Char (1) NULL,

    model Varchar (4) NULL,

    type Varchar (7) NOT NULL DEFAULT "PC"

Note that here the values \u200b\u200bof all columns have the default values \u200b\u200b(the first two - , and the last column is Type - PC). Now we could write:

    INSERT INTO PRODUCT_D (MODEL, MAKER)

    Values \u200b\u200b(1157, "B");

In this case, the missing value when inserting the string will be replaced by the default value - PC. Note that if the default value is not specified for the column in the CREATE TABLE statement and the NOT NULL restriction is not specified, which prohibits the use of NULL in this table column, the default value is NULL.

The question arises: can I not specify the list of columns and, nevertheless, use the default values? The answer is positive. To do this, instead of explicitly specifying the value of using the reserved word default:

    INSERT INTO PRODUCT_D.

    Values \u200b\u200b("B", 1158, Default);

Since all columns have default values, it would be possible to write to the default values \u200b\u200bto insert a string:

    INSERT INTO PRODUCT_D.

    Values \u200b\u200b(Default, Default, Default);

However, for this case, a special design of Default Values \u200b\u200bis intended (see the operator syntax), with which the above operator can be rewritten as

    INSERT INTO PRODUCT_D DEFAULT VALUES;

Note that when inserting a string in the table, all restrictions imposed on this table are checked. These may be limitations of the primary key or a unique index, check limits of type Check, limit reference integrity. In case of violation of any restriction, the insertion string will be rejected. Consider now the case of using a subquery. Let us need to insert into the product_d table all lines from the Product table related to the models of personal computers (Type \u003d 'PC'). Since the values \u200b\u200byou need are already available in some table, the formation of inserted strings manually, firstly, is ineffective, and, secondly, it may allow input errors. Use subqueries solves these problems:

The use of the "*" symbol in the subquery is in this case justified, since the order of the columns is the same for both tables. If it were not so, it would be necessary to apply a list of columns either in the INSERT statement, or in a subquery, or in both places, which would bring the order of the columns:

Here, as well as before, you can not specify all columns if you want to use the default values, for example:

In this case, the default PC for all inserted rows will be substituted into the Type Type Table Column.

Note that when using a subquery containing a predicate, only those lines for which the value of the predicate is equal to True (not unknown!). In other words, if the Type column in the Product table would allow NULL-value, and this value would be present in a number of rows, then these lines would not be inserted into the product_d table.

Overcome the restriction on the insertion of one line in the INSERT statement when using the row constructor in the Values \u200b\u200boffer allows an artificial use of a subquery to form a string with the UNION ALL offer. So if we need to insert several lines using one insert operator, you can write:

    INSERT INTO PRODUCT_D.

    SELECT "B" As Maker, 1158 AS Model, "PC" as Type

    Union All.

    SELECT "C", 2190, "LAPTOP"

    Union All.

    SELECT "D", 3219, "PRINTER";

Using UNION ALL preferred Union Even if the lack of rows-duplicates is guaranteed, since in this case the check will not be performed to exclude duplicates.

It should be noted that inserting several tuples using a row designer is already implemented in Systems for managing relational databases (DBMS) developed by Microsoft. Language of structured queries) is a universal computer language used to create, modify and manage data in relational databases.SQL Server 2008. With this opportunity, the last request can be rewritten in the form:

    INSERT INTO PRODUCT_D VALUES

    ("B", 1158, "PC"),

    ("C", 2190, "Laptop"),

General syntax:

a) insert one new line in the table:

Insert.<таблица> [(columns)]

Values

b) Insert in a table of one or more rows taken from the source specified in the subquery:

Insert.<таблица> [(columns)]