mysql8.0学习笔记基础篇

DDL语句

1. 查询所有数据库 : show databases;
2. 查询当前数据库:select databases();
3. 使用数据库:use 数据库名;
4. 创建数据库:create database [if not exists] 数据库名 [ default charset 字符集] [collate 排序规则];
5. 删除数据库:drop database [if exists] 数据库名;
6. 创建表语句:
create table employee
(
   id     int comment '自增字段',
   age    int comment '年龄',
   gender varchar(255) comment '性别',
   name   varchar(255) comment '姓名',
   number    int comment '员工编号',
   dep_id int comment '部门编号'
)comment'员工表';
7. 查询表结构:desc 表名;


在这里插入图片描述```

//8. 查询数据库所有表:
show tables;
//9.修改表名:
alter table 表名 rename to 新表名;
//10.查询指定表的建表语句:
show create table 表名;
//11.删除指定表:
drop table [if exists] 表名;
//12. 删除对应的表并重新创建:
truncate table 表名;
//13. 表中添加字段:
alter table 表名 add 字段名 数据类型 [comment 注释] [约束];
// 14.  删除表中字段:
alter table 表名 drop 字段名;
// 15. 修改表中字段数据类型
alter table 表名 modify 字段名 新数据类型(长度);
// 16. 修改字段名和字段类型:
alter table 表名 change 旧字段名 新字段名 类型(长度)[comment 注释] [约束];

DML语句

  1. 表中添加数据:
// 批量添加
insert into 表名 values (1,2,...),(1,2,...);
// 给指定字段添加数据
insert into 表名(字段名1,字段名2,...) values (1,2,...),(1,2,...);
  1. 删除数据
// 删除数据
delete from 表名 [where 条件] ;
  1. 修改数据
// 修改数据
update employee set 字段名 =1,字段名 =2,...[where 条件];

DQL查询语句

  1. DQL基本语法
    查询语句
  2. 基本查询语句
//查询多个字段
select 字段1,字段2,字段3 from 表名;
select * from 表名;
// 设置别名
select  字段1 [as 别名1], 字段2 [as 别名2]... from 表名;
//去除重复记录
select distinct 字段列表 from 表名;
//条件查询
select 字段列表 from 表名 where 条件列表;


3. 分组查询

// 分组查询语句
SELECT 字段列表 from 表名 [WHERE 条件] GROUP BY 分组字段名 [HAVING 分组后过滤条件]
//where 与 having区别
1> 执行时机不同,WHERE是分组之前进行过滤,不满足WHERE条件不参与分组,而HAVING是分组之后对结果进行过滤;
2> 判断条件不同,WHERE不能对聚合函数进行判断,而HAVING可以。
  1. 排序查询
// 排序查询语法
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;
//排序方式中默认为升序排序:ASC,DESC为降序
  1. 分页查询
// 查询语句
select 字段列表 from 表名 Limit 起始索引,查询记录数;


6. 查询语句的编写顺序与执行顺序

DCL语句

  1. 管理用户
// 查询用户
USE mysql;
SELECT * FROM user;
//创建用户
CREATE USER '用户名'@'主机名'IDENTIFIED BY '密码';
//修改用户密码
ALTER USER '用户名'@'主机名'IDENTIFIED WITH mysql_native_password BY '新密码';
//删除用户
DROP USER '用户名'@'主机名';
  1. 权限管理
// 查询权限
SHOW GRANTS FOR '用户名'@'主机名';
//授予权限
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';
//撤销权限
REVOKE 权限列表 ON 数据库名,表名 FROM '用户名'@'主机名';

约束

// 添加约束
create table 表名(
    字段名 数据类型,
    ...
    [constraint ][外键名称] foreign key (外键字段名) references 主表(主表列名)
);
//删除约束
alter table 表名 drop foreign key 外键名称;
//删除/更新行为
ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段) REFERENCES 主表名(主表字段名) ON UPDATE CASCADE ON DELETE CASCADE;

嵌套查询








事务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL 8 Cookbook: Over 150 recipes for high-performance database querying and administration Design and administer enterprise-grade MySQL 8 solutions MySQL is one of the most popular and widely used relational databases in the World today. The recently released MySQL 8 version promises to be better and more efficient than ever before. This book contains everything you need to know to be the go-to person in your organization when it comes to MySQL. Starting with a quick installation and configuration of your MySQL instance, the b ook quickly jumps into the querying aspects of MySQL. It shows you the newest improvements in MySQL 8 and gives you hands-on experience in managing high-transaction and real-time datasets. If you’ve already worked with MySQL before and are looking to migrate your application to MySQL 8, this book will also show you how to do that. The book also contains recipes on efficient MySQL administration, with tips on effective user management, data recovery, security, database monitoring, performance tuning, troubleshooting, and more. With quick solutions to common and not-so-common problems you might encounter while working with MySQL 8, the book contains practical tips and tricks to give you the edge over others in designing, developing, and administering your database effectively. What You Will Learn Install and configure your MySQL 8 instance without any hassle Get to grips with new features of MySQL 8 like CTE, Window functions and many more Perform backup tasks, recover data and set up various replication topologies for your database Maximize performance by using new features of MySQL 8 like descending indexes, controlling query optimizer and resource groups Learn how to use general table space to suit the SaaS or multi-tenant applications Analyze slow queries using performance schema, sys schema and third party tools Manage and monitor your MySQL instance and implement efficient

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值