@TOC


📝表的操作

🌠 创建表

语法:

CREATE TABLE table_name (
 	field1 datatype,
 	field2 datatype,
 	field3 datatype
 ) character set 字符集 collate 校验规则 engine 存储引擎;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

说明:

  • field 表示列名
  • datatype 表示列的类型
  • character set 字符集,如果没有指定字符集,则以所在数据库的字符集为准
  • collate 校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准

🌠 创建表案例

create  table  users (
 id int,
 name varchar(20) comment '用户名',
 password char(32) comment '密码是32位的md5值',
 birthday date comment '生日'
 ) character set utf8 engine MyISAM;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

说明:

  • 不同的存储引擎,创建表的文件不一样。 users 表存储引擎是
  • MyISAM ,在数据目中有三个不同的文件,分别是:
  • users.frm:表结构
  • users.MYD:表数据
  • users.MYI:表索引

【MySQL 数据库】MySQL 表的操作_数据库

🌉innodb的存储目录

创建一个engineinnodb的数据库,观察存储目录

  1. 创建指定引擎的数据库 首先登录 MySQL,执行以下命令创建数据库(InnoDB 是 MySQL 5.5+ 后的默认引擎,但可以显式指定):
-- 创建数据库并指定字符集(引擎通过配置默认或表级指定)
mysql> create database innodb_demo character set utf8mb4 collate utf8mb4_general_ci;


-- 切换到该数据库
mysql> user innodb_demo;

-- 创建一张明确指定 InnoDB 引擎的表(验证引擎生效)
mysql> create table test_innodb(
    ->   id int primary key auto_increment,
    ->   name varchar(50)
    -> )engine=InnoDB;-- 显式指定 InnoDB 引擎
Query OK, 0 rows affected (0.04 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  1. 查找 MySQL 数据存储目录 InnoDB 数据库的文件通常存储在 MySQL 的数据目录中,可通过以下方式查询:

方法 1:通过 MySQL 命令查询

mysql> show variables like 'datadir';
  • 1.

执行后会返回类似结果(路径因系统而异):

mysql> show variables like 'datadir';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| datadir       | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.06 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  1. 查看 InnoDB 数据库的存储文件 进入上一步查询到的 datadir 目录(需要系统管理员权限),查看 innodb_demo 数据库对应的文件:
# 切换到数据目录(替换为你的实际路径)
cd /var/lib/mysql/

# 查看数据库目录
ls -ld innodb_demo/

```dart
root@wenksen-VMware-Virtual-Platform:/var/lib/mysql# ls
 auto.cnf        binlog.000031   binlog.000053    '#ib_16384_0.dblwr'
 binlog.000010   binlog.000032   binlog.000054    '#ib_16384_1.dblwr'
 binlog.000011   binlog.000033   binlog.000055     ib_buffer_pool
 binlog.000012   binlog.000034   binlog.000056     ibdata1
 binlog.000013   binlog.000035   binlog.000057     ibtmp1
 binlog.000014   binlog.000036   binlog.000058     innodb_demo
 binlog.000015   binlog.000037   binlog.000059    '#innodb_redo'
 binlog.000016   binlog.000038   binlog.000060    '#innodb_temp'
 binlog.000017   binlog.000039   binlog.000061     mysql
 binlog.000018   binlog.000040   binlog.000062     mysql.ibd
 binlog.000019   binlog.000041   binlog.000063     performance_schema
 binlog.000020   binlog.000042   binlog.000064     private_key.pem
 binlog.000021   binlog.000043   binlog.index      public_key.pem
 binlog.000022   binlog.000044   ca-key.pem        server-cert.pem
 binlog.000023   binlog.000045   ca.pem            server-key.pem
 binlog.000024   binlog.000046   client-cert.pem   sys
 binlog.000025   binlog.000047   client-key.pem    test1
 binlog.000026   binlog.000048   db1               test2
 binlog.000027   binlog.000049   db2               undo_001
 binlog.000028   binlog.000050   db3               undo_002
 binlog.000029   binlog.000051   debian-5.7.flag   wenksen-VMware-Virtual-Platform.pid
 binlog.000030   binlog.000052   helloworld
root@wenksen-VMware-Virtual-Platform:/var/lib/mysql# ls -ld innodb_demo/
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

进入数据库目录查看文件

cd innodb_demo/
ls -l
  • 1.
  • 2.
root@wenksen-VMware-Virtual-Platform:/var/lib/mysql# cd innodb_demo/
root@wenksen-VMware-Virtual-Platform:/var/lib/mysql/innodb_demo# ll
总计 120
drwxr-x---  2 mysql mysql   4096  7月 25 18:00 ./
drwx------ 14 mysql mysql   4096  7月 25 17:58 ../
-rw-r-----  1 mysql mysql 114688  7月 25 18:00 test_innodb.ibd
root@wenksen-VMware-Virtual-Platform:/var/lib/mysql/innodb_demo#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

InnoDB 存储文件说明:

  • 表结构文件test_innodb.frm(存储表结构定义,MySQL 8.0 后合并到 .ibd 文件)
  • 表数据和索引文件test_innodb.ibd(InnoDB 独立表空间文件,包含数据和索引)
  • 数据库目录innodb_demo/ 文件夹本身用于组织该数据库的所有表文件
  1. 补充说明
  • InnoDB 默认使用「独立表空间」(每个表一个 .ibd 文件),可通过 innodb_file_per_table 变量查看配置:
SHOW VARIABLES LIKE 'innodb_file_per_table'; -- 通常为 ON
  • 1.
mysql>   SHOW VARIABLES LIKE 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set (0.01 sec)

mysql>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 如果使用「系统表空间」,数据会存储在 datadir 下的 ibdata1 文件中(不推荐,不利于管理)。

🌠查看表结构

desc 表名;

示例:

【MySQL 数据库】MySQL 表的操作_字段_02

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int          | YES  |     | NULL    |       |
| name     | varchar(20)  | YES  |     | NULL    |       |
| password | char(32)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| assets   | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> select * from users;
+------+------+----------+------------+--------+
| id   | name | password | birthday   | assets |
+------+------+----------+------------+--------+
|    1 | a    | b        | 1982-01-04 | NULL   |
|    2 | b    | c        | 1984-01-04 | NULL   |
+------+------+----------+------------+--------+
2 rows in set (0.00 sec)

mysql> alter table users modify name varchar(60);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

🌠修改表

在项目实际开发中,经常修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。

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);
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

案例: 在users表添加二条记录

mysql> insert into users values(1,'a','b','1982-01-04'),(2,'b','c','1984-0104');
  • 1.
mysql> select * from users;
+------+------+----------+------------+
| id   | name | password | birthday   |
+------+------+----------+------------+
|    1 | a    | b        | 1982-01-04 |
|    2 | b    | c        | 1984-01-04 |
+------+------+----------+------------+
2 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

users表添加一个字段,用于保存图片路径

mysql> alter table users add assets varchar(100) comment '图片路径' after birthday;
  • 1.
mysql> alter table users modify name varchar(60);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int          | YES  |     | NULL    |       |
| name     | varchar(60)  | YES  |     | NULL    |       |
| password | char(32)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| assets   | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

插入新字段后,对原来表中的数据没有影响:

mysql> select * from users;
+------+------+----------+------------+
| id   | name | password | birthday   |
+------+------+----------+------------+
|    1 | a    | b        | 1982-01-04 |
|    2 | b    | c        | 1984-01-04 |
+------+------+----------+------------+
2 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 修改name,将其长度改成60
mysql> alter table users modify name varchar(60);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int          | YES  |     | NULL    |       |
| name     | varchar(60)  | YES  |     | NULL    |       |
| password | char(32)     | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| assets   | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 删除password列 注意:删除字段一定要小心,删除字段及其对应的列数据都没了
mysql> alter table users drop password;
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc users;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int          | YES  |     | NULL    |       |
| name     | varchar(60)  | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| assets   | varchar(100) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

to:可以省掉

  • name列修改为xingming
mysql> alter table employee change name xingming varchar(60); 
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc employee;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int          | YES  |     | NULL    |       |
| xingming | varchar(60)  | YES  |     | NULL    |       |
| birthday | date         | YES  |     | NULL    |       |
| assets   | varchar(100) | YES  |     | NULL    |       |
| password | char(20)     | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

🌠删除表

语法格式:

DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
  • 1.

示例:

drop table t1;
  • 1.

🚩总结