SQL指令

prompt 修改提示符名称

DDL

操作数据库

1.创建

​ create database 数据库名称;

​ create database if not exists 数据库名称;

​ create database 数据库名称 character set 字符集名;

2.查询

​ show databases;

​ show create database 数据库名称; 查询某个数据库的创建语句

3.修改

​ alter database 数据库名称 character set 字符集名称;

4.删除

​ drop database 数据库名称;

​ drop database if exists 数据库名称;

5.使用数据库

​ select database(); 查询正在使用的数据库

​ use 数据库名称; 使用数据库

操作表

1.创建

create table 表名(列名1 数据类型1,列名2 数据类型2, …. ,列名n 数据类型n);

create table 表名 like 被复制的表名; 表的复制

2.查询

show tables; 查询某个数据库里表的名称

desc 表名; 查询表的结构

3.修改

alter table 表名 rename to 新的表名; 修改表名

alter table 表名 character set 字符集名称; 修改字符集

alter table 表名 add 列名 数据类型; 添加一列

alter table 表名 change 列名 新的列名 新数据类型; 修改列名称 类型

alter table 表名 modify sex 新数据类型; 修改列类型

alter table 表名 drop 列名; 删除列

4.删除

drop table 表名;

drop table if exists 表名;

DML

1.添加数据

insert into 表名(列名1,列名2,….列名n) values(值1,值2,….值n);

insert into 表名 values(值1,值2,….值n);

2.删除数据

delete from 表名 [where 条件];

​ 删除全部记录:delete from 表名; 不推荐

​ truncate table 表名; 推荐

3.修改数据

update 表名 set 列名1 = 值1, 列名2 = 值2,….[where 条件];

DQL

select [distinct] [字段] [别名] , [分组字段] , [聚合函数] …from 表名 [where 条件] [分组查询] [having 条件] [排序查询] [分页查询]

distinct 去重复

ifnull(表达式1,表达式2)

1.条件查询

<,>,=,<=,>=,!=,<>

between …and…

in(A,B,C…)

is null is not null

not and or ! && ||

模糊查询:like _:单个字符 %:多个任意字符

2.排序查询

order by 排序字段1 排序方式1, 排序字段2 排序方式2…;

​ 排序方式:ASC 升序,默认 DESC 降序

3.聚合函数

聚合函数排除了null的值,解决方法 1选择非空 2ifnull函数

1.count()

1.一般选择非空的列:主键

2.count(*)

2.max()
3.min()
4.sum()
5.avg()

4.分组查询

group by 分组字段;

5.分页查询

limit 开始的索引,每页查询的条数;

​ 开始的索引 = (当前的页码 - 1)* 每页显示的条数

​ limit 分页操作是一个”方言’’

约束

对表中数据进行限定,保证数据的正确性、有效性和完整性

主键约束:primary key

1.非空且唯一 2.一张表只能有一个字段作为主键 3.主键就是表中唯一标识

create table 表名(列表名称 数据类型 primary key…); 创建时添加约束

alter table 表名 modify 列表名称 数据类型 primary key; 创建完后添加

alter table 表名 drop primary key; 删除约束

自动增长:auto_increment

​ create table 表名(列表名称 数据类型 primary key auto_increment…); 创建时添加自动增长

​ alter table 表名 modify 列表名称 数据类型 [primary key] auto_increment; 添加自动增长

​ alter table 表名 modify 列表名称 数据类型; 删除自动增长

非空约束:not null

create table 表名( 列表名称 数据类型 not null…); 创建时添加约束

alter table 表名 modify 列表名称 数据类型 not null; 创建完后添加

alter table 表名 modify 列表名称 数据类型; 删除约束

唯一约束:unique

create table 表名(列表名称 数据类型 unique…); 创建时添加约束

alter table 表名 modify 列表名称 数据类型 unique; 创建完后添加

alter table 表名 drop index 列表名称; 删除约束

外键约束:foreign key

create table 表名(…, constraint 外键名称 foreign key (外键列名称) references 主表名称(主表列名称)); 创建时添加外键约束

alter table 表名 add constraint 外键名称 foreign key (外键列名称) references 主表名称(主表列名称); 创建完后添加

alter table 表名 drop foreign key 外键名称; 删除外键

on update cascade 级联更新

on delete cascade 级联删除

数据库的备份与还原

1.命令行:

备份:mysqldump -u用户名 -p密码 数据库名称 > 保存的路径
还原:

1.登陆数据库

2.创建数据库

3.使用数据库

4.执行文件。source 文件路径

多表查询

1.内连接查询

1.隐式内连接:用where消除无用的数据

2.显式内连接: select 字段列表 from 表名 [inner] join 表名2 on 条件

从哪些表查以及合理的条件和查询的字段

2.外连接查询

1.左外链接

​ select 字段表 from 表1 left [outer] join 表2 on 条件

​ 查询左表所有数据以及其交集部分

2.右外连接

​ select 字段表 from 表1 right [outer] join 表2 on 条件

​ 查询右表所有数据以及其交集部分

3.子查询

查询中嵌套查询,称嵌套查询为子查询。

1.子查询的结果是单行单列的:

可以作为条件,使用运算符去判断。

2.子查询的结果是多行单列的:

子查询可以作为条件,使用运算符in来判断

3.子查询的结果是多行多列的:

子查询可以作为一张虚拟表

DCL

1. 管理用户

​ %:所有的主机 localhost:本地主机

  1. 添加用户:

    create user ‘用户名’ @ ‘主机名’ identified by ‘密码’;

  2. 删除用户:

    drop user ‘用户名’ @ ‘主机名’;

  3. 修改用户密码:

    update user set password = password(‘新密码’) where user = ‘用户名’;

    set password for ‘用户名’ @ ‘主机名’ = password(‘新密码’);

  4. 查询用户:

    use mysql;

    select * from user;

2.授权

  1. 查询权限:

    show grants for ‘用户名’ @ ‘主机名’;

  2. 授予权限:

    grant 权限列表 on 数据库名.表名 to ‘用户名’ @ ‘主机名’;

    grant all on . to ‘用户名’ @ ‘主机名’;

  3. 撤销权限:

    revoke 权限列表 on 数据库名.表名 from ‘用户名’ @ ‘主机名’;

赏

一个好奇的人