标签:esc 开启 显示 用户信息 地址 tar 字符 启动服务 冲突
insert into 表名 [(字段名)] values(值列表1),(值列表2),...
insert into 表名 [(字段列表)] values(值列表) on duplicate key update 字段 = 新值;
replace into [(字段列表)] values(列表值)
insert into 表名1 [(字段列表)] select *from /(字段列表) from 表名2
update 表名 set 字段名 = 新值 where 条件 limit 限制数量
truncate 表名
select select选项 字段列表 from 数据源 where 条件 group by 分组 having 条件 order by 排序 limite 限制数量
select distinct *from 表名;
select distinct 字段名1 as 别名1 ,字段名2 别名2 ... from 表名;
- 数据源只要是一个符合二维表的结构即可
- 单表数据 : from 表名
- 多表数据: from 表名1 ,表名2... (字段数拼接 ,数据数相乘---笛卡尔乘积)
form 后面跟的不是实体表,而是一个从表中查询的一个数据表
from (select 字段列表 from 表名)as 表别名
- 条件筛选(从数据表中获取数据的时候)
- 原理 :在磁盘一条条获取数据时,每获取一条数据,就对其筛选(不符合的数据就不放到内存中)
- 根据指定的字段进行分组,方便统计
- 分组后,只会保留各组的第一个数据。
- 统计函数
- count () : 统计每组中的数量
- avg() : 求平均值
- sum() : 求和
- max() :...
- min() : ...
- group_concat() : 拼接
> group by 字段名
group by 字段1,字段2 ; //先按照字段1进行分组,然后将结果按字段2进行分组
insert into text2 values('xiaoming','cq',1,'female',60),('xiaoli','bj',2,'male',60),('kongming','cq',2,'female',45),('xiaoqing','sh',1,'female',75),('jiangming','gz',2,'male',30),('wangming','gz',1,'male',90);
select class , gender ,group_concat(name),avg(score),min(score),max(score) ,count(*) from text2 group by class,gender;
--结果:
+-------+--------+--------------------+------------+------------+------------+----------+
| class | gender | group_concat(name) | avg(score) | min(score) | max(score) | count(*) |
+-------+--------+--------------------+------------+------------+------------+----------+
| 1 | female | xiaoming,xiaoqing | 67.5000 | 60 | 75 | 2 |
| 1 | male | wangming | 90.0000 | 90 | 90 | 1 |
| 2 | female | kongming | 45.0000 | 45 | 45 | 1 |
| 2 | male | xiaoli,jiangming | 45.0000 | 30 | 60 | 2 |
+-------+--------+--------------------+------------+------------+------------+----------+
每一次分组向上统计的过程都会产生一次新的统计数据,且该数据的字段为null
group by 字段 [asc|desc] whith rollup
select class , gender ,min(score),max(score) from text2 group by class,gender with rollup;
-- 结果
+-------+--------+------------+------------+
| class | gender | min(score) | max(score) |
+-------+--------+------------+------------+
| 1 | female | 60 | 75 |
| 1 | male | 90 | 90 |
| 1 | NULL | 60 | 90 |
| 2 | female | 45 | 45 |
| 2 | male | 30 | 60 |
| 2 | NULL | 30 | 60 |
| NULL | NULL | 30 | 90 |
+-------+--------+------------+------------+
insert into text2 values('songtiti','lz',2,'female',98),('wanwu','km',3,'male',89);
select group_concat(name) ,class ,count(*)from text2 group by class having count(*)>2;
-- 结果
+------------------------------------+-------+----------+
| group_concat(name) | class | count(*) |
+------------------------------------+-------+----------+
| xiaoming,xiaoqing,wangming | 1 | 3 |
| xiaoli,kongming,jiangming,songtiti | 2 | 4 |
+------------------------------------+-------+----------+
单字段排序
order by 字段 [asc|desc]; //asc 升序(默认), desc 降序。
order by 字段1 [asc|desc] ,字段2 [asc|desc] ; // 先按字段1排序 , 然后对字...
// select * from text2 having class = 2 order by score asc;
select * from text2 order by class , score asc;
// 结果:
+-----------+------+-------+--------+-------+
| name | city | class | gender | score |
+-----------+------+-------+--------+-------+
| xiaoming | cq | 1 | female | 60 |
| xiaoqing | sh | 1 | female | 75 |
| wangming | gz | 1 | male | 90 |
| jiangming | gz | 2 | male | 30 |
| kongming | cq | 2 | female | 45 |
| xiaoli | bj | 2 | male | 60 |
| songtiti | lz | 2 | female | 98 |
| wanwu | km | 3 | male | 89 |
+-----------+------+-------+--------+-------+
limit 数值
limit offset ,length ; //offset 偏移量(获取数据的初始位置) ,length 数据条数
select * from text2 order by class , score asc limit 2,5;
-- 结果:
+-----------+------+-------+--------+-------+
| name | city | class | gender | score |
+-----------+------+-------+--------+-------+
| wangming | gz | 1 | male | 90 |
| jiangming | gz | 2 | male | 30 |
| kongming | cq | 2 | female | 45 |
| xiaoli | bj | 2 | male | 60 |
| songtiti | lz | 2 | female | 98 |
+-----------+------+-------+--------+-------+
+ - * \ | 算术运算符 | |
< > = >= <= <> = | 比较运算符 | |
and or not (非) | 逻辑运算符 | |
in() | In运算符 | in(结果1,结果2,结果3..,) |
is | is运算符 | is null /is not null |
like | like运算符 | |
select name,class,score,class*score from text2;
-- 结果:
+-----------+-------+-------+-------------+
| name | class | score | class*score |
+-----------+-------+-------+-------------+
| xiaoming | 1 | 60 | 60 |
| xiaoli | 2 | 60 | 120 |
| kongming | 2 | 45 | 90 |
| xiaoqing | 1 | 75 | 75 |
| jiangming | 2 | 30 | 60 |
| wangming | 1 | 90 | 90 |
| songtiti | 2 | 98 | 196 |
| wanwu | 3 | 89 | 267 |
+-----------+-------+-------+-------------+
闭区间 [x , y]
select name,class,score,class*score from text2 having score between 60 and 90;
select name,class,score,class*score from text2 having score >=60 and score <=90;
select * from text2 having name in('xiaoli','wanwu');
-- 结果:
+--------+------+-------+--------+-------+
| name | city | class | gender | score |
+--------+------+-------+--------+-------+
| xiaoli | bj | 2 | male | 60 |
| wanwu | km | 3 | male | 89 |
+--------+------+-------+--------+-------+
用于判断字段是否为null
is null /is not null
用于模糊匹配
% :多个字符
like ‘匹配模式‘
字段数要要一样
将分表数据联合在一起显示
select 语句
union [union选项]
select 语句;
select *from text2
union all
select * from text2 ;
(select * from text2 where gender='male' order by score limit 50 )
union
(select * from text2 where gender='female' order by score limit 50 );
分类
表1 cross join 表2
select * from text2 cross join www;
表1 [inner] join 表2 on 匹配条件;
select * from www inner join text2 on name = name_id;
-- 或:
select * from www inner join text2 on text2.name = www.name_id;
-- 或
select * from www as W inner join text2 as T on T.name =W.name_id; //别名
-- 或
select * from www as W inner join text2 as T where T.name =W.name_id; //where=>on
//用where(having)代替on : 先笛卡尔积后 where 筛选数据
//(效果)等价于 :select * from www ,text2 having text2.name = www.name_id;
--结果
+------+----------+----------+------+-------+--------+-------+
| age | name_id | name | city | class | gender | score |
+------+----------+----------+------+-------+--------+-------+
| 12 | xiaoming | xiaoming | cq | 1 | female | 60 |
+------+----------+----------+------+-------+--------+-------+
主表 left join 从表 on 连接条件
select * from text2 left join www on name = name_id;
-- 结果
+-----------+---------+-------+--------+-------+------+----------+
| name | city | class | gender | score | age | name_id |
+-----------+---------+-------+--------+-------+------+----------+
| xiaoming | cq | 1 | female | 60 | 12 | xiaoming |
| xiaoli | bj | 2 | male | 60 | NULL | NULL |
| kongming | cq | 2 | female | 45 | NULL | NULL |
| xiaoqing | sh | 1 | female | 75 | NULL | NULL |
| jiangming | gz | 2 | male | 30 | NULL | NULL |
| wangming | gz | 1 | male | 90 | NULL | NULL |
| songtiti | lz | 2 | female | 98 | NULL | NULL |
| wanwu | km | 3 | male | 89 | NULL | NULL |
| qingtian | beijing | 1 | male | 0 | NULL | NULL |
| xiayu | beijing | NULL | male | 0 | NULL | NULL |
+-----------+---------+-------+--------+-------+------+----------+
主表 right join 从表 on 连接条件
select * from text2 right join www on name = name_id;
-- 结果
+----------+------+-------+--------+-------+------+----------+
| name | city | class | gender | score | age | name_id |
+----------+------+-------+--------+-------+------+----------+
| xiaoming | cq | 1 | female | 60 | 12 | xiaoming |
| NULL | NULL | NULL | NULL | NULL | 15 | zouming |
+----------+------+-------+--------+-------+------+----------+
连接查询中的一个关键字,用于替代对应的on,进行条件匹配
使用using后,只会保留连接同名字段中的一个。
表1 [inner/left/right] join 表2 using(同名连接字段)
2.主查询概念
按位置父类 | |
---|---|
where子查询 | 子查询位置在where条件中 |
from子查询 | 子查询的位置在from数据源中 |
select * form 数据源1 where 条件 逻辑符号 (select 字段名 form 数据源2 where 条件 )
主查询 where 条件 in (列子查询)
select * from www where www.name in(select name from text2);
主查询 where 条件 [(一个行元素)] = (行子查询)
select * from text2 where (score , height)=(select max(score),min(height) from text2);
-- 结果:(身高最矮且分数最高)
+----------+------+-------+--------+-------+--------+
| name | city | class | gender | score | height |
+----------+------+-------+--------+-------+--------+
| songtiti | lz | 2 | female | 98 | 0 |
+----------+------+-------+--------+-------+--------+
select * from text2 where (score , height)=(select max(score),max(height) from text2);
--结果:(身高最高且分数最高)
Empty set (0.00 sec)
select 字段列表 from (表子查询)[where][group by ][having][order by][limit][...]
select *from (select * from text2 order by score desc) as ww where ww.class is not null group by class;
-- 结果(获取每个班级中分数最高的学生信息)
+----------+------+-------+--------+-------+--------+
| name | city | class | gender | score | height |
+----------+------+-------+--------+-------+--------+
| wangming | gz | 1 | male | 90 | 0 |
| songtiti | lz | 2 | female | 98 | 0 |
| wanwu | km | 3 | male | 89 | 165 |
+----------+------+-------+--------+-------+--------+
where exitst(查询语句)
select * from www as W where exists(select name from text2 as T where T.name = W.name);
-- 结果(表text2与表www中名字相同的学生信息)
+------+----------+
| age | name |
+------+----------+
| 12 | xiaoming |
+------+----------+
In | 主查询 where 条件 in(列子查询) | |
Any | =any(列子查询) | 任意一个匹配即可 |
Any | <>any(列子查询) | 不匹配其中的任意一个 |
some | 与any一样 | |
All | all(列子查询) | 匹配其中的所有 |
All | <>all(列子查询) | 不... |
-- any:
select * from www where name in(select name from text2);
-- 等价于
select * from www where name =any(select name from text2);
-- 等价于
select * from www as W where exists(select name from text2 as T where T.name = W.name);
+------+----------+
| age | name |
+------+----------+
| 12 | xiaoming |
+------+----------+
-- all:
select * from www where name <>all(select name from text2);
-- 结果(www表中学生姓名 与 表 text2中的姓名都不相同的学生信息 )
+------+---------+
| age | name |
+------+---------+
| 15 | zouming |
+------+---------+
mysql 提供了 一个用于备份的客户端 mysqlldump.exe
数据备份的结果都是SQL指令
语法: mysqldump -hPup 数据库名字 [[表1 [表2...] ] ] > 备份文件地址
mysqldimp -hlocalhost -p3306 - uroot -proot 库名 > url
mysqldump -hlocalhost -p3306 - uroot -proot kkk > F:\桌面\sqlbeifeng\TextDocument.sql
单表备份
多表备份
mysqldump -hlocalhost -p3306 - uroot -proot 库名 表1 表2 > url
mysql -hPup 数据库 < url
source SQL文件url //必须先选库
使用root 用户在mysql.user表中插入新用户数据
通过专门的SQL语句
create user 'root'@'' identified by '147258';
create user user2; //不限定客户端ip ,没有密码
?
drop user 用户名 @host
drop user root@'';
set password for 用户 = password(‘新的明文密码‘);
update mysql.user set password = password(‘新的明文密码’) where 条件
set password for user1 = password('159263');
grant 权限列表 on 数据库.表名 to 用户 // (*.*代表整个所有数据库 、 )
- 权限列表
- 权限之间用逗号 分隔
- all privileges 代表全部权限- 数据库.表名
- 数据库1.表1 分配数据库1中的表1的权限
- 数据库1.* 分配数据库1的权限(数据库1中的所有表)
- *.* 所有的数据库
grant select on kkk.www to user1; //查
grant update on kkk.www to user1; // 增
revoke 权限列表 /all privileges on 数据库.表 from 用户
revoke all privileges on *.* from user1;
flush privileges;
通过mysqld启动服务:mysqld --skip-grant -tables; //跳过服务器权限启动服务器
启动MYSQL服务
外键关系 :一张表(从表)中的有一个字段,保存着指向另一张表(主表)的主键
外键创建时会产生一个索引,但删除的时候只会删除索引自身不会删除该索引。
[constraint
外键名
] foreign key(外键字段) references 主表(主键)
create table tableA (
id int primary key auto_increment,
name varchar(11) not null,
class int ,
foreign key(id) references text2(id)
);
alter table 从表 add [constraint
外键名
] foreign key(外键字段) reference 主表(主键)
alter table tableA add foreign key(class) references text2(id);
-- show create table tablea;
+-----------------------------------------------------------+
| tablea | CREATE TABLE `tablea` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(11) NOT NULL,
`class` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `class` (`class`), //自动添加的索引
CONSTRAINT `tablea_ibfk_1` FOREIGN KEY (`class`) REFERENCES `text2` (`id`)
// `tablea_ibfk_1` : 外键名
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
alter table 从表 drop foreign key 外键名字
alter table tableA drop foreign key `tablea_ibfk_1`;
alter table 表名 drop index 索引名字; //删除索引
不能插入主表不存在的数据
...
add foreign key(外键字段) reference 主表(主键) 约束模式
当客户端发送一条SQL语句(增删改)给服务器时,服务器执行完后。不用的等待用户的同步命令,会自动同步数据。
查看自动事务是否开启
show variables like ‘autocommit%‘;
show variables like 'autocomm%';
-- 结果
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
rollback //回滚 (清空之前的操作)
set autocommit = off;
start transaction | 开启事务 | |
commit | 事务提交(数据同步) | |
rollback | 回滚(撤销) | |
savepoint | 回滚点 | 提前设置好回滚点,当后面操着有误时可以回滚到该点出的操作,不必rollback ---- 撤销全部操作 |
end transaction |
show variables [like ‘pattern‘]
select @@变量名 ;
修改系统变量的值
set 变量名 = 新值
set global 变量名 = 值 ;
或:
select @@global.变量名 = 值;
会话变量即用户变量
会话变量对当前用户使用的客户端有效
set @变量名 := 值 (:= 为专用赋值符号)
重新赋值
select @变量名 := 值 from 数据源
查看变量
select @变量名;
? declare 变量名 数据类型 [default 默认值...]
select 字段列表 ,if(条件,为真结果,为假结果) as 别名 from 表名
select * ,if(score>60, 'jige','bujige') as score_judge from kkk.text2;
-- 结果
/*
+-----------+---------+-------+--------+-------+--------+----+-------------+
| name | city | class | gender | score | height | id | score_judge |
+-----------+---------+-------+--------+-------+--------+----+-------------+
| xiaoming | cq | 1 | female | 60 | 175 | 1 | bujige |
| xiaoli | bj | 2 | male | 60 | 177 | 2 | bujige |
| kongming | cq | 2 | female | 45 | 0 | 3 | bujige |
| xiaoqing | sh | 1 | female | 75 | 182 | 4 | jige |
| jiangming | gz | 2 | male | 30 | 147 | 5 | bujige |
| wangming | gz | 1 | male | 90 | 0 | 6 | jige |
| songtiti | lz | 2 | female | 98 | 0 | 7 | jige |
| wanwu | km | 3 | male | 89 | 165 | 8 | jige |
| qingtian | beijing | 1 | male | 0 | 0 | 9 | bujige |
| xiayu | beijing | NULL | male | 0 | 161 | 10 | bujige |
+-----------+---------+-------+--------+-------+--------+----+-------------+
*/
标签:esc 开启 显示 用户信息 地址 tar 字符 启动服务 冲突
原文地址:https://www.cnblogs.com/zoukun/p/12228209.html