Mysql笔记

Mysql笔记

数据库的创建和管理

创建数据库

1
2
3
4
5
6
7
create database 数据库名字
default character set 字符集名字
Default collate 排序规则名 ;
例子:
create database webinfo
default character set utf8mb4
Default collate utf8mb4_general_ci;

显示当前所有的数据库列表

1
Show databases;

指定默认的数据库列表

Use 数据库名字;

1
use stuinfo;

使用alter database 语句修改数据库

1
2
3
4
5
6
7
Alter database 数据库名字
Default character set 字符集名
Default collate 排序规则名字;
例子:
Alter database stuinfo
Default character set utf8
Default collate utf8_general_ci;

删除数据库

1
Drop database 数据库名字;

数据表的创建和管理

使用create table 语句创建数据表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Create table 表名 (
字段名1 数据类型 \[属性\] \[索引\] ,
字段名1 数据类型 \[属性\] \[索引\],
.......
字段名1 数据类型 \[属性\] \[索引\]
);
例子:

Create table student (
Id int unsigned not null auto_increment comment '学生ID',
sNo char(10) not null comment '学号',
sName varchar(20) not null comment '姓名',
sex char(1) not null default '男' comment '性别',
birthday Date not null comment '出生日期',
depyName varchar(30) not null comment '班级名称',
remark Varchar(80) comment '备注',
primary key (id), / \*设置id为主键\*/
unique (sNo), / \*设置sNo为唯一性索引\*/
index (sName) / \*设置sName为普通索引\*/
); ENGINE=InnoDB;

Not null 不为空 unsigned无符号 auto_increment自动增加

查看数据表

1
Show tables;

复制数据表

1
Create table 新表名 like 旧表名;

创建临时表

新建之后,当mysql关闭后会自动删除

1
2
3
4
5
6
7
8
9
10
11
12
Create temporary table student (
Id int unsigned not null auto_increment comment '学生ID',
sNo char(10) not null comment '学号',
sName varchar(20) not null comment '姓名',
sex char(1) not null default '男' comment '性别',
birthday Date not null comment '出生日期',
depyName varchar(30) not null comment '班级名称',
remark Varchar(80) comment '备注',
primary key (id), / \*设置id为主键\*/
unique (sNo), / \*设置sNo为唯一性索引\*/
index (sName) / \*设置sName为普通索引\*/
); ENGINE=InnoDB;

Not null 不为空 unsigned无符号 auto_increment自动增加

查看表结构

Describe | desc 表名;

例子:

1
desc 表名;

查看表的结构 sql语句

1
Show create table 表名;

修改表结构

使用alter table 修改表结构

Alter table 表名

Add字段名 数据类型 [属性] [索引] [First | after 字段名] –添加新字段

Modify 字段名 数据类型 [属性] [索引] ---更改指定数据类型

Change 字段名 新字段名 数据类型 [属性] [索引] ---更改指定数据类型同时更改名字

Drop 字段名 ----删除指定字段

Rename as 新表名 ---用来给数据表重新命名

例子:

使用insert操作表中数据

Insert into 表名 (字段名1,字段名2,字段名3)

Values (值1, 值2, 值3 ), (值1, 值2, 值3 ), (值1, 值2, 值3 )

例子:

使用update修改表中数据

Update 表名

Set 字段名1=值1 , 字段名2=值2[,…]

[where 条件]

使用delete删除表中数据

Delete from 表名

[where 条件]

例子:

1
Delete from student where sNo='1308013105';

会删除所在字段(sNo)的那一行的数据,不是只单单删除sNo

使用truncate语句清空表中数据

Truncate [table] 表名

删除表

1
2
Use 数据库名字;
Drop table 表名;

创建索引

定义

MySQL 的索引类型主要有以下几种。

• 普通索引(NDEX):最基本的索引,它没有任何限制,是用来提升数据库性能、提高

数据查询效率的一项重要的技术。

• 唯一性索引(UNIQUE)):索引列的值必须唯一,但允许有空值。一张表中可以有多个

唯一性索引。如果是组合索引,则列值的组合必须唯一。

• 主键索引(PRIMARY KEY):一种特殊的唯一性索引,但不允许有空值。一张表中只能

有一个主键。为了有效实现数据的管理,每张表都应该有自己的主键,一般是在建表的

同时创建主键索引。

• 全文索引(FULLITEXT):主要用来查找文本中的关键字,而不是直接与索引中的值相

比较。全文索引跟其他索引大不相同,它更像是一个搜索引擎,而不是简单的 WHERE

语句的参数匹配。全文索引配合 MATCH AGAINST 操作使用,而不是一般的 WHERE

语句加 LIKE。目前只有 CHAR、VARCHAR、TEXT 列上可以创建全文索引。

在create table创建索引

Create table 表名 (

字段名1 | 索引项…,

)

索引项的语法:

Primary key 索引名 (字段名)

Unique 索引名 (字段名)

Index | key 索引名 (字段名)

Fulltext 索引名 (字段名)

例子

1
2
3
4
5
6
7
8
9
10
11
12
Create table student (
Id int unsigned not null auto_increment comment '学生ID',
sNo char(10) not null comment '学号',
sName varchar(20) not null comment '姓名',
sex char(1) not null default '男' comment '性别',
birthday Date not null comment '出生日期',
depyName varchar(30) not null comment '班级名称',
remark Varchar(80) comment '备注',
primary key (id), / \*设置id为主键\*/
unique (sNo), / \*设置sNo为唯一性索引\*/
index (sName) / \*设置sName为普通索引\*/
); ENGINE=InnoDB;

Not null 不为空 unsigned无符号 auto_increment自动增加

使用alter table 语句创建索引

Alter table 表名

​ Add 索引项;

例子:

1
2
3
Alter table course 
Add unique ux_cNo(cNo),
Add index ix_cName(cName);

使用create index语句创建索引

Create [unique] | [fulltext] index 索引名

On 表名 (字段名)

例子:

在成绩表上创建唯一性索引(组合索引)

1
2
Create unique index ux_sId_cId
ON score (sId,cId);

使用Show index 语句查看索引

Show index from <表名> [from <数据库名字>]

例子:

1
Show index from student;

查看学生表中的索引

Select查询

应该是到这会用到sql语句,[点此下载]

选择字段进行查询

Select 字段1 [,字段2,字段3] from 表名;

例子:

1
Select deptname,name,sNo,sex from student;

定义字段别名

1
Select sNo AS '学号' , sName AS '姓名' from student;

条件查询

使用like模糊查询

使用in 进行范围查询

使用order by 子句对查询结果

降序

中文名拼音排序

先按班级升序排列,同一个班级内出生日期降序排列

使用limit子句限制返回记录的行数

使用distinct关键字过滤重复记录

内连接

使用统计函数对数据进行统计汇总

使用group by进行分组查询

使用having子句对分组汇总结果进行筛选

使用exists关键字创建子查询

查询选修课程的女生名单

薄弱盲区

复制表到新表

向表中插入子查询结果

带子查询的修改语句

带子查询的删除语句

特别声明
千屹博客旗下的所有文章,是通过本人课堂学习和课外自学所精心整理的知识巨著
难免会有出错的地方
如果细心的你发现了小失误,可以在下方评论区告诉我,或者私信我!
非常感谢大家的热烈支持!

Mysql笔记
https://blog.qianyios.top/posts/43761/
作者
严千屹
发布于
2023年1月20日
更新于
2024年9月14日
许可协议