
1.创建数据库
1create database ruozedata;
2.创建用户并授权
1grant all privileges on ruozedata.* to ruoze@'%' identified by '123456';
2flush privileges;
3.基本权限、语法查看
1查看数据库:
2show create database ruozedata;
3查表结构:
4show create table stuinfo;
5查看用户权限:
6show grants for ruoze@'%';
4.mysql基本字段类型
11.字段类型
2数值类型
3int 整数
4long 长整数
5
6float 单精度
7double 双精度
8decimal 小数值 钱挂钩的
9
10char 定长字符串 0-255字节
11varchar 变长字符串 0-65535字节
12text
13
14日期和时间
15date YYYY-MM-DD 2019-09-10
16time HH:MM:SS 10:10:10
17datetime 2019-09-10 10:10:10
18timestamp 2019-09-10 10:10:10
5.表
1创建表:
2create table 表名(
3字段名 类型,……
4)
5示例:
6create table stuinfo(
7id int auto_increment primary key,
8num int,
9name varchar(100),
10age int,
11createtime timestamp default current_timestamp,
12createuser varchar(100),
13updatetime timestamp default current_timestamp on update current_timestamp,
14updateuser varchar(100)
15)
16添加字段:
17alter table 表名 add 字段名 字段类型
18alter table stuinfo add name varchar(100);
6.插入数据
1insert into 表名(字段名1,字段名2,……)
2values(值1,值2,……)
3示例:
4insert into ruozedata.stuinfo(num,name,age)
5values(1,'ruoze',12,……);
7.更改数据
1update 表名 set 字段名=新值
2示例:
3update stuinfo set age=15 where id=1;
8.查询
1select * from 表名:
2示例:
3select * from stuinfo 普通查询
4select * from stuinfo 带条件查询
5where id >1;
6select * from stuinfo 多条件查询
7where id >2 or name='jepson';
9.删除数据
1delete from 表名
2示例:
3delete from stuinfo where id=3;
10.group by
1group by出现的字段 务必出现在 select 后面
2having 过滤 等价于 子表+where
3按照部门分组,查询各部门的薪水和跟部门人数
4select
5deptno ,sum(sal) as saal,
6count(ename)
7from emp
8group by deptno;
9找薪水和>9000的是哪个部门?
10select
11deptno,sum(sal) as saal
12from emp
13group by deptno
14having saal >9000;
11.order by
1通过部门,薪水排序
2select
3*
4from emp
5order by deptno,sal desc;
12.连接查询
1# left join 以左表为主 a<--b a数据最全 b是匹配 匹配多少算多少 on就是匹配条件
2select
3a.*,
4b.*
5from testa as a
6left join testb as b on a.aid=b.bid
7
8#right join 以右表为主 a-->b b数据最全 a是匹配 匹配多少算多少 on就是匹配条件
9select
10a.*,
11b.*
12from testa as a
13right join testb as b on a.aid=b.bid
14
15#inner join
16select
17a.*,
18b.*
19from testa as a
20inner join testb as b on a.aid=b.bid;

13.topN
1#哪些部门的哪些职业的薪水和,最高1位的职业是什么?
2create view sal
3as
4select
5deptno,job,
6sum(sal+ifnull(comm,0)) as sal
7from emp
8group by deptno,job;
9
10select * from sal;
11
12select
13a.*
14from sal a
15where
16(
17select count(*) from sal b
18where a.deptno=b.deptno
19and a.sal<b.sal
20) =0
21order by a.deptno;
文章转载自木木人,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




