数据类型
char(4) varchar(4)
区别:在数量大时,updata时varchar比较耗时
枚举:单选enum() 多选set()
查询
\G 格式化输出
select * from student\G
查询60-70之间的学生
select * from student where score<=70 and score>=60;
查询不及格学生的姓名和成绩
select name,score where score between 0 and 59;
查询AID1905和AID190班的学生姓名及成绩
select name,score where class in ("AID1905","AID1905");
查询北京的姓赵的学生信息(模糊查询 %-多个 -表示单个)
select * from student where city="beijing" and name link "赵%";
查询姓名为空的
select * from student where name is NULL;
正序查询
select * from student order by score DESC
limit n:显示前n条
limit m,n 从第(m+1)条开始,显示n条
每页显示10条,显示第6页的内容.方法:limit (6-1)*10 10>>limit (m-1)*n,n
selcet * from student limit 50,10
查询总结
通用顺序:
select 聚合函数 from 表名
where
group by
having
order by
limit
聚合函数
方法 | 函数 |
avg(字段名) | 该字段的平均值 |
max(字段名) | 该字段的最大值 |
min(字段名) | 该字段的最小值 |
sum(字段名) | 该字段所有纪录的和 |
count(字段名) | 该字段纪录的总数 |
找出攻击力最大的函数
select max(attack) from sanguo;
表中有多少个英雄,并且重命名
select count(name) as number from sanguo;
蜀国中攻击力大于200的英雄数量
select count (id) as number from sanguo where country="蜀" and attack>200;
group by
计算每个国家的平均攻击力
select country,avg(attack) from sanguo group by country;
所有国家的男英雄中,英雄数量最多的前2名的国家名称及英雄数量
select country,count(id) as number from sanguo
where gender="M"
group by country
order by number desc
limit 2
having语句
\\找出平均攻击力大于105的国家的前2名,显示国家和平均攻击力
select country,avg(attack) from sanguo
group by country
having avg(attack)>105
order by avg(attack) desc
limit 2;
distinct
\\不显示字段重复,distict左边可以使用聚合,右边不可以
eg1:表中都有哪些国家
select distinct country from sanguo;
eg2:计算一共有多少个国家
select count(distinct country) from sanguo;
运算符
+-*/%**
//查询时显示攻击力翻倍
select name,attack*2 from sanguo;
//更新所有蜀国英雄攻击力*2
update sanguo set attack=attack*2 where country ="蜀";
MYSQL设置
\\查看pro相关有哪些命令
show variables like "%pro%";
\\设置profiling生效
set profiling=1;
\\查看profiling
show profiles;
索引
unique:唯一索引,不允许重复,但NULL例外。 KEY标志为UNI
\\创建索引 index 普通,unqique 唯一
\\创建新表student,值:id name phone ,索引 :name->index ,phone->unique
create table student(
id int primary key auto_increment,
name varchar(20),
phone varchar(11),
index (name),
unique (phone);\\已有表添加
create [unique] index 索引名 on 表名(字段名);\\查看索引
desc 表名,key值
show index from 表名/G;删除索引
drop index 索引名 on 表名;
外键
cascade 删除和更新,同步操作
restrict 默认(从表有相关操作,主表不能操作
set null
主表删除更新,从表纪录字段变为NULL
关联查询
select ... from 表名 where 条件参数 (条件select ...)
eg:攻击力小于平均攻击力的名字和攻击力
select name,attack from sanguo where attack< (select avg(attack) from sanguo);
eg:找到每个国家攻击力最高的英雄的名字和攻击值
select name,attack from sanguo shere (country,attack) in (select country,max(attack) from sanguo group bye country;
外键
create table slave (
sut_id int,
name varchar(20),
money decimal (6,2),
FOREIGN key (stu_id) references master(id)
on delete cascade ON UPDATE cascade)charset=utf8;
JOIN
内连接:都完全匹配。
SELECT province.pname,city.cname,county.coname FROM province INNER JOIN city ON province.pid=city.cp_id INNER JOIN county ON city.cid=county.copid;
左外连接:左完全显示,右可为空。
SELECT province.pname,city.cname FROM province LEFT JOIN city ON province.pid=city.cp_id;
右外连接:与左相反。
SQL导入\导出
查看mysql可以扫描文件的位置
show variables like"secure_file_priv";
导入:
load data low_priority infile "/var/lib/mysql-files/scoretab.csv" into table scoretab fields terminated by "," lines terminated by "\n";
导出:
select id,name from scoretab into outfile "xxx/aaa.csv" fields terminated by "," lines terminated by "\n";
锁:
读锁(共享锁):
写锁(互斥、排他)
锁粒度:
表级锁:myisam
行级锁:innodb
事务:
begin;#方法一
start transaction;#方法二
执行代码
commit;#执行成功的,提交到数据库
rooback;执行失败的,进行回滚
union all
union 和 union all 的区别是,union 会自动压缩多个结果集合中的重复结果,而 union all 则将所有的结果全部显示出来,不管是不是重复。
SELECT create_time FROM `e_msku_sku` WHERE msku = '21-BQLEDNL120W-BK' UNION SELECT create_time FROM `e_msku_sku` WHERE msku = '21-BQLEDNL120W-BK'