In previous SQL lessons,you learned about database related commands, data tables in SQL, such as create table commands, delete table commands. Starting from this article we will go into the query in SQL, opening up the query INSERT INTO.

INSERT statement in SQL

The INSERT INTO statement in SQL is used to add a new row of data to the table in the database.

Syntax of the INSERT INTO statement:

INSERT statement in SQL

INSERT statement in SQL

The INSERT INTO statement has two basic syntaxes, which we’ll look at in the two syntaxes discussed in this article.

INSERT INTO statement, syntax 1:

INSERT INTO TEN_BANG (cot1, cot2, cot3,...cotN)  VALUES (gia_tri1, gia_tri2, gia_tri3,...gia_triN);

Here, cot1, cot2, cot3, … cotN are the names of the columns in the table for which you want to insert the data. gia_tri1, gia_tri2, gia_tri3, … gia_triN is the value to add corresponding columns.

If you want to add values to all columns in the table, you can useINSERT INTO with syntax twobelow:

INSERT INTO TEN_BANG
VALUES (gia_tri1, gia_tri2, gia_tri3,...gia_triN);

Do you hit the difference? In syntax 2, you do not need to list the names of the columns in the table, just make sure that the order of values you want to add to the order of the columns in the table is okay.

Example of INSERT INTO:

The following command creates six records for the NHANVIEN table

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (1, 'Thanh', 24, 'Haiphong', 2000.00 );

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (2, 'Loan', 26, 'Hanoi', 1500.00 );

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (3, 'Nga', 24, 'Hanam', 2000.00 );

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (4, 'Manh', 29, 'Hue', 6500.00 );

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (5, 'Huy', 28, 'Hatinh', 8500.00 );

INSERT INTO NHANVIEN (ID,TEN,TUOI,DIACHI,LUONG)
VALUES (6, 'Cao', 23, 'HCM, 4500.00 );

You can use syntax 2 to add the 7th record to the table NHANVIEN as follows:

INSERT INTO NHANVIEN  VALUES (7, 'LAM', 29, 'Hanoi, 15000.00 );

After doing all the above, we will have NHANVIEN table as follows:

+----+----------+-----+-----------+---------+
| ID |   TEN    | TUOI|  DIACHI   |   LUONG | 
+----+----------+-----+-----------+---------+ 
| 1  | Thanh    |  24 | Haiphong  | 2000.00 | 
| 2  | Loan     |  26 | Hanoi     | 1500.00 | 
| 3  | Nga      |  24 | Hanam     | 2000.00 | 
| 4  | Mạnh     |  29 | Hue       | 6500.00 | 
| 5  | Huy      |  28 | Hatinh    | 8500.00 | 
| 6  | Cao      |  23 | HCM       | 4500.00 | 
| 7  | Lam      |  29 | Hanoi     | 15000.00| 
+----+----------+-----+-----------+---------+

Add a table to the table available:

In SQL, you can add table B to table A by using the SELECT statement in table B, correctly as follows:

INSERT INTO TEN_BANG_A [(cot1, cot2, ... cot)]   SELECT cot1, cot2, ...cotN   FROM TEN_BANG_B  [WHERE DIEU_KIEN];

In the above statement, you would add the columns from Table B to the Table A, so that the sections in [] are possible or not.

Read More:

Leave a Comment

Close