13.向表中插入数据

1、命令语法:

nsert into <表名> [(<字段名 1>)[,.....<字段名>])]values(值 1)[,(值 n)]​

2、新建一个简单的测试表 test

mysql> use oldboy
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables ;
+------------------+
| Tables_in_oldboy |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
mysql> create table test(
-> id int(4) not null auto_increment,
-> name char(20) not null,
-> primary key(id)
-> );
Query OK, 0 rows affected (0.07 sec)
mysql> show tables;
+------------------+
| Tables_in_oldboy |
+------------------+
| student |
| test |
+------------------+
2 rows in set (0.00 sec)​

3.向表中插入数据

1、往 test 表中插入第一条数据
mysql> insert into test (id,name) values(1,'oldboy');
Query OK, 1 row affected (0.05 sec)
mysql> select * from test;
+----+--------+
| id | name |
+----+--------+
| 1 | oldboy |
+----+--------+
1 row in set (0.00 sec)​
2、由于 id 列为自增的,所以,可以只在 name 列插入值
mysql> insert into test(name) values('oldgirl');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+---------+
| id | name |
+----+---------+
| 1 | oldboy |
| 2 | oldgirl |
+----+---------+
2 rows in set (0.00 sec)
3、如果不指定列,就要按规矩为每列都插入恰当的值
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | oldboy |
| 2 | oldgirl |
| 3 | zhangxuan |
+----+-----------+
4 rows in set (0.00 sec)
4、 批量插入数据方法,提示效率
mysql> insert into test (id,name) values (4,'engchao'),(5,'geili');
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | oldboy |
| 2 | oldgirl |
| 3 | zhangxuan |
| 4 | engchao |
| 5 | geili |
+----+-----------+
5 rows in set (0.00 sec)​

猜你喜欢

转载自www.cnblogs.com/hackerlin/p/12539635.html