文章目录
- 1.0 MySQL表操作详解
- 1.1 MySQL创建表
- 1.2 MySQL查看表结构
- 1.3 MySQL修改表
1.0 MySQL表操作详解
1.1 MySQL创建表
指令:create table table_name(field1 datatype, field2 datatype) character + collate + engine;
后面三个参数可以省略,MySQL会自动调用默认配置
MariaDB [clx_database]> create table if not exists `stu_list`( //添加表
-> id int,
-> name varchar(20) comment '姓名',
-> password char(30) comment '校园卡密码',
-> birthday date comment '生日'
-> );
Query OK, 0 rows affected (0.01 sec)
MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_list |
+------------------------+
1 row in set (0.00 sec)
MariaDB [clx_database]> desc stu_list; //打印表结构
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
MariaDB [clx_database]> show create table stu_list \G; //打印表的构建信息
*************************** 1. row ***************************
Table: stu_list
Create Table: CREATE TABLE `stu_list` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '姓名',
`password` char(30) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '校园卡密码',
`birthday` date DEFAULT NULL COMMENT '生日'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci //此处为MySQL配置的表的默认属性
1 row in set (0.00 sec)
可以看到stu_list 默认使用的存储引擎是 InnoDB,接下来我们使用MyISAM存储引擎再创建一个表
MariaDB [clx_database]> create table if not exists `stu_myisam`(
-> id int,
-> name varchar(20),
-> password char(30),
-> birthday date
-> ) engine MyISAM; //显式设置使用MyISAM存储引擎
Query OK, 0 rows affected (0.01 sec)
MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_list |
| stu_myisam |
+------------------------+
2 rows in set (0.00 sec)
我们发现,不同的存储引擎,创建表的文件是不一样的。
stu_myisam 表的存储引擎是MyISAM,再数据存储中有三个不同的文件,分别存储不同的信息
stu_myisam.frm 表结构
stu_myisam.MYD 表数据
stu_myisam.MYI 表索引
1.2 MySQL查看表结构
指令:desc + table_name
MariaDB [clx_database]> desc stu_list;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
1.3 MySQL修改表
1.3.1 表的插入
指令:insert into table_name (field1, field2, field3 …) values (variable1, variable2, variable3 …);
MariaDB [clx_database]> system clear
MariaDB [clx_database]> insert into stu_list (id, name, password, birthday) values (1, 'a', 'b', '2000-0-0'), (2, 'c','d','2020-0-0');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+
| id | name | password | birthday |
+------+------+----------+------------+
| 1 | a | b | 2000-00-00 |
| 2 | c | d | 2020-00-00 |
+------+------+----------+------------+
2 rows in set (0.00 sec)
在项目实际开发中,可能想要修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。
1.3.2 表添加一个字段
指令: alter table table_name add field datatype + after field(没有默认添加在队尾);
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+
| id | name | password | birthday |
+------+------+----------+------------+
| 1 | a | b | 2000-00-00 |
| 2 | c | d | 2020-00-00 |
+------+------+----------+------------+
2 rows in set (0.00 sec)
MariaDB [clx_database]> alter table stu_list add path varchar(100); //在队尾添加一个path 字段
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+------+
| id | name | password | birthday | path |
+------+------+----------+------------+------+
| 1 | a | b | 2000-00-00 | NULL |
| 2 | c | d | 2020-00-00 | NULL |
+------+------+----------+------------+------+
2 rows in set (0.00 sec)
MariaDB [clx_database]> alter table stu_list add sex char(1) after name; //在name字段后面添加 sex字段
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+------+----------+------------+------+
| id | name | sex | password | birthday | path |
+------+------+------+----------+------------+------+
| 1 | a | NULL | b | 2000-00-00 | NULL |
| 2 | c | NULL | d | 2020-00-00 | NULL |
+------+------+------+----------+------------+------+
2 rows in set (0.00 sec)
可以看到表添加新的字段后,内部原有数据的新建字段都被设置成NULL ,如果因为表的约束内部成员变量不可以为空,则想要插入新字段则必须将原有数据全部删除,或者新建表将原有数据重新编辑然后导入,非常麻烦。就算要插入一定要放在最后,因为上层的业务逻辑一般都是从前向后插入,若你在中间插入一个字段很可能就会将其他字段的数据抢走引起连锁反应,导致后面各个字段拿到的数据与自身类型不匹配的现象
1.3.3 表删除一个字段
指令: alter table table_name drop filed;
MariaDB [clx_database]> alter table stu_list drop sex; //删除性别这个字段
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> select * from stu_list;
+------+------+----------+------------+------+
| id | name | password | birthday | path |
+------+------+----------+------------+------+
| 1 | a | b | 2000-00-00 | NULL |
| 2 | c | d | 2020-00-00 | NULL |
+------+------+----------+------------+------+
2 rows in set (0.00 sec)
1.3.4 表名修改
指令: alter table table_name rename to new_name
MariaDB [clx_database]> alter table stu_list rename to student_list;
Query OK, 0 rows affected (0.01 sec)
MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| stu_myisam |
| student_list |
+------------------------+
2 rows in set (0.00 sec)
1.3.5 表字段名修改
指令:alter table table_name change name newname newtype;
MariaDB [clx_database]> alter table student_list change name xingming char(60); //修改name字段为xingming 字段
Query OK, 2 rows affected (0.02 sec) //类型: char(60)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| xingming | char(60) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| path | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
1.3.6 表字段类型更改
指令:alter table table_name modify filed datatype;
MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| xingming | char(60) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| path | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
MariaDB [clx_database]> alter table student_list modify xingming varchar(20); //将姓名字段的类型更改为 varchar(20)
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [clx_database]> desc student_list;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| xingming | varchar(20) | YES | | NULL | |
| password | char(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| path | varchar(100) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
1.3.7 表的删除
指令: drop table table_name;
MariaDB [clx_database]> drop table stu_myisam; //删除表stu_myisam
Query OK, 0 rows affected (0.00 sec)
MariaDB [clx_database]> show tables;
+------------------------+
| Tables_in_clx_database |
+------------------------+
| student_list |
+------------------------+
1 row in set (0.00 sec)