mysql基础语句

0 阅读4分钟

DDL

data definition language数据定义语言

开启

sudo :暂时使用管理员权限(sudo systemctl start mysqld),只有在信任名单(sudoers)里才可用

mysqld :d的意思是daemon(守护进程),用于curd的工具,mysql更倾向于下达指令

名称角色类比你的操作
**<font style="color:rgb(68, 71, 70);">mysql</font>**客户端 (Client)手机上的 App你敲 SQL 语句的地方,用来下达指令。
**<font style="color:rgb(68, 71, 70);">mysqld</font>**服务端 (Daemon)后台服务器真正干活的人。负责存取数据、处理逻辑。
  • 当你执行 systemctl start mysqld 时,你是在启动后台引擎
  • 当你执行 mysql -u root -p 时,你是在打开一个窗口去连接那个引擎。

数据库操作

show databases: 查看数据库

use (数据库名):进入该数据库

select database() :查看当前数据库

create database if not exists [default character set utf8mb4] [collate utf8mb4_general_ci]:建库,【编码格式】【排序规则】

drop database [if exists](数据库名):删除数据库

用于建造结构,数据库和表结构

主要命令:show alter drop

表操作

show tables:查看所有表

create table (表名)(

字段1 类型 [comment 注释],

...

)[comment 表注释]:建表

:::info 注释要加‘’;

:::

desc (表名):显示表结构

show create table (表名):显示建表语句


alter table (表名) add (字段名 类型)【comment 注释】【约束】;:新增表结构

alter table (表名) modify 字段名 新类型;:修改字段类型

alter table (表名 )change (旧字段名 新字段名 类型)【comment 注释】:修改字段名和类型

alter table (表名)drop (字段名):删除字段

alter table (表名)rename to (新表名):修改表名

删除表

drop table 【if exists】(表名):删表

truncate table 表名;:删除并重建表

类型

:::info MySQL 中的数据类型主要分为三类:数值类型、字符串类型、日期时间类型。

:::

DML

操作表内数据 Data Manipulation Language

主要命令 :insert update delete

:::info 字符串和日期数据应该包含在引号中

:::

insert

insert into (表名) values (值1,值2,...):给全部字段插入数据

insert into 表名(字段名)values (值1,值2,...)(值1,值2,...)...:给选定字段批量加入数据

insert into (表名)values (值1,值2,...)(值1,值2,...)...:给全部字段批量加入数据

update

update (表名)set 字段名1=值1,...【where 条件】:更改符合条件的具体字段值

delete

delete from (表名)【where 条件】:删除指定条件的该条数据

DQL

查询表内数据Data Query language

主要命令:select

select 字段名1,字段名2,... from (表名);

select * from (表名);:查询

select 字段名1 别名1,... from (表名);

select distinct 字段名1,...from (表名);

select 字段名 from 表名 where 条件;:条件查询

条件

:::info not用于not in之类的

:::

聚合函数

:::info null值不参与聚合函数运算

:::

select 聚合函数(字段明1,...)from (表名);

分组查询

select 字段列表 from (表名) where 条件 group by 分组字段名 【having 分组后过滤条件】;

where 与 having 区别

  • 执行时机不同:<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">where</font> 是分组之前进行过滤,不满足 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">where</font> 条件,不参与分组;而 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">having</font> 是分组之后对结果进行过滤。
  • 判断条件不同:<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">where</font> 不能对聚合函数进行判断,而 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">having</font> 可以。

举例子

-- 3.查询年龄小于 45 的员工,并根据工作地址分组,获取员工大于等于 3 的工作地址
select workaddress, count(*) from emp where age < 45 group by workaddress having count(*) >= 3;

排序查询

select 字段列表 from 表名 order by 字段1 排序方式1,...;

ASC升序,默认升序

DESC降序

执行顺序

DCL

访问权限控制Data Control language

管理用户

查询用户

use mysql;
select * from user;//权限啥的都有,略多
SELECT host, user, plugin FROM mysql.user;#只看核心字段
plugin插件的意思

创建用户

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

修改用户密码

alter user ‘用户名’@‘主机名’identified with mysql_native_password by ‘新密码’;

//mysql_native_password是原密码存储在数据库的名称

删除用户

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

:::info 主机名可以使用 <font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">%</font> 通配

:::

权限控制

查询权限

show grants for '用户名'@'主机名'

授予权限

grant 权限列表 on 数据库名.表名 to '用户名'@'主机名' 【with grant option】;

撤销权限

revoke 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';