MySql数据库常用基础命令-1

-- mysql workbench 常用快捷键
-- 新建tab(new tab) ctrl+t
-- 执行当前语句(execute current statement) ctrl+enter
-- 执行全部或选中的语句(execute all or selection) ctrl+shift+enter
-- 查看执行计划(explain current statement) ctrl+alt+x
-- 注释 - -加空格,如 – select * from t;


-- 创建数据库
create database students;
-- 使用数据库
use students;
-- 展示数据库中所有的表
show tables;
-- 创建水果表
-- id为int类型自增长的非空主键
-- name为最大20字符的非空可变长度的字符串类型
-- money为非空int类型
-- isshou为非空bit类型默认为1,此处用作逻辑删除
-- time为非空时间类型
create table fruit(
id int auto_increment primary key not null,
name varchar(20) not null,
money int not null,
isshou bit default 1 not null,
time datetime not null
);
-- 删除水果表
drop table fruit;
-- 展示水果表的结构
desc fruit;
-- 查询水果表全部数据
select *from fruit;
-- 修改表类型money非空int类型改为可空int类型
alter table fruit change money money int;
-- 展示水果表的创建代码
show create table fruit;

-- 获取当前操作数据库名称
select database();
-- 全列插入
insert into fruit value(0,'苹果',10,0,'2019-08-19');
insert into fruit value(5,'百香果',null,0,'2019-08-19');
-- 缺省插入
insert into fruit(name,money,isshou,time) value('哈密瓜',18,1,'2019-08-18');
-- 同时插入多条数据
insert into fruit(name,money,isshou,time) value('西瓜',1,0,'2019-08-19 12:45:06'),('香瓜',5,1,'2019-08-18');
-- 修改数据权限
SET SQL_SAFE_UPDATES = 0;
-- 修改数据,将水果表中逻辑未删除的数据全部改为逻辑删除
update fruit set isshou=1 where isshou=0;
-- 修改数据,将水果表中id为3的数据的时间改为指定时间
update fruit set time='2019-08-19 00:00:00' where id=3;
-- 删除水果表中id为4的数据
delete from fruit where id = 4;
-- 创建数据库
create database teacher charset=utf8;
-- 删除数据库
drop database teacher;
-- 逻辑删除
-- isDelete,修改
select *from htgs limit 200000;
select id,cgdw from htgs limit 200000;
-- 消除重复行(distinct,原集数据未改变)
select distinct isshou from fruit;
-- 查询水果表全部数据
select *from fruit;
-- 查询水果表id大于1的数据
select *from fruit where id>1;
-- 查询水果表名称不是哈密瓜的数据
select *from fruit where name!='哈密瓜';
-- 查询水果表isshou逻辑未删除的数据
select *from fruit where isshou=0;
-- 查询水果表id大于2并且逻辑未删除的数据
select *from fruit where id>2 and isshou=0;
-- 查询水果表价格大于5或者逻辑未删除的数据
select *from fruit where money>5 or isshou=0;
-- 查询水果表id不大于2的数据
select *from fruit where not id>2;
-- 查询水果表名称第一个字符为‘哈’的所有数据
select *from fruit where name like '哈%';
-- 查询水果表名称总共3个字符中间字符为‘密’的所有数据
select *from fruit where name like '_密_';
-- 查询水果表名称第一个字符为‘哈’的所有数据或者名称包含‘瓜’的全部数据
select *from fruit where name like '哈%' or name like '%瓜%';
-- 查询水果表id为1,2,4的数据,没有则不返回结果
select *from fruit where id in(1,2,4);
-- 查询水果表id在2~4范围的全部数据
select *from fruit where id between 2 and 4;
-- 查询水果表id在2~4范围的并且逻辑未删除的全部数据
select *from fruit where id between 2 and 4 and isshou=0;
-- 查询水果表价格不为null并且逻辑未删除的数据
select *from fruit where money is not null and isshou=0;
-- 聚合函数,查询水果表逻辑未删除的条数
select count(*) from fruit where isshou = 0;
-- 聚合函数,查询水果表逻辑未删除的数据的最高价格
select max(money) from fruit where isshou = 0;
-- 复合查询+聚合函数,查询水果表id为逻辑未删除的水果表全部数据中最小的id
select *from fruit where id= (select min(id) from fruit where isshou = 0);
-- 聚合函数,查询水果表逻辑未删除的全部数据的价格总和
select sum(money) from fruit where isshou = 0;
-- 聚合函数,查询水果表逻辑删除的数据的id的平均值
select avg(id) from fruit where isshou = 1;
-- 聚合函数,查询水果表的isshou和不同isshou的数量按照isshou进行分组,之后对结果集进行isshou数量大于2的筛选
select isshou,count(*) as rs from fruit group by isshou having rs>2;
-- 查询水果表逻辑未删除的数据按照升序排列
select *from fruit where isshou=0 order by money asc;
-- 分页,查询水果表第二条数据开始的2条数据(0开始数)
select *from fruit limit 1,2;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值