数据库
启动数据库:net start mysql
登录数据库:mysql -u root -p
登录数据库:mysql -h localhost -P 3306 -u root -p
-h 主机地址,本机就是localhost 或者127.0.0.1
-P 端口号,必须大写P,区分密码
-u 表示User用户名
-p 表示对应的密码,如果直接显示输入必须连着p写,反之不写,Enter之后会提示输入密码,安全输入
下载数据库 :mysqld.exe --install
清除数据库缓存 :sys delete mysql
数据库关键字优先级
from > where > group by > having > select > order by > limit
数据库代码优先级
select * form 表名 where 条件 order by group by having limit
数据库小操作
起别名:as
去重:distinct
比较运算符:> < >= <= != <>(不等于)
逻辑运算符:and(并且) or(或者) not (不在)
模糊查询:like % _
正则查询: rlike
排序:asc (升序) desc(降序)
空判断: is
非空判断: is not
聚合函数:max( )最大 min( )最小 sum( )求和 avg( )平均值
保留小数: round select round(avg(age),2) from stu;
分组:group by
将分组的结果连接起来:group_concat:group_concat
分页 :limit 可以写1个参数 可以两个
左连接 :left join on
右连接 :right join on
正则: rlike
库操作
1.查看库:show databases
2.创建库:create database 库名
3.删除库:drop database 库名
4.切换库:use 库名
5.显示创建库细节:show create database 库名
6.创建gbk库:create database 库名 character set gbk
7.退出数据库:exit、quit
表操作
1.显示所有表:show tables
2.创建表:create table 表名(类型 字段名,类型 字段名)
3.删除表:drop table 表名
4.查看表结构:desc 表名
5.查看创建表细节:show create table 表名
6.添加列(字段):alter table 表名 add 字段名 类型 约束
7.删除列(字段):alter table 表名 drop 字段名
8.修改表名:alter table 原名字 rename to 新名字;
9.查看外键:show create table 表名;
10.删除外键:alter table 表名 drop foreign key 外键名;
11.查看运行时间 :show profiles;
12.查看索引:show index from 表名
13.删除索引:drop index 索引名 on 表名
14.在表上创建索引:create 索引 index 索引名 on 表名(字段)
15.在已存在的表上创建索引:alter table 表名 add 索引名(字段())
16.创建普通索引:create index 索引名 on 表名(字段())
17.表约束条件
1.主键:primary key
2.自增:auto_increment
3.非空:not null
4.唯一:unique
5.小点:decimal (5,5)
6.枚举:enum( ) 单选
7.浮点型:float(10.2)
18.所属类型
1.数字类型:int, tinyint
2.字符串类型:char(固定长度的小型数据)、varchar(不固定长的小型数据)、小点:decimal(5,5)
3.时间类型
1.date:年月日
2.datetime:年月日时分秒
3.time:时分秒
4.year:年
插入数据
insert into 表名 values(数据)
insert into 表名(字段名,字段名) values(对应前面字段的数据)
删除数据
1.删除表:delete from 表名
2.物理删除:delete 表名 from 表名 where 字段=数据
3.清空表:truncate table 表名
4.删除数据库:drop database 数据库名;
修改数据
1.update 表名 set 字段=数据
2.指定修改:update 表名 set 字段=数据 where 字段=数据
3.update 表名 set 字段=数据,字段=数据 where 字段=数据
查询数据
1.查询表:select * from 表名
2.查询表指定字段:select * from 表名 where 字段=数据
3.select 字段 from 表名 where 字段=数据
4.select * from 表名 where 字段>=<数值
5.select 字段1 as 名字1,字段2 as 名字2 from 表名 where 字段=数据
6.select *,math+10 from 表名
7.统计每个学生的总分
select 字段 as 姓名,(math+English+Chinese) as 总分 from student
8.查询总分大于230分的同学
select * from 表名 where (math+English+Chinese)>230
9.查询成绩在80-90之间的同学
select * from 表名 where 字段 between 80 and 90
10.查询所有课程大于80的同学成绩
select * from 表名 where 数学>80 and 英语>80 and Chinese>80;
11.查询数学成绩在 80 60 90内的同学,既有60 80 90的
select * from 表名 where math in(80,60,90)
外键操作
外键的作用:保持数据一致性,完整性,主要目的是控制存储在外键表中的数据。 使两张表形成关联,外键只能引用外表中的列的值!
查看外键名字:
show create table 表名;
删除外键:
alter table 表名 drop foreign key 外键名;
添加外键:
alter table 表名 add foreign key(从表字段名) references a(主表字段名);
alter table courses add(cou_id int,foreign key(cou_id) references users(id));
模糊查询
_: 表示任意单个字符。匹配单个任意字符
%: 表示任意0个或多个字符
like:相似
1.查询所有姓名中包含张的同学
select * from 表名 where 字段 like '%张%'
2.找出字段中既有“三”又有“猫”的记录,请运用 and条件
select * from 表名 where 字段 like '%三%' and 字段 like '%猫%'
3.查询出字段中唐三藏的信息
select * from 表名 where 字段 like '_三_'
排序查询
asc:升序
desc:降序
1.按照什么字段进行升序:select * from 表名 order by 字段 asc
2.按照什么字段进行降序:select * from 表名 order by 字段 desc
3.查询未删除男生信息,按学号降序
select * from 表名 where gender=1 and is_delect=0 order by id desc
4.显示所有的学生信息,先按照年龄从大-->小排序,当年龄相同时 按照身高从高-->矮排序
select * from 表名 order by 年龄 desc,身高 desc
分组查询
group by:分组
分组注意事项: 在分组时,select后面跟的的字段一般都会出现在 group by 后
1.将查询结果按一个或多个进行分组,字段值相同的为一组 GROUP_CONCAT(`name`):查看每组里面的姓名都有哪些
select deparmant, GROUP_CONCAT(`name`) from employee GROUP BY deparmant
2.根据性别 分类,看不同的性别都有哪些人
select name,gender from 表名 group by gender,name
分页查询
limit i,n;
i:为查询结果的索引值(默认从0开始),当i=0时可省略i
n:为查询结果返回的数量(也就是条数)
表示从第i+1条开始,取n条数据
limit n 等同于 limit 0,n
索引是从零开始,但是数据是从1开始存储的,也就是说limit 0,1;查询出来的数据是第一条,limit 1,1;查询出来的数据是第二条。
limit只有在mysql才适用,Oracle使用伪列进行分页查询。
在mysql中使用limit来进行分页,例如:
select * from a where a.name like 'j%' limit 1,10;
在oracle中则是利用rownum伪列进行分页查询,例如:
select * from (select a.*,rownum r) tt where tt.r<100;
分页查询公式:(indexPage-1) x pageSize+1,indexPage x pageSize
select * from 表名 limit m,n
聚合函数
count:个数
sum:总数
avg:平均数
max:最大值
min:最小值
1.统计员工个数
select count() from 表名 --包括空值
select count(*) from 表名 --不包括空值
2.求某班平均成绩,求某个公司员工的平均工资
select avg(score) from 表名 --平均成绩
select avg(salary) from 表名 --平均工资
3.求某个班的总成绩?
select sum(score) from 表名
4.求班里最高分
select max(score) from 表名
4.求班里最低分
select min(score) from 表名
5.算数
select round(sum(age)/count(*),2) as 平均值 from stu;
使用:
1.select 语句的选择列表(子查询或外部查询);
2、having 子句;
3、compute 或 compute by 子句中等;
SQL分组+聚合函数
select deparmant, GROUP_CONCAT(salary), SUM(salary),AVG(salary) 平均工资,MAX(salary) 最高工资 from employee GROUP BY deparmant;
1.查询每个部门的部门名称以及每个部门的人数
select deparmant, GROUP_CONCAT(`name`), COUNT(*) from employee GROUP BY deparmant
2.查询每个部门的部门名称以及每个部门工资大于1500的人数
SELECT deparmant,GROUP_CONCAT(salary), COUNT(*) from employee WHERE salary > 1500 GROUP BY deparmant
SQL分组+group by + Having
group by + having 用来分组查询后指定一些条件来输出查询结果
having 和 where 一样,但 having 只能用于 group by
1.查询工资总和大于 9000的部门名称
SELECT deparmant, GROUP_CONCAT(salary), SUM(salary) FROM employee GROUP BY deparmant HAVING SUM(salary) > 9000;
2.查询工资大于2000的,工资总和大于9000的部门名称以及工资和
select deparmant,GROUP_CONCAT(salary), SUM(salary) from employee WHERE salary > 2000
GROUP BY deparmant HAVING sum(salary) > 9000 ORDER BY SUM(salary) DESC;
子查询
什么是子查询
select语句当中嵌套select语句,被嵌套的select语句是子查询。
1.查询李老师的所有学生
select * from stu where id in(select s_id from ts where t_id=(select id from t where name="李老师"))
连接查询
左连接:将两张表对应的数据查询出来,同时将左表自己没有关联的数据也查询出来
1.左连接 :left join on
将左表为主表(left旁边的表名)如果关联表中没有对应的信息,会用null填充
select * from 表1 left join 表2 on 条件;
右连接:将两张表对应的数据查询出来,同时将右表自己没有关联的所有数据查询出来
2.右连接 :right join on
将右表为主表(right旁边的表名)如果关联表中没有对应的信息,会用null填充
select * from 表1 right join 表2 on 条件;
内连接:查询两张表,设定条件,将两张表中对应的数据查询出来,不会产生笛卡尔积,不会产生临时表,性能高
3.内连接 :inner join on
select * form 表1 inner join 表2 on 表1.字段=表2.字段;
select calss_name,sky.id,name,age,gender from xue inner join sky on sky.cls_id=xue.id;
4.自关联:select * from 表1 as 别名 inner join 表2 as 别名 on 别名1.字段=别名2.字段 where 别名1.字段=“内容”;
多表联查
笛卡尔积现象
1、在表的连接查询方面有一种现象被称为:笛卡尔积现象。
2、例子:找出每一个员工的部门名称,要求显示员工名和部门名。
emp表:
多表联合查询可以通过连接运算实现,而连接运算又可以通过广义笛卡尔积后在进行选择运算来实现。
查询条件中要包含连接条件,通过不同的连接条件可以实现等值连接、不等值连接等各种连接。
实例:学生表 student 老师表 teacher 课程表 course 选课表 student_course
1.按“0001”号课程成绩由高到低顺序显示所有学生学号、姓名、成绩(二表连接)
select*fromstudent.student_id,student.student_name,student_couerse.scorsfromstudent,student_couersewherestudent.student_id= student_couerse.student_idandstudent_couerse.student_id='0001'orderbystudent_couerse.scoredesc
正则查询
正则查询: rlike
. 代表一个(任意字母) 任意字母
[ ] 匹配到方括号内的其中一个字母
* 0到任意多个
+ 1到任意多个
? 0或者1个
引导符:^ :在正则表达式最前方使用 ^ ,代表只能按照表达式的格式去匹配
结束符: $ :在正则表达式最后面使用 $ ,代表只能按照表达式的格式去匹配
select * from stu where name rlike"^周.*";
范围查询
范围查询: in not in (between a and b) (not between a and b )
在18、34
select * from stu where age in(18,34);
不在18、34
select * from stu where age not in(18,34);
select * from stu where age between 18 and 34;
不在18到34
select * from stu where age not between 18 and 34;
判断查询
空判断: is
select * from stu where height is null;
非空判断: is not
select * from stu where height is not null;
去重操作
方法:
distinct方法:一般用于比较小的表进行去重,会过滤掉多余的重复记录,返回不重复的记录或字段; rowid方法:根据Oracle带的rowid属性,可以进行判断是否存在重复语句;
group by方法:主要用于分组统计,一般都是使用在聚合函数中使用;
select distinct gender from stu;
select distinct 字段名 from 表名;
索引操作
开启事务:begin; start transaction
提交事务:commit;
回滚事务:rollback;
事务的原子性:
保证了要么同时成功,要么同时失败
一致性:
保证数据一致
隔离性:
执行语句之间不会互相影响
持久性:
数据会一直存在
常见索引:
普通索引: index
唯一索引: primary key--主键、unique--唯一
联合索引: unique(id,name)
全文索引: fulltext 用于搜索一片很长的文章
空间索引: spatial 了解
方法一:创建表直接加上索引
通过pymysql加上10万条数据
开启运行时间检测:set profiling=1;
select * from 表 where 字段=“ha-99”;
查看运行时间 :show profiles;
查看索引:show index from 表名
删除索引:drop index 索引名 on 表名
方法二:在表上创建索引:
create 索引 index 索引名 on 表名(字段)
方法三:在已存在的表上创建索引
alter table 表名 add 索引名(字段())
创建普通索引:
create index 索引名 on 表名(字段())