MySql语句测试实例:
## 显示数据库
show databases;
## 创建数据库
# utf-8
CREATE DATABASE database_name DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
# gbk
CREATE DATABASE database_name DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
## 打开数据库
use test;
## 用户管理
# 创建用户
create user '用户名'@'IP地址' identified by '密码';
# 删除用户
drop user '用户名'@'IP地址';
# 修改用户
rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
# 修改密码
set password for '用户名'@'IP地址' = Password('新密码')
# PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)
show grants for '用户'@'IP地址' -- 查看权限
grant 权限 on 数据库.表 to '用户'@'IP地址' -- 授权
revoke 权限 on 数据库.表 from '用户'@'IP地址' -- 取消权限
## 数据表的创建
# 显示数据表
show tables;
# 创建数据表
# 设置可以为空
# 设置默认值 not null
# 设置自增 auto_increment
# 设置主键 primary key
# 设置外键 constraint fk_cc foreign key (color_id) references color(nid)
# 设置注释 comment 'id'
create table color (
nid int not null auto_increment primary key comment 'id',
username varchar(16) not null default '张三' comment '名称'
);
create table fruit (
fid int not null primary key,
smt char(32) null,
color_id int not null,
constraint fk_cc foreign key (color_id) references color(nid)
);
## 删除表
drop table fruit;
## 清空表
delete from color;
truncate table color;
## 修改表
# 添加列:
alter table color add create_time date;
# 删除列:
alter table color drop column create_time;
# 修改列:
alter table color modify column username varchar(3);
alter table color change username usernames varchar(6);
# 添加主键:
alter table color add primary key(nid);
# 删除主键:
alter table color drop primary key;
alter table color modify nid int, drop primary key;
# 添加外键:
alter table fruit add constraint fk_cc_fr foreign key fruit(color_id) references color(nid);
# 删除外键:
alter table color drop foreign key fk_cc_fr;
# 修改默认值:
alter table color alter username set default '李四';
# 删除默认值:
alter table color alter username drop default;
## 数据表的相关操作
# 新增
insert into color(nid, username) values(default, '王五');
insert into color(nid, username) values(default, '王五'),(default, '李四');
insert into color(username) select username from color;
# 删除
delete from color;
delete from color where nid = 2;
# 修改
update color set username = '小明' WHERE nid = 3;
# 查询
# 普通查询
select * from color;
select * from color where nid > 3;
select nid, username as name from color where nid > 2;
# 排序查询
# 根据 ‘nid’ 从大到小排列
select * from color order by nid desc;
# 根据 ‘nid’ 从小到大排列
select * from color order by nid asc;
# 先根据 ‘nid’ 排序, 如果相同, 再根据 ‘usernam’ 排序
select * from color order by nid desc, username asc;
# 模糊查询
# ‘%’ 表示匹配多个字符
select * from color where username like '王%';
# '_' 表示匹配一个字符
select * from color where username like '王_';
# 分组查询
select username from color group by username;
select nid, username from color group by nid, username;
select nid, username from color where nid > 2 group by username order by nid desc;
select nid, username, count(*), sum(nid), max(nid) from color group by username;
select nid, username from color group by username having max(nid) > 5;
# 多表查询
# 表连接
# 无对应关系不显示
select C.nid, C.username, F.smt from color C, fruit F where C.nid = F.color_id;
# 无对应关系不显示
select C.username, F.smt from color C inner join fruit F on C.nid = F.color_id;
# C表所有显示,如果F表中无对应关系, 则为null
select C.username, F.smt from color C left join fruit F on C.nid = F.color_id;
# F表所有显示,如果F表中无对应关系,则值为null
select C.username, F.smt from color C right join fruit F on C.nid = F.color_id;
# 组合
# 组合,自动处理重合
select username from color union select smt from fruit;
# 组合,不处理重合
select username from color union all select smt from fruit;
声明:此博文中的SQL语句本人亲测无误,如有有错误,请私信告知!