Mysql 基础知识

SQL 分类

DDL ( Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据
段、数据库、表、列、索引等数据库对象。常用的语句关键字主要包括create、drop、 alter 等。

DML ( Data Manipulation Language)语句:数据操纵语句,用于添加、删除、更新
和查询数据库记录,并检查数据完整性。常用的语句关键字主要包括insert、 delete、 update和select等。

DCL ( Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许
可和访问级别的语句。这些语句定义了数据库、表、字段、用户的访问权限和安全级别。主要的语句关键字包括grant、 revoke 等。

默认数据库的功能

默认的mysql 库含义

1
2
3
4
5
6
7
8
9
10
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
  • information schema: 主要存储了系统中的一些数据库对象信息,比如用户表信息、列信息、权限信息、字符集信息、分区信息等。
  • performance_schema:主要用于收集数据库服务器性能参数。
MySQL 5.5开始新增一个数据库:PERFORMANCE_SCHEMA,主要用于收集数据库服务器性能参数。并且库里表的存储引擎均为PERFORMANCE_SCHEMA,而用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表。MySQL5.5默认是关闭的,需要手动开启,在配置文件里添加:performance_schema=ON

查看 performance_schema 是否打开

1
2
3
4
5
6
7
mysql> show variables like 'performance_schema';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| performance_schema | ON |
+--------------------+-------+
1 row in set (0.01 sec)

查看表结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> desc voice_info;
+-----------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+-------------------+----------------+
| voice_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(100) | NO | | NULL | |
| send_date | timestamp | YES | | CURRENT_TIMESTAMP | |
| send_messages | int(1) | NO | | NULL | |
| send_phone | int(1) | NO | | NULL | |
| send_popo | int(1) | NO | | NULL | |
| send_stone | int(1) | NO | | NULL | |
| chat_id | int(32) | NO | | NULL | |
| time_out_minute | int(10) | NO | | NULL | |
+-----------------+------------------+------+-----+-------------------+----------------+
9 rows in set (0.00 sec)

查看详细的表定义信息

1
mysql> show create table user \G;
  • \G 含义:能够按照字段竖向排列。

追加字段 add

1
2
3
mysql> alter table voice_info add column age int(3);
Query OK, 0 rows affected (0.20 sec)
Records: 0 Duplicates: 0 Warnings: 0

修改字段名称和类型 modifychange

1
2
3
mysql> alter table voice_info change age age1 int(4);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

1
2
3
mysql> alter table voice_info modify age varchar(20);
Query OK, 90 rows affected (0.20 sec)
Records: 90 Duplicates: 0 Warnings: 0

注意: change 和modify都可以修改表的定义,不同的是change 后面需要写两次列名,不方便。但是change的优点是可以修改列名称,modify则不能。

删除字段drop

1
2
3
mysql> alter table voice_info drop column age1;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0

在某个字段前面追加字段

1
2
3
mysql> alter table voice_info add name varchar(20) after age;
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0

将某个字段移动到最前面

1
2
3
mysql> alter table voice_info modify age int(4) first;
Query OK, 90 rows affected (0.11 sec)
Records: 90 Duplicates: 0 Warnings: 0