文章目录
- 1.表的操作
- 1.1创建表
- 1.2创建表案例
- 1.3查看表结构
- 1.4修改表
- 1.5删除表
1.表的操作
1.1创建表
语法:
CREATE TABLE table_name(
filed1 datatype,
filed2 datatype,
filed3 datatype
)character set 字符集 collate 校验规则 engine 存储引擎;
说明:
field
表示列名datatype
表示列的类型character set
字符集,如果没有指定字符集,则以所在数据库的字符集为准collate
校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准
1.2创建表案例
Create Table:
CREATE TABLE `users` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL COMMENT '用户名',
`password` char(32) DEFAULT NULL COMMENT '密码',
`birthday` date DEFAULT NULL COMMENT '生日'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
说明:
-
不同的存储引擎,创建表的文件不一样。
users
表存储引擎是MyISAM
,在数据目中有三个不同的文件,分别是:users.frm
:表结构users.MYD
:表数据users.MYI
:表索引
备注:创建一个engine是
innodb
的数据库,观察存储目录
1.3查看表结构
desc 表名;
示例:
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
【图1.3-1】
1.4修改表
在项目实际开发中,经常修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。
语法:
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename MODIFY(column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename DROP(column);
案例:
- 在users表添加两条记录用于后期测试
insert into users values(1,'张三','1234','2000-10-1'),(2,'李四','4321','1999-7-1');
mysql> select * from users;
+------+--------+----------+------------+
| id | name | password | birthday |
+------+--------+----------+------------+
| 1 | 张三 | 1234 | 2000-10-01 |
| 2 | 李四 | 4321 | 1999-07-01 |
+------+--------+----------+------------+
2 rows in set (0.01 sec)
- 在users表添加一个字段,用于保存图片路径
mysql> alter table users add image_path varchar(60) comment '图片路径' after birthday;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| image_path | varchar(60) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
插入新字段后,对原来表中的数据没有影响:
mysql> select * from users;
+------+--------+----------+------------+------------+
| id | name | password | birthday | image_path |
+------+--------+----------+------------+------------+
| 1 | 张三 | 1234 | 2000-10-01 | NULL |
| 2 | 李四 | 4321 | 1999-07-01 | NULL |
+------+--------+----------+------------+------------+
2 rows in set (0.00 sec)
查看users表创建语句
mysql> show create table users \G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL COMMENT '用户名',
`password` char(32) DEFAULT NULL COMMENT '密码',
`birthday` date DEFAULT NULL COMMENT '生日',
`image_path` varchar(60) DEFAULT NULL COMMENT '图片路径'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
- 修改name,讲其长度该成60
mysql> alter table users modify name varchar(60);
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(60) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| image_path | varchar(60) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> show create table users \G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`id` int(11) DEFAULT NULL,
`name` varchar(60) DEFAULT NULL,
`password` char(32) DEFAULT NULL COMMENT '密码',
`birthday` date DEFAULT NULL COMMENT '生日',
`image_path` varchar(60) DEFAULT NULL COMMENT '图片路径'
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
- 删除password列
注意:删除字段一定要小心,删除字段及其对应的列数据都没了
mysql> alter table users drop password;
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(60) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| image_path | varchar(60) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from users;
+------+--------+------------+------------+
| id | name | birthday | image_path |
+------+--------+------------+------------+
| 1 | 张三 | 2000-10-01 | NULL |
| 2 | 李四 | 1999-07-01 | NULL |
+------+--------+------------+------------+
2 rows in set (0.00 sec)
- 修改表名为employee
mysql> alter table users rename to employee;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| employee |
| person |
+-----------------+
2 rows in set (0.00 sec)
to:可以省略 alter table users rename employee;
- 将name列修改成
xingming
mysql> alter table users change name xingming varchar(60); -- 新字段需要完整定义
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc users;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| xingming | varchar(60) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| image_path | varchar(60) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
1.5删除表
语法格式:
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [,tbl_name]...
示例:
drop table t1;
mysql> show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| person |
| t1 |
| users |
+-----------------+
3 rows in set (0.01 sec)
mysql> drop table t1;
Query OK, 0 rows affected (0.12 sec)
mysql> show tables;
+-----------------+
| Tables_in_test2 |
+-----------------+
| person |
| users |
+-----------------+
2 rows in set (0.00 sec)
注:请谨慎删除
(本篇完)