MySQL基础知识
图
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,属于Oracle旗下产品,MySQL是最流行的关系型数据库管理系统之一,在WEB应用方面,MySQL是最好的RDBMS应用软件之一

-- 查看数据库
show databases;

-- 查看当前数据库
select database();

-- 切换数据库
use database_name;

-- 创建数据库
create database database_name;

-- 创建数据库并指定编码
create database database_name default character set utf8mb4 collate utf8mb4_general_ci;

-- 删除库
drop database database_name;

-- 查看表
show tables;

-- 查看表结构
show columns from table_name;
-- 或
discribe table_name;
-- 或
desc table_name;

-- 创建表
create table dept(
dept_id int primary key auto_increment comment '部门ID',
dept_name varchar(100) not null  comment 'xxx',
dept_address varchar(100)  comment 'xxx',
dept_age int default 0  comment 'xxx'
)  comment '部门表';

-- 删除表
drop table name;

-- 修改表名
alter table old_name rename to new_name;

--查询建表语句
show create table table_name;

--给表加注释

ALTER TABLE `your_table_name` COMMENT = 'your_table_comment';

-- 添加
alter table table_name
add column_name datatype

-- 删除
alter table table_name 
drop column column_name

-- 修改列名
alter table table_name
rename column old_name to new_name 

-- 改变列数据类型
alter table table_name
alter column column_name datatype

insert into table_name(col,xxx) values(col_value,xxx)

delete from table_name where xxxx
-- 删除null字段
delete from table_name where xx is null

update table_name set xxx=xxx where xxx

select xx 
from table_name
where xxx
group by xxx
having xxx
order by xxx
limit xxx

where

操作符

操作符 作用
= 等于
<> 不等于
!= 不等于
< 小于
<= 小于等于
> 大于
>= 大于等于
between 在指定的两个值之间

如:

select xxx
from xxx
where xxx between 5 and 10

多条件

有序多条件:使用andor连接,两者同时使用时,or条件加上()

无序多条件:使用in指定条件值,也可使用not否定条件

通配符

提醒:不区分大小写

%:任何字符出现任意次数

where xxx like 'xxx%'(xxx开头)
where xxx like '%xxx'(xxx结尾)
where xxx like '%xxx%'(中间)
where xxx like 'x%x'(两头)

_:匹配单个字符

where xxx like 'x_x'

正则

where xxx regexp '1000|2000|3000'
where xxx regexp '[123]000'
where xxx regexp '[1-3]000'
where xxx regexp '.' 任意
where xxx regexp '^' 文本的开始
where xxx regexp '$' 文本的结尾

如果需要区分大小写
where xxx regexp binary ''

order by

排序,默认升序asc(可省略),降序使用desc

select xx 
from table_name
where xxx
order by xx desc
或者多条件
order by xx desc, xx asc
如果order by的不是数字,而是varchar类型,需要进行cast转换,也可以多条件
order by cast(xx as int) desc, cast(xx as int) desc

可多字段排序

select xx 
from table_name
where xxx
order by xx desc,xx asc

limit

限制数据条数

select xx 
from table_name
where xxx
limit 10,10

limit限制从第N条开始的第M条数据,N从0开始计数。

注意:直接使用limit 10,10的形式分页效率不高,当类似limit 100000,10时,会先检索前100000条在取后10条

优化方式为使用where先过滤

select xx 
from table_name
where xxx and id>xxx
limit 10

group by

分组

select xxx 
from xxx
group by xxx
having xxx

having用于过滤分组后的数据

字段拼接

select concat(name,'-',age) user from user;
 select concat(rtrim(name),'-',age) user from user;(去空格)

结果
+--------+
| user   |
+--------+
| tom-20 |
| tom-22 |
+--------+

聚合函数

函数 作用
avg 平均值
count 行数
max 最大值
min 最小值
sum 列数值总数

distinct去重

select distinct name from xxx

用户管理

-- 查询用户
use mysql;
select * from user;

-- 创建用户
create user '用户名'@'主机名' identifled by '密码'

-- 修改用户密码
alter user '用户名'@'主机名' identifled with mysql_native_password by '密码'

-- 删除用户
drop user '用户名'@'主机名'

-- 可以使用%代代表访问任意主机