博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL基础语法
阅读量:2047 次
发布时间:2019-04-28

本文共 2373 字,大约阅读时间需要 7 分钟。

木木人

分享自身的一些所见所得

公众号

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 小数值  钱挂钩的 910char    定长字符串 0-255字节     11varchar 变长字符串 0-65535字节   12text 1314日期和时间15date    YYYY-MM-DD 2019-09-1016time    HH:MM:SS   10:10:1017datetime 2019-09-10 10:10:1018timestamp 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的是哪个部门?10select11deptno,sum(sal) as saal12from emp13group by deptno14having saal >9000;

11.order by

1通过部门,薪水排序2select 3*4from emp5order 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.bid1415#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; 910select * from sal;1112select 13a.*14from sal a 15where16(17select count(*) from sal b 18where a.deptno=b.deptno19and a.sal

转载地址:http://lzqof.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-46》739.每日温度
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
[Kick Start 2020] Round A 2.Plates
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>