· 6 years ago · Apr 25, 2019, 09:22 AM
1Home / Basic MySQL Tutorial / MySQL Insert
2MySQL Insert
3?
4Summary: in this tutorial, you will learn how to use MySQL INSERT statement to add one or more rows to a table.
5
6Introduction to the MySQL INSERT statement
7The INSERT statement allows you to insert one or more rows into a table. The following illustrates the syntax of the INSERT statement:
8
91
102
11INSERT INTO table(c1,c2,...)
12VALUES (v1,v2,...);
13In this syntax,
14
15First, specify the table name and a list of comma-separated columns inside parentheses after the INSERT INTO clause.
16Then, put a comma-separated list of values of the corresponding columns inside the parentheses following the VALUES keyword.
17The number of columns and values must be the same. In addition, the positions of columns must be corresponding with the positions of their values.
18
19To add multiple rows into a table using a single INSERT statement, you use the following syntax:
20
211
222
233
244
255
266
27INSERT INTO table(c1,c2,...)
28VALUES
29 (v11,v12,...),
30 (v21,v22,...),
31 ...
32 (vnn,vn2,...);
33In this syntax, rows are separated by commas in the VALUES clause.
34
35MySQL INSERT examples
36Let’s create a new table named tasks for practicing the INSERT statement.
37
381
392
403
414
425
436
447
458
469
47CREATE TABLE IF NOT EXISTS tasks (
48 task_id INT AUTO_INCREMENT,
49 title VARCHAR(255) NOT NULL,
50 start_date DATE,
51 due_date DATE,
52 priority TINYINT NOT NULL DEFAULT 3,
53 description TEXT,
54 PRIMARY KEY (task_id)
55);
56Using simple INSERT statement example
57The following statement adds a new row to the tasks table:
58
59world wide web
60
611
622
633
644
65INSERT INTO
66 tasks(title,priority)
67VALUES
68 ('Learn MySQL INSERT Statement',1);
69MySQL returns the following message after the statement executed:
70
711
721 row(s) affected
73It means that one row has been inserted into the tasks table successfully.
74
75You can verify it by using the following query: