文章目录

  • 一、DDL操作
  • 1.1 数据库操作
  • 1.2 表操作
  • 1.3 用户操作
  • 1.4 查看命令show
  • 1.5 获取帮助
  • 二、DCL操作
  • 2.1 用户授权
  • 2.2 查看授权
  • 2.3 取消授权
  • 三、DML操作
  • 3.1 插入insert
  • 3.2 查询select
  • 3.2.1 常规查询
  • 3.2.2 条件查询
  • 3.2.3 order by用法
  • 3.2.4 group by用法
  • 3.2.5 内连接&左连接&右连接
  • 3.2.5.1 内连接
  • 3.2.5.2 左连接
  • 3.2.5.3 右连接
  • 3.2.5.4 其他用法
  • 3.3 修改update
  • 3.4 删除delete
  • 3.5 删除truncate


一、DDL操作

1.1 数据库操作

1.查看数据库。

MariaDB [(none)]> show databases;

mysql 减法 避免负数 mysql加减乘除_数据库


2.创建数据库qingjun。

MariaDB [(none)]> create database if not exists qingjun;

mysql 减法 避免负数 mysql加减乘除_数据库_02


3.删除数据库qingjun。

MariaDB [(none)]> drop database if exists  qingjun;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_03

1.2 表操作

mysql 减法 避免负数 mysql加减乘除_webview_04

1.进入数据库,创建表student,id不能为空;名字长度不能超过10;年龄是整数,不能超过255岁。

MariaDB [(none)]> use test;
MariaDB [test]> create table student(id int not null,name varchar(10),age tinyint);

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_05


2.查看所有表。

MariaDB [test]> show tables;

3.查看表结构。

MariaDB [test]> desc student;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_06


4.创建表时添加主键,且默认值为1。

MariaDB [test]> CREATE TABLE IF NOT EXISTS baimu(id int not null default 1 primary key);

mysql 减法 避免负数 mysql加减乘除_linux_07


5.修改表结构,添加score字段,float为数据类型。

MariaDB [test]> alter table baimu add score float;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_08

6.修改表结构,删除id字段。

MariaDB [test]> alter table baimu drop id;

mysql 减法 避免负数 mysql加减乘除_linux_09


7.修改表结构,修改字段属性,将score字段的NULL类型为no。

MariaDB [test]> alter table baimu modify score float not null;

mysql 减法 避免负数 mysql加减乘除_linux_10

1.3 用户操作

1.创建用户wuhan,指定密码。本地需使用密码登录数据库。

MariaDB [(none)]> create user 'wuhan'@'127.0.0.1' identified by 'citms';

mysql 减法 避免负数 mysql加减乘除_linux_11


2.创建用户beijing,不设置密码。本地无密码登录数据库,进入数据库设置用户密码。

MariaDB [(none)]> create user 'beijing'@'localhost';

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_12

3.创建用户lisi,设置密码。远程主机登录数据库需要使用密码登录。

MariaDB [(none)]> create user  'lisi'@'192.168.130.161'   identified by 'citms';

mysql 减法 避免负数 mysql加减乘除_mysql_13


4.删除用户。

MariaDB [(none)]> drop user 'lisi'@'192.168.130.161';

mysql 减法 避免负数 mysql加减乘除_webview_14


5.查看所有用户。

select user,host from mysql.user;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_15

1.4 查看命令show

1.查看支持的所有字符集。

show character set;

mysql 减法 避免负数 mysql加减乘除_webview_16

2.查看当前数据库支持的所有存储引擎。

show engines\G

mysql 减法 避免负数 mysql加减乘除_webview_17

3.查看所有库。

show databases;

mysql 减法 避免负数 mysql加减乘除_linux_18


4.查看某个库的所有表。

show tables from test;

mysql 减法 避免负数 mysql加减乘除_webview_19


5.查看某个库的某张表结构。

desc test.baimu;

mysql 减法 避免负数 mysql加减乘除_mysql_20


6.查看创建库的过程。

show create database test;

mysql 减法 避免负数 mysql加减乘除_mysql_21


7.查看创建表的过程。

show create table test.baimu;

mysql 减法 避免负数 mysql加减乘除_mysql_22


8.查看某张表的状态。

show table  status like 'baimu'\G

mysql 减法 避免负数 mysql加减乘除_webview_23


9.查看表状态的其他用法。

//查看表名为student及其后面的所有字符长度的表状态,比如student1、student123等。
show table status like 'student%'\G;

//查看表名为student及其前面的所有字符长度的表状态,比如123student、filstudent等。
show table status like '%student'\G; 

//查看表名为student,且其后面以为字符长度的表状态,比如student1、studentp等。
show table status like 'student_'\G;

1.5 获取帮助

1.查看如何创建数据库。

help create database;

mysql 减法 避免负数 mysql加减乘除_mysql_24


2.查看如何创建表。

help create table;

mysql 减法 避免负数 mysql加减乘除_webview_25

二、DCL操作

权限类型

释义

ALL

所有权限

SELECT

读取内容的权限

INSERT

插入内容的权限

UPDATE

更新内容的权限

DELETE

删除内容的权限

表示方式

释义

*.*

所有库的所有表

db_name

指定库的所有表

db_name.table_name

指定库的指定表

注意事项:

  1. WITH GRANT OPTION权限不能随便使用, 被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户,所以不建议使用。
  2. mysql服务进程启动时会读取mysql库中的所有授权表至内存中,GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中,对于不能够或不能及时重读授权表的命令,需要手动执行命令刷新权限。

2.1 用户授权

1.当创建用户时没有设置密码时,可以在授权时设置密码。

//创建用户不设置密码。
create user 'baimu'@'192.168.130.161';

//给用户授予只能在192.168.130.161主机上能操作所有权限,并设置用户密码。
grant  all   on  *.*   to 'baimu'@'192.168.130.161' identified by 'citms'; 

//刷新权限。
flush privileges;

2.当创建用户设置了密码,可以直接授权,不需再次设置密码,也可以再次设置密码用来修改之前密码。

//创建用户设置密码。
create user 'qingjun'@'192.168.130.161' identified by 'citms';

//给用户授予只能在192.168.130.161主机上能操作所有权限。
grant all on *.* to 'qingjun'@'192.168.130.161';

//刷新权限。
flush privileges;

3.授权一种权限,针对所有资源。

//给用户授予只能在192.168.130.161主机上使用insert插入权限,允许可以对所有库中的所有表操作。
grant insert on *.* to 'qingjun'@'192.168.130.161';

//刷新权限。
flush privileges;

4.授权一种权限,针对部分资源。

//给用户授予只能在192.168.130.161主机上使用insert插入权限,只允许对text库中的所有表进行操作。
grant insert on test.* to 'qingjun'@'192.168.130.161';

//刷新权限。
flush privileges;

5.授权用户能在所有主机上访问。

//给用户授予只能在192.168.130.161主机上使用insert插入权限,只允许对text库中的所有表进行操作。
grant insert on test.* to 'qingjun'@'%';

//刷新权限。
flush privileges;

2.2 查看授权

1.查看当前登录用户的授权信息。

show grants;

mysql 减法 避免负数 mysql加减乘除_数据库_26


2.查看指定用户qingjun的授权信息。

show grants for 'qingjun'@'192.168.130.161';

mysql 减法 避免负数 mysql加减乘除_webview_27

2.3 取消授权

1.删除baimu用户对数据库的select权限。

revoke select   on *.* from 'baimu'@'192.168.130.161';

//刷新权限。
flush privileges;

mysql 减法 避免负数 mysql加减乘除_数据库_28

三、DML操作

  • DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。

3.1 插入insert

1.插入一条数据。

//查看表结构。
desc student;

//往student表中插入一条数据
insert student value('2','zhangsan','22');

//查看表内容。
select * from student;

mysql 减法 避免负数 mysql加减乘除_webview_29


2.插入多条数据。

//查看表结构。
desc student;

//往student表中插入2条数据
insert student values('3','lisi','20'),('4','wangwu','30');

//查看表内容。
select * from student;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_30


3.只插入一个字段值。

desc student;

//往student表中插入1条数据,只包含一个字段值,其余字段默认为空。
insert student(name) value('haha');

//查看表内容。
select * from student;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_31

3.2 查询select

3.2.1 常规查询

1.一般用法。

//查看表所有字段内容。
select * from student;

//查看表中name字段内容。
select name from student;

//查看表中id,name字段内容。
select id,name from student;

mysql 减法 避免负数 mysql加减乘除_linux_32

2.使用as更改字段别名查询。

//把原有的id,name,age分别改成编号,名字,年龄。
select id as 编号,name as 名字,age as 年龄 from student;

//id不改,把name,age分别改成n,a。
select id,name as n,age as a from student;

//查看age最大的名字和年龄。
select name,max(age) as age from student;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_33

3.2.2 条件查询

操作类型

常用操作符

操作符

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

BETWEEN

AND

LIKE:模糊匹配

RLIKE:基于正则表达式进行模式匹配

IS NOT NULL:非空

IS NULL:空

条件逻辑操作

AND

OR

NOT

1.一般用法。

//查找age>25的字段所有内容。
select * from student where age >25;

//查找age>30的name内容。
select name from student where age >30;

mysql 减法 避免负数 mysql加减乘除_webview_34


2.between用法:查找age在10~30之间的所有内容。

select * from student where age between 10 and 30;

mysql 减法 避免负数 mysql加减乘除_webview_35

3.and 的用法:查找name=tom,且age>25的所有内容。

select * from student where name = 'tom' and age > 25;

mysql 减法 避免负数 mysql加减乘除_mysql_36


4.or的用法:查找name=tom,或者age>25的所有内容。

select * from student where name='tom' or age > 25;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_37


5.like用法。

//查看name的结尾像u的结果。
select * from student where name like '%u';

//查看name的开头像l的结果。
select * from student where name like 'l%';

mysql 减法 避免负数 mysql加减乘除_linux_38


6.rlike用法,基于正则表达式。

//查看以m结尾的结果.
select * from student where name rlike 'm$';

//查看以l开头的结果。
select * from student where name rlike '^l'; 

//每个点代表一个字符,此处并没有表示开头结尾,所有会匹配4个字符以上的所有结果。
select * from student where name rlike '....'

//每个点代表一个字符,此处表示了开头结尾,只会匹配4个字符的所有结果.
select * from student where name rlike '^....$';

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_39


7.NULL和NOT NULL的用法。

//查询age为空的字段内容。
select * from student where age is NULL;

//查询age不为空的字段内容。
select * from student where age is NOT NULL;

mysql 减法 避免负数 mysql加减乘除_webview_40

3.2.3 order by用法

语法

意义

ORDER BY ‘column_name’

根据column_name进行升序排序

ORDER BY ‘column_name’ DESC

根据column_name进行降序排序

ORDER BY ’column_name’ LIMIT 2

根据column_name进行升序排序 并只取前2个结果

ORDER BY ‘column_name’ LIMIT 1,2

根据column_name进行升序排序 并且略过第1个结果取后面的2个结果

1.升序,使用asc,默认省略了。

//查询student表中age字段以升序方式排列的结果。
select * from student where age is not NULL order by age asc;

mysql 减法 避免负数 mysql加减乘除_mysql_41


2.降序,使用desc。

//查询student表中age字段以降序方式排列的结果.
select * from student where age is not NULL order by age desc;

mysql 减法 避免负数 mysql加减乘除_linux_42


3.根据降序结果只取前面3个结果。

//查询student表中age字段以降序方式排列,且只取前三个值.
select * from student where age is not NULL order by age desc limit 3;

mysql 减法 避免负数 mysql加减乘除_mysql_43


4.根据升序结果,跳过前两个结果后取3个结果。

//查询student表中age字段以升序方式排列,去掉最前面的2个值,取第三个到第五个的三个值.
select * from student where age is not NULL order by age limit 2,3;

mysql 减法 避免负数 mysql加减乘除_mysql_44

3.2.4 group by用法

1.创建一个info的表,有名称、工资、部门三列内容。

create table info(id int not null primary key auto_increment,name varchar(50) not null,department varchar(100),salary int);

mysql 减法 避免负数 mysql加减乘除_webview_45


2.插入内容。

insert info(name,department,salary) values('tom','sales',5000),('jerry','dev',20000),('zhangshan','dev',10000),('lisi','sales',30000),('wangwu','office',8000),('qianliu','financial',10000),('zhaoqi','financial',20000),('sunba','operation',25000),('zhoujiu','operation',9000),('wutian','dev',15000);

mysql 减法 避免负数 mysql加减乘除_数据库_46


3.查看名字,部门,最高工资,再将这一结果组成个新组(group by的用法)

select name,department,max(salary) as salary from info group by department;

mysql 减法 避免负数 mysql加减乘除_mysql_47


4.查看名字,部门,最低工资,再将这一结果组成个新组(group by的用法)

MariaDB [maqiang]> select name,department,min(salary) as salary from info group by department;

mysql 减法 避免负数 mysql加减乘除_webview_48

3.2.5 内连接&左连接&右连接

1.给info表新增一个字段jn,并修改jn字段值。

//新增字段。
alter table info add jn int not null;

//修改字段值。
update info set jn = 1 where id = 1;
update info set jn = 2 where id = 2;
update info set jn = 3 where id = 3;
update info set jn = 4 where id = 4;
update info set jn = 5 where id = 5;
update info set jn = 6 where id = 6;
update info set jn = 7 where id = 7;
update info set jn = 8 where id = 8;
update info set jn = 9 where id = 9;
update info set jn = 10 where id = 10;

mysql 减法 避免负数 mysql加减乘除_mysql_49


2.创建第二张表格basic_info,该表格里定义字段job_number与第一张表里的jn是一个意思。

//创建表。
create table basic_info(job_number int not null,age int not null);

//往basic_info表里插入数据。
insert basic_info values(1,21),(3,25),(5,30),(7,26),(9,28),(10,30),(2,20);

mysql 减法 避免负数 mysql加减乘除_linux_50

3.2.5.1 内连接
  • 内连接:使用inner join,取两张表格共有的值,再生成一张新表。

1.第一种查看方法是没有改名字,用的where。

select * from info inner join basic_info where info.jn = basic_info.job_number;

mysql 减法 避免负数 mysql加减乘除_mysql_51


2.第二种查看方法是把info改成a,basic_info改成b,用的on。

select * from info as a inner join basic_info as b on a.jn = b.job_number;

mysql 减法 避免负数 mysql加减乘除_数据库_52

3.2.5.2 左连接
  • 左连接: 使用left join,把左边(第一张表)有的字段内容取出来,左边有但右边没有的字段补齐并留空

1.优先看jn列,先取第一张表的jn列的所有字段。此处的4,6,8 第一张表有,但第二张表没有,所以其对应的字段内容留空。

select * from info as a left join basic_info as b on a.jn = b.job_number;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_53

3.2.5.3 右连接
  • 右连接:使用right join,把右边(第二张表)有的字段内容取出来,右边有但左边没有的字段补齐并留空。

1.优先看job_number列,先取第二张表的job_number列的所有字段。这里没有留空部分是因为第二张表有的字段值在第一张表里都有,无法留空。

select * from info as a right join basic_info as b on a.jn = b.job_number;

mysql 减法 避免负数 mysql加减乘除_webview_54


2.现在在第二张表里job_number字段插入11,第一张表jn字段没有11。

insert basic_info value(11,35);

mysql 减法 避免负数 mysql加减乘除_数据库_55


3.执行右连接,此时会把11的值取出来并留空。优先看job_number列,先取第二张表的job_number列的所有字段。此处的11值在第二张表有,但第一张表没有,所以其对应的字段内容留空。

select * from info as a right join basic_info as b on a.jn = b.job_number;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_56

3.2.5.4 其他用法

1.取两张表里的一个tom内容。

  • 以info和basic_info两张表做对比,取第一张表中name=tom,且第一张表中的jn值=第二张表中的job_number值。
select * from info,basic_info where info.name = 'tom' and info.jn = basic_info.job_number;

mysql 减法 避免负数 mysql加减乘除_linux_57

2.取两张表里的一个内容的某一项(只取name和age)

  • 条件一:从第一张表info里取name字段的某个值,从第二张表basic_info里age字段的某一个值。
  • 条件二:从第一张表info里取的name字段值=tom。
  • 条件三:第一张表info的jn字段值 = 第二张表basic_info的job_number字段值。
select info.name,basic_info.age from info,basic_info where info.name = 'tom' and info.jn = basic_info.job_number;

mysql 减法 避免负数 mysql加减乘除_mysql 减法 避免负数_58

3.3 修改update

1.修改student表里的age=NULL所在的记录对应的name字段值为hehe。

update student set name = 'hehe' where age is NULL;

mysql 减法 避免负数 mysql加减乘除_数据库_59


2.修改student表里的age=NULL所在的记录对应的age字段值为120。

update student set age = 120 where age is NULL;

mysql 减法 避免负数 mysql加减乘除_mysql_60

3.4 删除delete

1.删除student表里的id=2的那一行的数据。

delete from student where id = 2;

mysql 减法 避免负数 mysql加减乘除_linux_61


2.删除表里所有内容。

delete from student;

mysql 减法 避免负数 mysql加减乘除_linux_62


3.删除baimu这张表。

drop table  baimu;

mysql 减法 避免负数 mysql加减乘除_mysql_63

3.5 删除truncate

语句类型

特点

delete

DELETE删除表内容时仅删除内容,但会保留表结构。

DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项。

可以通过回滚事务日志恢复数据。

非常占用空间。

truncate

删除表中所有数据,且无法恢复。

表结构、约束和索引等保持不变,新添加的行计数值重置为初始值。

执行速度比DELETE快,且使用的系统和事务日志资源少。

通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放。

对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据。

不能用于加入了索引视图的表。

1.使用delete删除表内容,再插入新内容。此时再创建字段时,其id接着之前的来。删除之前最后一个id为10,那么用delete删除之后再创建时的id为11。

//删除整个表内容。
delete from info;

//新插入一条内容。
insert info(name,department,salary) values('tom','sales',5000);

mysql 减法 避免负数 mysql加减乘除_数据库_64

2.使用truncate删除表内容,再插入新内容,新创建字段其id会重置为1。

//删除表内容。
truncate info;

//新插入一条内容。
insert info(name,department,salary) values('tom','sales',5000);

mysql 减法 避免负数 mysql加减乘除_linux_65