my_mysql
###一键偷懒YUM安装MySQbL###
1.安装mysql数据库
#yum install -y mariadb-server mariadb
2.登录mysql数据库常用选项
-h:指定服务端主机地址
-u: 指定登入的用户名
-p:指明密码
-D:指明登入的数据库
-e:连接之后直接运行SQL语句,不进入交互式模式(可以在脚本中使用)
3.mysql语言分为3大类
DDL:数据库定义语言 create , alter , drop
DML:数据库操作语言 insert , delete , update , select
DCL:数据库控制语言 grant , revoke
4.数据库DDL语言
创建库 create database zxxsql;
删除库 drop database zxxsql;
修改库 alter database zxxsql character set = utf8
创建表 create table home (id int not null primary key auto_increment, name varchar(250) not null, class varchar(250) not null);
查看表结构 desc home;
修改表
添加字段:alter table home add gender enum('f','m');
删除字段:alter table home drop gender;
修改字段:
alter table home change name username varchar(100) after id;
alter table home modify username varchar(100) first;
删除表:drop table home;
5.数据库DML语言
<1>insert 在home表中插入3组class和username数据。
insert into home (class,username) values ('ops', '运维开发'), ('opsdev', '运维开发'), ('开发', 'java开发');
<2>update 修改home表数据
update home set class = '开发部门' where id = 1;
<3>delete 删除表数据
delete from home where class = '开发';
<4>select
查询表上的所有的数据 select * from home;
查询部分数据 select id,class from home;
# 还可以取个别名 select id as num,class from home;
使用where子句过滤
# 可以使用的算数运算符:>, < , >=, <=, ==, !=
# 可以使用连接词:and , or
select * from home where id >= 2;
select * from home where id <= 2 and id >1;
select * from home where id between 1 and 2;
# 可以使用like做模糊匹配(%:表示任意长度的字符,_:表示任意单个字符)
select * from home where class like 'ops%';
# 可以使用null对值进行判断
select * from home where id is not null;
select * from home where id is null;
使用order指定排序(默认是asc,升序排列)
select * from home order by id desc;
6.DCL数据库语言
<1>grant
#先创建用户,再授权
create user zxx@'172.16.19.%' identified by '123456';
grant all on *.* to zxx@'172.16.19.%';
flush privileges;
#创建用户的同时给用户授权
grant all on *.* to zxx@'172.16.19.%' identified by '123456';
flush privileges;
#给用户授予某些权限
show grants for zxx@'172.16.19.%';
<2>查看用户权限
show grants for zxx@'172.16.19.%';
<3>删除用户
delete from mysql.user where user = "zxx";
flush privileges;
7.select 查询语句详解
-1:where子句:指明过滤条件
可以使用的算数运算符:+, -, * ,/ ,= ,!= ,<=, >=
between 较小的数 and 较大的数
in (较小的数,较大的数)
is null 或 is not null
like模糊匹配
例如:
select * from home where id >= 2;
select * from home where id <= 2 and id >1;
select * from home where id between 1 and 2;
select * from home where id in (1,2); # 从1和2中取值
select * from home where class like 'ops%';
select * from home where id is not null;
select * from home where id is null;
-2:group by 子句:根据指定的查询条件将查询结构进行分组,用于做聚合运算
使用聚合函数:avg( ) , max( ) , min( ) , count( ), sum( )
select age,gender from students group by gender;
select avg(age),gender from students group by gender;
select min(age), gender from students group by gender;
select max(age), gender from students group by gender;
select count(id), gender from students group by gender;
-3:having子句:将分组之后的结果再次过滤
select avg(age) as 'average_age', gender from students group by gender having average_age > 50;
-4:order by子句 :根据指定的字段对查询结果进行排序,默认为升序,降序使用关键字desc
select name,age from students order by age desc;
-5:limit 子句:对查询的结果进行输出行数的限制
select name,age from students order by age desc limit 8; # 选前8行
select name,age from students order by age limit 4, 2; # 前4个不选,从第五行开始选2行
###总结单表查询次序##########################################################################################################################
子句的书写顺序:
where -> group by -> having -> order by -> limit
例如:
select *,avg(score) as '各班平均成绩' from students where id > 1 group by class having avg(score) > 55 order by score desc limit 3 ; #
#############################################################################################################
my_mysql的更多相关文章
- mysqli_set_charset和SET NAMES优劣分析
bool mysqli_set_charset ( mysqli $link , string $charset ) 这应该是首选的用于改变字符编码的方法,不建议使用 mysqli_query()执行 ...
- linux一些工具的安装(三)
linux(vmware15 centos7)中Docker安装 一.Docker卸载 1.查看已安装的docker安装包 $yum list installed|grep docker 执行后的 ...
- Docker: 创建带数据的MySql container
如果需要想要在一个装有docker的机器上启动一个MySql的container,并且整个MySql container有我想要的数据: 1. 先在已有的MySql instance上准备好数据 2. ...
- Docker容器学习梳理 - 应用程序容器环境部署
关于国内Docker镜像,可以参考:Docker容器学习梳理--基础知识(2) 的Docker镜像使用. 如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等 ...
- cenos7.0 安装docker
使用yum命令在线安装 yum install docker 安装后查看Docker版本 docker -v启动docker:systemctl start docker停止docker:syste ...
- PHP API中,MYSQL与MYSQLI的持久连接区别
转载自:http://www.cnxct.com/some-differences-between-mysql-and-mysqli-of-persistent-connection/ 很久很久以前, ...
- Python爬虫(八)
源码: import requests import re from my_mysql import MysqlConnect import time,random # 获取招聘详情链接 def ge ...
- Python爬虫(七)
源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...
- Python爬虫(六)
源码: import requests import re from my_mysql import MysqlConnect # 获取问答信息 def get_contents(page,heade ...
随机推荐
- Laravel 5.6 安装 guzzlehttp
环境:Laravel 5.6 安装 composer require guzzlehttp/guzzle 在vendor文件夹下,vendor\guzzlehttp\guzzle 引入 use Gu ...
- 【CSS3】使用CSS3制作全屏切换效果
在线演示: DEMO DEMO中及以下代码并没有写兼容代码,请使用高级浏览器打开,IE版本对CSS3支持并不太友好,IE11打开没有滚屏效果. 兼容代码前缀: -webkit- -moz- -o- - ...
- uva 562 Dividing coins(01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
- dos taskkill 命令
C:\Users\asn\Desktop>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { [/FI filter ...
- python进阶之异常处理
异常处理 在代码运行时,会因为各种原因出现bug,而程序遇到bug就会中断运行,而在日常生产中程序是要长时间运行不能随意中断的.因此就需要我们提前做好异常处理. 异常 print(x) # 一般报错就 ...
- P1095 水仙花数
题目描述 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:"水仙花数"是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3 ...
- 2018-2-13-wpf-使用-Dispatcher.Invoke-冻结窗口
title author date CreateTime categories wpf 使用 Dispatcher.Invoke 冻结窗口 lindexi 2018-2-13 17:23:3 +080 ...
- Java中getBytes()方法--使用详解
getBytes()方法详解 在Java中,String的getBytes()方法是得到一个操作系统默认的编码格式的字节数组.这表示在不同的操作系统下,返回的东西不一样! 1. str.getByte ...
- 2018-2-13-win10-uwp-隐藏实时可视化
title author date CreateTime categories win10 uwp 隐藏实时可视化 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 ...
- H3C IPv6邻居发现协议