MySQL数据库增删改查及安全值守操作

我在本篇文章中整理了一些MySQL数据库、表、字段的增删改查和安全值守操作的常用语句,以便同学们保存和快捷查询,详细如下:

(1)用户的查看、创建、删除
-- 以username为Superman,password为123123为例

①用户的创建
create user [email protected] identified by '123123';

②用户的删除
drop user [email protected];

③查看所有用户
use mysql;
select user,host from user;

④查看当前用户
select user();

(2)用户权限的查看、赋予、剥夺
①查看用户权限
show grants for [email protected];

②用户权限赋予
grant all privileges on student.* to [email protected] identified by '123123' with grant option;
-- student.*表示数据库student的所有表,grant为赋权,with option表示该用户也可以给其他用户授权

③用户权限剥夺
revoke all privileges on student.* from [email protected];

(3)库的增删查
-- 以student为数据库名为例
①数据库的创建
create database if not exists student;

②数据库的使用/查看
use student;
show tables;

③数据库的删除
drop database student;

(4)表的增删改查
-- 以stuinfo为表名为例
①表的创建
create table if not EXISTS `stuinfo` (
`sid` int(11) UNSIGNED PRIMARY key auto_increment,
`sname` varchar(20) not null,
`age` int,
`sex` enum('m','f') DEFAULT 'm',
`birth` datetime not null,
`email` varchar(50),
`addr` VARCHAR(50),
`tel` varchar(11)
)

②查看表的结构
desc stuinfo;

③查看表的内容
select * from stuinfo;

④删除一张表
drop table stuinfo;

⑤删除表中的内容
delete from stuinfo;

⑥向表中添加数据
insert into stuinfo (sname,age,sex,birth,email,addr,tel,bz) values('擎天柱',300,'m','1990-01-01','[email protected]','赛博坦','13012344321','汽车人领袖');

⑦修改表中的数据
update stuinfo set sname='大黄蜂' where sname='擎天柱';

(5)字段的增删改
①向表中增加一个字段(一列)
alter table stuinfo add beizhu varchar(50) comment '备注';

②删除表中的一个字段(一列)
alter table stuinfo drop beizhu;

③修改字段的名称
alter table stuinfo change beizhu bz varchar(20);

④修改字段的属性
alter table stuinfo modify bz varchar(50);

(6)元数据查询
①查看服务器版本信息
select version();

select @@version;

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

③查看当前用户名
select user();

④查看服务器状态
show status;

⑤查看服务器配置变量
show variables;

⑥查看数据文件存放位置
show global variables like '%datadir%';

⑦查看数据库的路径
select @@datadir;

⑧查看mysql的安装路径
select @@basedir;


(7)表中的数据查询
①查询所有姓名为“大”开头的学生信息
select * from stuinfo where sname like '大%';

②查询年龄大于20(含20)的学生信息
select *from stuinfo where age>=20;

③查询所有住在赛博坦或地球的学生信息
select * from stuinfo where addr='赛博坦' or addr='地球';

select * from stuinfo where addr in('赛博坦','地球');

④查询没有填写邮箱信息的学生
select * from stuinfo where email is null;

⑤Union联合查询(多条查询结果集,可实现跨表查询)
-- 要求结果集中的列数一致
-- 取出的最终列名,以第一条查询语句为准
-- union查询默认去重,如果不想去重,则使用union all
use student;
create table temp1(
uid int primary key auto_increment,
uname varchar(20) not null
)

insert into temp1(uname) values('横炮'),('铁皮'),('救护车');

create table temp2(
eid int primary key auto_increment,
ename varchar(20) not NULL
)

insert into temp2(ename) values('汽车人'),('汽车人'),('汽车人');

select * from temp1 union select * from temp2;
select * from temp1 union select 1,2;

⑥把查询结果放到同一行返回
select sid,group_concat(addr,bz) from stuinfo group by sid;

select sid as bianhao,group_concat(addr,bz) as xinxi from stuinfo group by sid;

⑦从m行开始,返回m+n行
select * from stuinfo limit 0,1;

⑧分组查询(聚合查询)
group_concat()函数

(8)常用字符串函数
①将字符串全部转换为小写字母返回
select lower('ABcd');

②将字符串全部转换为大写字母返回
select upper('ABcd');

③将多个字符串首尾相连后返回
select concat('123','abc');

④将多个字符串用指定连接符首尾相连后返回
select concat_ws(':','123','abc');

⑤从字符串的某个指定位置开始截取字符串并返回
select substr('flash',2,3);

select mid('flash',2,3);

⑥返回字符串的存储长度
select length('flash');

⑦返回字符串的字符个数
select char_length('flash');

⑧返回ASCII码值
select ord('1');

⑨睡眠指定描述
select sleep(4);
 

希望对同学们有所帮助 :)

猜你喜欢

转载自blog.csdn.net/Victor1889/article/details/131335561