剑指offer:一年又3天 |
---|
创建、查看、修改(属性和内容)与删除
语法:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
说明:
mysql> create table stu(
-> id int comment '唯一标识',
-> name varchar(32) comment '姓名',
-> age int comment '年龄',
-> password varchar(32) comment '密码');
Query OK, 0 rows affected (0.04 sec)
语法:
mysql> desc stu;
mysql> desc stu; --- 查看表结构:列名 数据类型 (数据类型以及后面的四个约束放在后面的文章中进行介绍)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
mysql> show create table stu \G --- 查看表的创建语句
*************************** 1. row ***************************
Table: stu
Create Table: CREATE TABLE `stu` (
`id` int(11) DEFAULT NULL COMMENT '唯一标识',
`name` varchar(32) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`password` varchar(32) DEFAULT NULL COMMENT '密码'
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
在项目实际开发中,经常修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,
表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。
语法:
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...) [after column]; --- 添加列
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...); --- 修改列属性
ALTER TABLE tablename DROP (column); --- 删除列
示例:
mysql> alter table stu add gender char(2) comment '性别' after name; --- 修改表stu:在name之后添加一列gender
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu; --- 查看表结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| gender | char(2) | YES | | NULL | | --- 插入成功
| age | int(11) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> show create table stu \G --- 查看一下创建语句
*************************** 1. row ***************************
Table: stu
Create Table: CREATE TABLE `stu` (
`id` int(11) DEFAULT NULL COMMENT '唯一标识',
`name` varchar(32) DEFAULT NULL COMMENT '姓名',
`gender` char(2) DEFAULT NULL COMMENT '性别', --- 插入成功
`age` int(11) DEFAULT NULL COMMENT '年龄',
`password` varchar(32) DEFAULT NULL COMMENT '密码'
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
mysql> alter table stu modify gender char(8); --- 修改:只修改gender列数据类型为 char(8)
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table stu \G
*************************** 1. row ***************************
Table: stu
Create Table: CREATE TABLE `stu` (
`id` int(11) DEFAULT NULL COMMENT '唯一标识',
`name` varchar(32) DEFAULT NULL COMMENT '姓名',
`gender` char(8) DEFAULT NULL, --- 结果:数据类型是修改了,但是我们之前写的注释“性别”也没有了
`age` int(11) DEFAULT NULL COMMENT '年龄',
`password` varchar(32) DEFAULT NULL COMMENT '密码'
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)
mysql> alter table stu drop gender; --- 删除列gender
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | | --- 已删除
| age | int(11) | YES | | NULL | |
| password | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
结论:修改是全覆盖式写入,因此当我们需要修改列的某一个属性时,需要先查看一下创建语句,复制粘贴只把想要修改的部分改掉即可。
语法:
ALTER TABLE tablename RENAME [ TO ] new_tablename;
示例:
mysql> create table test(id int);
Query OK, 0 rows affected (0.03 sec)
mysql> show tables; --- 查看所有表
+---------------+
| Tables_in_db1 |
+---------------+
| stu |
| test |
+---------------+
2 rows in set (0.00 sec)
mysql> alter table test rename to newTest; --- 修改表test名字为newTest
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| newTest | --- 修改完成
| stu |
+---------------+
2 rows in set (0.00 sec)
语法:
INSERT INTO tablename[(field1, field2, ...)] values(...), (...);
说明:
示例:
mysql> insert into stu(id, name) values(001, '张三'), --- 只插入id,name
-> (002, '李四');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into stu(name, id) values('王五', 003); --- 只插入id,name 一一对应
Query OK, 1 row affected (0.01 sec)
mysql> insert into stu values --- 默认插入所有列
-> (004, '赵六', 20, '123456'),
-> (005, '孙七', 19, '654321');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+------+--------+------+----------+
| id | name | age | password |
+------+--------+------+----------+
| 1 | 张三 | NULL | NULL | --- 没插入的数据显示为空
| 2 | 李四 | NULL | NULL |
| 3 | 王五 | NULL | NULL |
| 4 | 赵六 | 20 | 123456 |
| 5 | 孙七 | 19 | 654321 |
+------+--------+------+----------+
5 rows in set (0.00 sec)
语法:
SELECT field1, field2 FROM tablename [WHERE field3 = xxx];
说明:
示例:
mysql> select name, age from stu; --- 只显示name列与age列
+--------+------+
| name | age |
+--------+------+
| 张三 | NULL |
| 李四 | NULL |
| 王五 | NULL |
| 赵六 | 20 |
| 孙七 | 19 |
+--------+------+
5 rows in set (0.00 sec)
mysql> select id, name from stu where id >= 3; --- 只显示id >= 3的学生的id和name列
+------+--------+
| id | name |
+------+--------+
| 3 | 王五 |
| 4 | 赵六 |
| 5 | 孙七 |
+------+--------+
3 rows in set (0.00 sec)
mysql> select * from stu; --- * 代表所有列
+------+--------+------+----------+
| id | name | age | password |
+------+--------+------+----------+
| 1 | 张三 | NULL | NULL |
| 2 | 李四 | NULL | NULL |
| 3 | 王五 | NULL | NULL |
| 4 | 赵六 | 20 | 123456 |
| 5 | 孙七 | 19 | 654321 |
+------+--------+------+----------+
5 rows in set (0.00 sec)
语法:
UPDATE tablename SET field1 = xxx, field2 = xxx [WHERE field3 = xxx];
说明:
示例:
mysql> update stu set name = '周八', age = 30 where id = 5; --- where是设置查找条件:只修改id = 5的行
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stu;
+------+--------+------+----------+
| id | name | age | password |
+------+--------+------+----------+
| 1 | 张三 | NULL | NULL |
| 2 | 李四 | NULL | NULL |
| 3 | 王五 | NULL | NULL |
| 4 | 赵六 | 20 | 123456 |
| 5 | 周八 | 30 | 654321 | --- 修改完成
+------+--------+------+----------+
5 rows in set (0.00 sec)
mysql> update stu set password = '111111'; --- 不设置限制(查找条件)就会作用于所有行
Query OK, 5 rows affected (0.00 sec)
Rows matched: 5 Changed: 5 Warnings: 0
mysql> select * from stu;
+------+--------+------+----------+
| id | name | age | password |
+------+--------+------+----------+
| 1 | 张三 | NULL | 111111 | --- 全部修改
| 2 | 李四 | NULL | 111111 |
| 3 | 王五 | NULL | 111111 |
| 4 | 赵六 | 20 | 111111 |
| 5 | 周八 | 30 | 111111 |
+------+--------+------+----------+
5 rows in set (0.00 sec)
语法:
DELETE FROM tabename [where ...]; -- 删除特定数据(不写where会直接清空表)
示例:
mysql> create table tb10(
-> name varchar(20),
-> age int
-> );
Query OK, 0 rows affected (0.03 sec)
-- 插入数据省略
mysql> select * from tb10;
+--------+------+
| name | age |
+--------+------+
| 张三 | 20 |
| 张三 | 20 |
| 张三 | 20 |
| 李四 | 21 |
| 李四 | 21 |
| 李四 | 21 |
| 王五 | 30 |
| 王五 | 30 |
| 王五 | 30 |
+--------+------+
9 rows in set (0.00 sec)
mysql> delete from tb10 where name='李四'; -- 删除名字为李四的记录
Query OK, 3 rows affected (0.00 sec)
mysql> select * from tb10;
+--------+------+
| name | age |
+--------+------+
| 张三 | 20 |
| 张三 | 20 |
| 张三 | 20 |
| 王五 | 30 |
| 王五 | 30 |
| 王五 | 30 |
+--------+------+
6 rows in set (0.00 sec)
mysql> delete from tb10; -- 删除所有记录
Query OK, 6 rows affected (0.01 sec)
mysql> select * from tb10;
Empty set (0.00 sec) -- tb10为空
语法:
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
示例:
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| newTest |
| stu |
+---------------+
2 rows in set (0.00 sec)
mysql> drop table newTest; --- 删除newTest
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| stu |
+---------------+
1 row in set (0.00 sec)