插入数据(insert)
insert语句的3个主要组成部分:
- 所要插入数据的表的名称;
- 表终需要使用的列的名称;
- 需要插入到列的值。
数字型主键生成机制
数字型主键生成机制,除了随机选择数字外,还可以有以下两个常用选择:
- 查看表中当前主键的最大值,并加1;
- 让数据库服务器自动提供。
第1种方法在并发的时候可能会发生问题,基于第2种方案,MySQL有一种实现方式:只需简单地为主键列打开自增(auto-increment)特性。
一般情况下,应该在建表时定义此属性;
也可以使用alter table
修改已经存在的表定义:
ALTER TABLE person MODIFY person_id SMALLINT UNSIGNED AUTO_INCREMENT;
该语句实质上重新定义了 person 表的 person_id 列,现在如果再次使用 describe 命令,会看到 person_id 的“Extra”列中列出了自增特性。
+-------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------------------+------+-----+---------+----------------+
| person_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| fname | varchar(20) | YES | | NULL | |
| lname | varchar(20) | YES | | NULL | |
| gender | enum('M','F') | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| street | varchar(30) | YES | | NULL | |
| city | varchar(20) | YES | | NULL | |
| state | varchar(20) | YES | | NULL | |
| country | varchar(20) | YES | | NULL | |
| postal_code | varchar(20) | YES | | NULL | |
+-------------+----------------------+------+-----+---------+----------------+
insert语句
执行如下SQL语句新增一列信息:
INSERT INTO person
(person_id, fname, lname, gender, birth_date)
VALUES (null, 'Zifeiy', 'Wang', 'M', '1982-05-27');
执行如下语句进行查询:
SELECT person_id, fname, lname, birth_date
FROM person;
效果如下:
mysql> INSERT INTO person
-> (person_id, fname, lname, gender, birth_date)
-> VALUES (null, 'Zifeiy', 'Wang', 'M', '1982-05-27');
Query OK, 1 row affected (0.05 sec)
mysql> SELECT person_id, fname, lname, birth_date
-> FROM person;
+-----------+--------+-------+------------+
| person_id | fname | lname | birth_date |
+-----------+--------+-------+------------+
| 1 | Zifeiy | Wang | 1982-05-27 |
+-----------+--------+-------+------------+
1 row in set (0.00 sec)
可以使用 where 子句指定想要提取的数据,例如 person_id 为1的行:
SELECT person_id, fname, lname, birth_date
FROM person
WHERE person_id=1;
或者lanme为‘Wang’的行:
SELECT person_id, fname, lname, birth_date
FROM person
WHERE lname='Wang';
Zifeiy还喜欢3种食物,因此再执行如下三条SQL:
INSERT INTO favorite_food (person_id, food) VALUES (1, 'apple');
INSERT INTO favorite_food (person_id, food) VALUES (1, 'banana');
INSERT INTO favorite_food (person_id, food) VALUES (1, 'orange');
下面获取Zifeiy的喜爱食物列表,并根据字典序进行排列(使用order by
):
mysql> SELECT food FROM favorite_food WHERE person_id=1 ORDER BY food;
+--------+
| food |
+--------+
| apple |
| banana |
| orange |
+--------+
3 rows in set (0.00 sec)
为了让Zifeiy不感到孤单,可以再次执行insert语句向person表中增加Binbin Fan:
INSERT INTO person
(person_id, fname, lname, gender, birth_date,
street, city, state, country, postal_code)
VALUES (null, 'Binbin', 'Fan', 'F', '1992-02-02',
'23 Tomas St.', 'Huzhou', 'ZJ', 'CHINA', '330501');
再次查询表格:
mysql> SELECT person_id, fname, lname, birth_date FROM person;
+-----------+--------+-------+------------+
| person_id | fname | lname | birth_date |
+-----------+--------+-------+------------+
| 1 | Zifeiy | Wang | 1982-05-27 |
| 2 | Binbin | Fan | 1992-02-02 |
+-----------+--------+-------+------------+
2 rows in set (0.00 sec)
更新数据
UPDATE person
SET street = '123 Haha St.',
city = 'Hangzhou',
state = 'JS',
country = 'CHINA',
postal_code = '320101'
WHERE person_id = 1;
删除数据
DELETE FROM person WHERE person_id = 2;