ps:前部分内容在MySQL(一)
三、DML语句
创建数据库表:
# 创建数据库表
create table [if not exists] tab_name(
col_name datatype [comment '注释'],
col_name datatype
);
插入数据:
# 插入数据
# 如果前面有字段按照前面的字段顺序添加,没有按照默认的顺序添加
insert into 表名[(字段名称1,字段名称2,字段名称3)] value(value1,value 2,value 3);
insert into 表名[(字段名称1,字段名称2,字段名称3)] values(value1,value 2,value 3);
# 插入多条数据
insert into 表名 values (), (), ();
数据更新:
# 数据库更新
update 表名 set 字段1=value1, 字段2=value2, where 条件;
删除数据:
# 删除数据
# 删除满足条件的数据
delete from 表名 where 条件;
四、DQL语句
查询数据:
# 无条件查询
select *(字段1,字段2,字段3) from 表名;
# 等值查询
select * from 表名 where 字段名 = value;
# 条件查询
# 查询两个条件都满足的值
select * from 表名 where 条件1 and 条件2;
# 查询满足任意一个条件的值
select * from 表名 where 条件1 or 条件2;
# 查询满足条件的值
select * from 表名 where 字段 between value1 and value2;
select * from 表名 where 字段 in(value1,value2,value3,...);
select * from 表名 where 字段 not in(value1,value2,value3,...);
设置列别名:
# 设置列别名
select 字段名 [as] '别名' from 表名 WHERE 条件;
剔除重复行:
# 剔除重复行
# 只是在显示的时候不显示重复的数据,数据未删除
select distinct * from 表名;
算数运算:
# 对查询出来的结果进行运算
select 字段1+字段2 from 表名;
# 对正在修改的值进行运算
update 表名 set 字段=字段+value where 条件;
NULL值查询:
# NUll值查询
# NUll值无法通过等值操作查询
# NUll值无法直接进行算数运算,如果直接进行算数运算得到的结果也将是null
select * from 表名 where 字段 is null;
select * from 表名 where 字段 is not null;
# 在select语句中,不能对null值进行数值运算,如果强行运算的话计算出来的结果也将是null
# 如果想在select语句中对null值进行运算的话,可以采用以下方法:
if null(字段,0) #如果为空,将null赋值为0
#例如:
select (salary*12 + IF NULL(comm,0) * 12 ) AS 年薪 from emp;
模糊查询:
# 模糊查询
模糊查询中的like效率其实非常低,所以一般情况下不推荐使用模糊查询
# 模糊查询中'_'表示一个字符,'%表示多个字符'
select * from 表名 where 字段 like '_x_';
select * from 表名 where 字段 like '%x%';
select * from 表名 where 字段 like '_x%';
逻辑运算:
# 逻辑运算
# 逻辑运算中,下列where语句中可以写>,<,=,<=,>=,!=等
select * from 表名 where 字段 > value;
排序:
# 排序
# asc表示升序(默认就是升序),desc表示降序
select * from 表名 order by 字段 asc;
select * from 表名 order by 字段 desc;
分页查询:
# 分页查询
# 分页查询中,第一个数字表示查询的起始行,第二个数字表示每页有多少条数据
#如果页码用n表示,页大小用s表示,则limit (n-1)*s,表示下列limit 0,2
select * from 表名 where limit 0,2;
单行函数:
# 单行函数
# 计算长度
select length(字段) from 表;
# 大小写转换(upper表示转换成大写,lower表示转换为小写)
select upper(字段),lower(字段) from 表名;
# 字符串拼接(concat)
select concat(字段1,字段2) from 表名;
五、DCL语句
查看当前登录的用户:
# 查看当前登录的用户
# ps:dual为虚拟表,其主要目的就是为了让select语句完整
select user() [from dual];
创建新用户:
#创建本地新用户的代码
create user 用户名@localhost identified by '密码';
#创建其他主机新用户的代码
create user 用户名@'主机地址' identified by '密码';
修改密码:
# 修改用户密码
# ps:'password expire never'意为密码永不过期
alter user 用户名@'ip主机地址' identified by '密码' password expire never;
# 修改密码之后不需要重新登录,但在下次登陆的时候需要使用新密码登陆
查询用户信息:
# 查询用户信息
select user,host from mysql.user;
用新用户登录:
# 新创建的用户只有登录权限,需要使用管理员账户授权
mysql -u 用户名 [-h ip地址] -p
为用户授权:
# 查询用户的权限
show grant for 用户名@localhost;
#修改用户权限
# ps:# 授权的新用户需要重新登录才能使用新权限
grant all on 数据库名.表名 to 用户名@localhost;
# ps:上述语句的all代表所有权限,还可以为:
# select(查询权限)
# insert(插入权限)
# update(更新权限)
#####################################################
# 创建任意IP都可以访问的用户
grant all privileges on *.* to '用户名'@'%';
# 将用户更新为任意IP都可以访问的用户
update mysql.user set host='%' where user='用户名';
#创建成功后用下列语句让更改有的用户的权限更新
flush privileges;
删除用户:
# 删除用户
drop user 用户名@localhost