show create table 表名 \G;(查看创建的属性)
alter table 表名 auto_increment=xx;(修改自增起始值)
set session auto_increment_offset=xx;(修改步长)
索引的目的:加速查找
约束:
主键
外键
唯一索引:unique 名字 (列名) ----不允许重复(可以为空)
联合唯一:unique 名字 (列名1,列名2)---不允许一起重复 sql语句补充
select * from 表名;
select 列名 as 别名 from 表名 where 条件;
select 列名 as 别名,数字 from 表名 where 条件;(加额外的数字列) select * from 表名 where 列名 != x;(不等于)
select * from 表名 where 列名 in (x,y,z);(in(在):x,y,z)
select * from 表名 where 列名 not in (x,y,z);(不在)
select * from 表1名 where 列名 in (select 列名 from 表2);(和表2中相同的)
select * from 表名 where 列名 between x and y; (介于x-y,包含边界) select * from 表名 where 列名 like "a%";(%代表所有的值,数量:0-无限多)
select * from 表名 where 列名 like "a_";(_代表所有的值,数据1个) select * from 表名 limit x;(前x条) select * from 表名 limit x,y;(起始位置从x起,往后取y条) select * from 表名 order by 列名 desc; #大到小
select * from 表名 order by 列名 asc; #小到大
select * from 表名 order by 列名1 desc,列名2 desc;分开排序
取后y条数据(先排序在取值)
select * from 表名 order by 列名 desc limit y;
分组:
select count(列名),max(列名),part_id from 表名 group by 列名;(sum:求和,avg:求平均值)
连表:
select * from 表1,表2 where 条件;

  select * from 表1 left join 表2 on 列1=列2;

消除重复的行

  select distinct 列名 from 表名;

模糊查询

 %匹配任意多个字符,_匹配一个字符 

select * from students where sname like '郭%';

 

范围查询

in 表示在一个非连续的范围内查询,

select * from 表名 where id in(1,3);

between ... and ...

select * from students where id between 2 and 4;

判断空 

 select * from students where isnull is null;

 select * from students where isnull is not  null;

优先级

小括号,not,比较运算符,逻辑运算符;

and比or先运算,如果希望先and要结合小括号

聚合(函数)

count(*)用于统计总行数

select count(*) from students;

max(列)找出最大值,min(列)找出最小值

select max(year) from students;
select min(year) from students;

sum(列)求和

 select sum(year) from students;

avg(列)求平均值

 select avg(year) from students;  

  

子查询,得到聚合处理后的结果

 select * from students where year=(select min(year) from students);

分组

group by相同的值分为一组

select year as 年龄,count(*) from students group by year;

  

分组之后进行筛选

where对from后的结果进行筛选,having是对分组后的结果进行筛选

select year as 年龄,count(*) from students group by year having  count(*)=3 or 年龄=22;

分页

select * from students order by id desc limit 2,5;

  

完整的查询语句

关系

   decimal(a,b)

  参数说明:

          a:指定小数点左边和右边可以存储的十进制数字的最大个数,最大精度为38.

        b:指定小数点右边可以存储的十进制数字的最大个数。小数位数必须是从0~a之间的值,默认小数位数是0.

添加外键

 alter table scores add foreign key(stuid) references students(id); 

如果直接写在创建表语句里面

foreign key(subid) references subject(id);

 

连接查询

select students.sname,subject.title,scores.score from scores
inner join students on scores.stuid=students.id
inner join subject on scores.subid = subject.id;

  

视图

复杂的查询语句,多次使用后维护非常麻烦,解决办法就是定义视图,

视图的本质查询语句进行封装,用它代表复杂的select语句,相当于快捷方式,别名

创建视图

create view stuscore as  select students.sname,subject.title,scores.score from scores inner join students on scores.stuid=students.id inner join subject on scores.subid = subject.id;

  

使用视图

select * from stuscore;

  

事务

当你通过sql语句对数据进行影响变更的时候,

  如果某个sql语句出错,你希望整个操作都进行回退

目的:保证一个业务逻辑的操作有效

也就是你操作数据只存在2种情况,成功或失败,成功才会改变数据

ACID

原子性:不可拆分

一致性:不会因为顺序影响结果,结果一致

隔离性:不会因为别的数据干扰,

持久性:不会因为特殊的情况造成数据丢失

引擎必须是innodb和bdb

步骤:

  1.开始begin

  begin;

  2.提交commit

  sql语句

  commit;

  (到这才会更改)

  3.回滚rollback

  rollback;

  放弃整个begin之后的操作

索引

数据默认是按照主键存的

等尽量往前写,范围尽量往后写

优化:优化where后面的语句建立索引,可以有效提高查询速度

查看索引

show index from students;

  

创建索引

create index ztitleindex on maoyantop100(ztitle(20));

删除索引

drop index ztitleindex on maoyantop100;

  

  

性能分析

1.开启运行时间检测

 set profiling=1;

  

2.执行查询语

 

select * from maoyantop100 where ztitle='触不可及';

  

3.查看执行时间

show profiles;

 

4.建立索引

create index ztitleindex on maoyantop100(ztitle(20));

  

5.重新执行sql语句

select * from maoyantop100 where ztitle='触不可及';

  

6.查看执行时间

 show profiles;

  

差距明显

 

<数据库>MySQL补充( 查询)的更多相关文章

  1. python进阶10 MySQL补充 编码、别名、视图、数据库修改

    python进阶10 MySQL补充    编码.别名.视图.数据库修改 一.编码问题 #MySQL级别编码 #修改位置: /etc/mysql/mysql.conf.d/mysqld.cnf def ...

  2. PHP操作mysql数据库:[2]查询数据听语音

    本文主要详细讲解如何使用php语言,对mysql数据库进行查询.添加.删除.更新等操作. 工具/原料   Macromedia Dreamweaver 8 mysql数据库,php语言 一.前言   ...

  3. MySQL、SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法. 可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应 ...

  4. Vc数据库编程基础MySql数据库的表查询功能

    Vc数据库编程基础MySql数据库的表查询功能 一丶简介 不管是任何数据库.都会有查询功能.而且是很重要的功能.上一讲知识简单的讲解了表的查询所有. 那么这次我们需要掌握的则是. 1.使用select ...

  5. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

  6. mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

    mysql中查询一个字段具体是属于哪一个数据库的那一张表:用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个 ...

  7. mysql数据库----索引补充

    1.索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据.对于索引,会保存在额外的文件中. 2.索引种类 普通索引:仅加速查询 唯一索引:加速查询 + 列值唯一(可以有 ...

  8. mysql数据库优化课程---16、mysql慢查询和优化表空间

    mysql数据库优化课程---16.mysql慢查询和优化表空间 一.总结 一句话总结: a.慢查询的话找到存储慢查询的那个日志文件 b.优化表空间的话可以用optimize table sales; ...

  9. MySql数据库之连接查询

    在MySql数据库中连接查询分为以下几种方式: 1.内连接查询 内连接查询通过关键字 inner join 关键字来实现,通过代码实现: select * from 表1 inner join 表2 ...

随机推荐

  1. if else 和 swith效率比较

    读大话设计模式,开头的毛病代码用if else实现了计算器,说计算机做了三次无用功,优化后是用switch,那么switch为什么比if else效率高呢, 百度找了几个说是底层算法不一样,找了一个比 ...

  2. 记一次数据丢失(电脑硬盘closed to down)的经历

    早上-高高兴兴上班去. 到了公司,突然发现出现windows.logo一两秒的时候会蓝屏,surprise. 百度了一下代码,ok修改硬盘格式,从ACHI到IDE 进入Bios,嗯?感觉界面变了,咋回 ...

  3. C++ BASS 实例

    #include <iostream> #include <string> #include <map> #include "..\sdk\bass\in ...

  4. Windows taskkill

    TASKKILL [/S system [/U username [/P [password]]]]         { [/FI filter] [/PID processid | /IM imag ...

  5. #define SYSTEMSERVICE(_func) KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)_func+1) 这

    这个跟KeServiceDescriptorTable的结构有关 下面是KeServiceDescriptorTable的结构定义 KeServiceDescriptorTabletypedef st ...

  6. selenium python bindings 项目结构总结

    一个合理的文档结构在import的过程中会避免很多错误,踩完坑来记录. webtests/ framework.py webdriver.py test_file.py module/ __init_ ...

  7. sql数据库还原,出现媒体簇的结构不正确,SQLServer无法处理此媒体簇的解决方法

    问题: sql数据库还原,出现媒体簇的结构不正确,SQL Server无法处理此媒体簇. 异常如下图. 造成问题的原因: 我的电脑上安装了sql2005和sql2008,问题就在于我用sql2008的 ...

  8. 通过java进行电脑屏幕截图

    package image; import java.awt.Desktop; import java.awt.Dimension; import java.awt.Rectangle; import ...

  9. 【bzoj 2870】 最长道路tree

    题目 边分治 边分和点分相比就是找到一条重心边,考虑所有经过这条边的路径,之后断开这条边分成两个联通块,继续分治 由于每次分治重心是一条边,所以只会产生两个联通块,考虑两个联通块显然要比像点分那样考虑 ...

  10. HYNB Round 8: 2016 ICPC Amritapuri Regionals

    HYNB Round 8: 2016 ICPC Amritapuri Regionals A - Tim and BSTs 做法 经典的树 DP 问题. \(dp[u][i]\) 表示考虑以 u 为根 ...