4-2:MySQL 插入记录INSERT
插入记录的语法结构:
INSERT [INTO] tbl_name [(col_name,...)] {VALUES| VALUE}
({expr | DEFAULT},...),(...),...
col_name是列名称,如果省略不写的话,代表所有的字段需要依次赋值
首先创建一个用于测试的数据表:
mysql> CREATE TABLE users(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(20) NOT NULL,
-> password VARCHAR(32) NOT NULL,
-> age TINYINT UNSIGNED NOT NULL DEFAULT 10,
-> sex BOOLEAN
-> );
Query OK, 0 rows affected (0.01 sec)
现在我们省略col_name,则插入记录的时候需要依次为所有字段赋值,但是id字段是自动编号的
我们该如何为id字段赋值呢?只需要赋值为NULL或者DEFAULT就可以了:
mysql> INSERT users VALUES(NULL,'Tom','123',25,1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
+----+----------+----------+-----+------+
1 row in set (0.00 sec)
mysql> INSERT users VALUES(DEFAULT,'John','123',25,1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
| 2 | John | 123 | 25 | 1 |
+----+----------+----------+-----+------+
2 rows in set (0.00 sec)
可以看到id自动编号为,其他字段比如age除了可以直接赋值外,还可以赋值表达式,比如:
mysql> INSERT users VALUES(NULL,'John','123',25-1,1);
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
| 2 | John | 123 | 25 | 1 |
| 3 | Tom | 123 | 25 | 1 |
| 4 | John | 123 | 24 | 1 |
+----+----------+----------+-----+------+
4 rows in set (0.00 sec)
DEFAULT除了可以用于给自动编号的字段赋值外,还可以用于给设置了默认值的字段赋值:
mysql> SHOW COLUMNS FROM users;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
| sex | tinyint(1) | YES | | NULL | |
+----------+----------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
mysql> INSERT users VALUES(NULL,'John','123',DEFAULT,1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
| 2 | John | 123 | 25 | 1 |
| 3 | Tom | 123 | 25 | 1 |
| 4 | John | 123 | 24 | 1 |
| 5 | John | 123 | 10 | 1 |
+----+----------+----------+-----+------+
5 rows in set (0.00 sec)
接下来一次性写入多条记录,只需要用,分隔即可,这里以两条记录为例:
mysql> INSERT users VALUES(DEFAULT,'Tom','123',25,1),(NULL,'Rose',md5('123'),DEFAULT,0);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM users;
+----+----------+----------------------------------+-----+------+
| id | username | password | age | sex |
+----+----------+----------------------------------+-----+------+
| 1 | Tom | 123 | 25 | 1 |
| 2 | John | 123 | 25 | 1 |
| 3 | Tom | 123 | 25 | 1 |
| 4 | John | 123 | 24 | 1 |
| 5 | John | 123 | 10 | 1 |
| 6 | Tom | 123 | 25 | 1 |
| 7 | Rose | 202cb962ac59075b964b07152d234b70 | 10 | 0 |
+----+----------+----------------------------------+-----+------+
7 rows in set (0.00 sec)
md5是php中用于计算的一个字符串的哈希值的方法。