| sex | char(8) | YES | | NULL | |
| birth | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
(11)新增一个字段
Mysql代码
mysql> alter table person add(address varchar(50));
Query OK, 1 row affected (0.27 sec)
Records: 1 Duplicates: 0 Warnings: 0
查看字段描述:
Mysql代码
mysql> desc person;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | char(8) | YES | | NULL | |
| birth | date | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
(12)更新字段内容
查看修改前表的内容:
Mysql代码
mysql> select * from person;
+------+----------+------+------------+---------+
| id | name | sex | birth | address |
+------+----------+------+------------+---------+
| 1 | zhangsan | 1 | 1990-01-08 | NULL |
+------+----------+------+------------+---------+
1 row in set (0.00 sec)
修改:
Mysql代码
mysql> update person set name='lisi' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from person;
+------+------+------+------------+---------+
| id | name | sex | birth | address |
+------+------+------+------------+---------+
| 1 | lisi | 1 | 1990-01-08 | NULL |
+------+------+------+------------+---------+
1 row in set (0.00 sec)
mysql> update person set sex='man',address='China' where id=1;
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from person;
+------+------+------+------------+---------+
| id | name | sex | birth | address |
+------+------+------+------------+---------+
| 1 | lisi | man | 1990-01-08 | China |
+------+------+------+------------+---------+
1 row in set (0.00 sec)
为了方便下面测试删除数据,在向person表中插入2条数据:
Mysql代码
mysql> insert into person(id,name,sex,birth,address)
-> values(2,'wangwu','man','1990-01-10','China');
Query OK, 1 row affected (0.02 sec)
mysql> insert into person(id,name,sex,birth,address)
-> values(3,'zhangsan','man','1990-01-10','China');
Query OK, 1 row affected (0.04 sec)
mysql> select * from person;
+------+----------+------+------------+---------+
| id | name | sex | birth | address |
+------+----------+------+------------+---------+
| 1 | lisi | man | 1990-01-08 | China |
| 2 | wangwu | man | 1990-01-10 | China |
| 3 | zhangsan | man | 1990-01-10 | China |
+------+----------+------+------------+---------+
3 rows in set (0.00 sec)
(13)删除表中的数据
删除表中指定的数据:
Mysql代码
mysql> delete from person where id=2;
Query OK, 1 row affected (0.02 sec)
mysql> select * from person;
+------+----------+------+------------+---------+
| id | name | sex | birth | address |
+------+----------+------+------------+---------+
| 1 | lisi | man | 1990-01-08 | China |
| 3 | zhangsan | man | 1990-01-10 | China |
+------+----------+------+------------+---------+
2 rows in set (0.00 sec)
删除表中全部的数据:
Mysql代码
mysql> delete from person;
Query OK, 2 rows affected (0.04 sec)
mysql> select * from person;
Empty set (0.00 sec)
(14)重命名表
查看重命名前的表名:
Mysql代码
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| person |
+-------------------+
1 row in set (0.00 sec)
重命名:
Mysql代码
mysql> alter table person rename per