mysql-insert插入

基础创建语句

1.创建一张test表

create table test(

`id` int(4) not null auto_increment,

`name` char(20) not null,

primary key(id)

);

2.插入

方法1:插入多条数据在后面加逗号和括号即可

语法:insert into `表名`(字段名,字段名) values(值,值),(值,值);

mysql>insert into test(id,name) values(1,'soulchild');

因为id字段是自增的,所以插入时不写id也可以

mysql>insert into test(name) values('xiaoming');

mysql> select * from test;

+----+-----------+

| id | name      |

+----+-----------+

|  1 | soulchild |

|  2 | xiaoming  |

+----+-----------+

2 rows in set (0.00 sec)

 

方法2:

按照表结构顺序插入(多条数据在后面加逗号和括号即可)

语法:insert into `表名` values(值,值),(值,值);

mysql>insert into test values(3,'xiaobai');

mysql> select * from test;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | soulchild |
|  2 | xiaoming  |
|  3 | xiaobai   |
+----+-----------+
3 rows in set (0.00 sec)

插入多条数据举例:(推荐)

mysql> insert into test values(1,'soulchild'),(2,'xiaoming'),(3,'xiaobai'),(4,'xiaoli'),(5,'xiaohong');

相关文章

发表新评论