Windows.  Viruses.  Notebooks.  Internet.  office.  Utilities.  Drivers

Last update: 13.07.2017

To add data, the INSERT command is used, which has the following formal syntax:

INSERT table_name [(column_list)] VALUES (value1, value2, ... valueN)

At the beginning there is an INSERT INTO statement, then in brackets you can specify a list of columns, separated by commas, into which data should be added, and at the end after the word VALUES, in brackets, the values ​​​​to be added for the columns are listed.

For example, let's say the following database was created earlier:

CREATE DATABASE productsdb; GO USE productsdb; CREATE TABLE Products (Id INT IDENTITY PRIMARY KEY, ProductName NVARCHAR(30) NOT NULL, Manufacturer NVARCHAR(20) NOT NULL, ProductCount INT DEFAULT 0, Price MONEY NOT NULL)

Let's add one line to it using the INSERT command:

INSERT Products VALUES ("iPhone 7", "Apple", 5, 52000)

After successful completion of SQL Server Management Studio should show "1 row(s) affected" in the message box:

Note that the values ​​for the columns in parentheses after the VALUES keyword are passed in the order in which they are declared. For example, in the CREATE TABLE statement above, you can see that the first column is Id. But since the IDENTITY attribute is set for it, the value of this column is automatically generated, and it can be omitted. The second column represents ProductName, so the first value, the string "iPhone 7", will be passed to that column. The second value, the string "Apple", will be passed to the third column Manufacturer, and so on. That is, the values ​​are passed to the columns as follows:

    ProductName: "iPhone 7"

    Manufacturer: Apple

Also, when entering values, you can specify the immediate columns in which values ​​will be added:

INSERT INTO Products (ProductName, Price, Manufacturer) VALUES ("iPhone 6S", 41000, "Apple")

Here the value is specified for only three columns. And now the values ​​are passed in the order of the columns:

    ProductName: "iPhone 6S"

    Manufacturer: Apple

For unspecified columns (in this case ProductCount) will add a default value if the DEFAULT attribute is set, or NULL. However, unspecified columns must be nullable or have a DEFAULT attribute.

We can also add multiple lines at once:

INSERT INTO Products VALUES ("iPhone 6", "Apple", 3, 36000), ("Galaxy S8", "Samsung", 2, 46000), ("Galaxy S8 Plus", "Samsung", 1, 56000)

In this case, three rows will be added to the table.

Also, when adding, we can specify that the default value be used for the column using the DEFAULT keyword or NULL:

INSERT INTO Products (ProductName, Manufacturer, ProductCount, Price) VALUES ("Mi6", "Xiaomi", DEFAULT, 28000)

In this case, the default value will be used for the ProductCount column (if it is set, if not, then NULL).

If all columns have a DEFAULT attribute that defines a default value, or are nullable, then you can insert default values ​​for all columns:

INSERT INTO Products DEFAULT VALUES

But if you take the Products table, then such a command will fail, since several fields do not have a DEFAULT attribute and at the same time do not allow null values.

Deleting entries

The DELETE statement is used to remove records from a table:

DELETE FROM tablename WHERE condition;

This statement removes from the specified table records (not individual column values) that satisfy the specified condition. The condition is boolean expression, whose various designs were discussed in previous labs.

The following query removes records from the Customer table in which the value of the LName column is "Ivanov":

DELETE FROM Customer

WHERE LName = "Ivanov"

If the table contains information about several clients with the last name Ivanov, then all of them will be deleted.

The WHERE clause can contain a subquery to select data (the SELECT statement). Subqueries in a DELETE statement work exactly the same way as they do in a SELECT statement. The following query removes all customers from the city of Moscow, with the city's unique identifier returned using a subquery.

DELETE FROM Customer

WHERE IdCity IN (SELECT IdCity FROM City WHERE CityName = "Moscow")

Transact-SQL extends standard SQL by allowing you to use another FROM clause in a DELETE statement. This extension, which specifies a join, can be used instead of a subquery in the WHERE clause to specify the rows to be deleted. It allows you to set data from the second FROM and remove the corresponding rows from the table in the first FROM clause. In particular, the previous query could be rewritten as follows

DELETE FROM Customer

FROM Customer k INNER JOIN

The operation of deleting records from a table is dangerous in the sense that it is associated with the risk of irreversible data loss in case of semantic (but not syntactic) errors in the formulation of the SQL statement. To avoid trouble, before deleting records, it is recommended that you first run the appropriate select query to see which records will be deleted. So, for example, before executing the delete query discussed earlier, it would not hurt to execute the corresponding select query.

SELECT *

FROM Customer k INNER JOIN

City c ON k.IdCity = c.IdCity AND c.CityName = "Moscow"

To remove all records from a table, just use the DELETE statement without the WHERE keyword. At the same time, the table itself with all the columns defined in it is saved and ready for inserting new records. For example, the following query removes records for all products.

DELETE FROM Product

Task for independent work: Formulate on SQL language a request to delete all orders that do not contain any goods (i.e. all empty orders).

Change requests are used to add (INSERT INTO), delete (DELETE) and update (UPDATE) records in tables.

INSERT INTO command

The INSERT INTO command can be used to add one or more records to the end of a table.

The syntax of the INSERT INTO command is as follows:
INSERT INTO Table_Name [(Field_Name)] VALUES (Data_Values);

For example, let's put a record in the "Groups" table containing data (1, BF-16a, 1, 1) in the corresponding columns (Group ID, Name, Course, Semester).

For this request in the format:
INSERT INTO Groups (GroupCode, Name, Course, Semester) VALUES (1, BF-16a, 1, 1);
we will enter it on the query form by executing the "Query Designer" command on the "Create" tab, in the "SQL Mode" mode from the View menu.

We save the query with the name "Add_1_records". As a result, an unrelated object will appear in the "Navigation Area" - "Add_1_records" (Fig.1.).


Rice. 1.

After saving the query, you need to execute this query by clicking on the "Run" icon. As a result of executing the “Add_1_records” command, the first record will appear in the empty table (Fig. 2.)



Rice. 2.

UPDATE command

The UPDATE command is intended to modify already existing records in the columns of the table, which allows you to update the values ​​in the given columns of the table.

The syntax for the UPDATE command is as follows:
UPDATE Table_Name SET Column_Name = New_Value WHERE Filter_Condition;

The SET clause specifies the names of the columns whose data needs to be changed. The WHERE clause is optional, but it can only point to the rows of the columns (tables) that need to be updated.

Let's create a query to change a record in the "Groups" table of the sql_training_st.mdb database.

For example, let's change an already existing entry for the first key field id in the "Name" column of the "Groups" table.

Request in the format:
UPDATE Groups SET Name="BF-16b" WHERE ID=1;

We save the query with the name "Change_record_1". As a result, an object will appear in the "Navigation Area" - "Change_record_1" (Fig. 3.).



Rice. 3.

After saving the query, you need to execute this query by clicking on the "Run" icon. As a result of executing the Change_Record_1 command, a dialog box will appear in which you must enter the value of the ID=1 parameter and click OK. After these actions in the "Groups" table, the entry in the "Name" field will change from BF-16a to BF-16b (Fig. 4.).



Rice. 4.

DELETE command

The DELETE command is designed to remove records from a table.

Format of request to delete records:
DELETE FROM Table_Name WHERE Selection_Condition;

Let's create a query to change a record in the "Groups" table of the sql_training_st.mdb database.

For example, let's delete the existing entry for the first key field id in all columns (fields) of the "Groups" table.

For this request in the format:
DELETE FROM Groups WHERE ID=1;
create on the query form by executing the "Query Designer" command on the "Create" tab, in the "SQL Mode" mode from the View menu.

We save the query with the name "Delete_record_1". As a result, an object will appear in the "Navigation Area" - "Delete_record_1" (Fig. 5.).



Rice. 5.

After saving the query, you need to execute this query by clicking on the "Run" icon. As a result of executing the "Delete_record_1" command, a dialog box will appear in which you must enter the value of the ID=1 parameter and click OK. After these steps, a dialog box "Confirm the deletion of records" will open. After confirmation in the "Groups" table, the data in all fields will be deleted, i.e. the record is deleted (Fig. 6.).



Rice. 6.

It should be noted that to delete data in specific fields of the table, it is advisable to use the UPDATE command and replace the values ​​in the required fields with Null. If it is necessary to drop the table as a whole, then the DROP TABLE statement should be used (the syntax of the statement is: DROP TABLE TableName;).

Let's restore the first record in the "Groups" table by executing the "Add_1_records" command, as a result we will restore the first record (Fig. 7.).



Rice. 7.

General syntax:

a) inserting one new row into the table:

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

VALUES

b) Inserting into the table one or more rows taken from the source specified in the subquery:

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