mysql 不区分大小写(类b)
数据库
创建数据库 create database 数据库名 charset=ust8
删除数据库 delete database 数据库名
使用某个数据库 use 数据库名
显示所有数据库 show databases
显示当前使用的数据库 select database()
表
创建表 create table 表名(字段,类型,约束)
id int auto_increment primary key not null
varchar
foreign key(stuid) references students(id)
显示所有的表 show tables
显示创建表的语句 show create table scores;
删除表 drop table 表名
修改表 alter table 表名 add|change(修改类型)|drop 列
数据crud
从sql文件中导入数据 source 文件名
插入数据 insert into 表名(字段,类型,约束:不能为空的都要有)values(当不指定插入数据时,每一个都要包括)
修改数据 update 表名 set 字段名=值 where
删除数据 delete from 表名 --- 可添加条件
逻辑删除 isdelete
查询数据 select 列 from 表名 where --- 可添加条件
MySQL 查询
select * from 表名 where --- 可添加条件
select 后面写列名 *表示所有列
例:select id,birthday from students;
例:select distinct gender from students;--- 消除重复的值
条件
条件包括:1 比较用算符 = > < !=
2 逻辑用算符 and or not
3 模糊查询 % 任意多个字符
_ 任意一个字符
例 select * from students where name like '郭%'
4 范围查询 in
例 select * from students where id in(1,5,3)
例 select * from students where id between 3 and 8
5判空查询 null
select * from students where birthday is null
小括号,not ,比较运算符,逻辑运算符
and 比 or 先运算 ,可以使用小括号改变顺序
聚合(目的为统计)---不再看到数据
1 count
select count(*) from students
2 max
select max(id) from students
3 min
select min(id) from students
4avg --平均(默认保留四位小数)
5 sum
分组
select 列1,列2, 聚合......from 表名 group by 列1,列2,列3.......
例 select gender count(*) group by gender;
分组后筛选
having
#where 写入位置在from的后面,having在group by 的后面
#having 和 where 都是对行进行筛选
#where 是对 from后的原始集进行筛选,having 是对select 后的结果集进行筛选
#having 和 where 后的规则一样
例 select gender as xingbie,count(*) from students group by gender having xingbie=0;
排序
select * from 表名 order by 列1 asc|desc,列2 asc|desc......
#按照列1进行排列,列1相同按照列2排列
#默认值从小到大(升序asc)
分页
select * from 表名 limit start,count
#关键字 limit
完整-------:
select distinct *
from 表名 where.....
group by..... having......
order by.....
limit start,count
执行顺序:
( from
join )这两个语句创建了一个新的表格
where
group by
select distinct *
having
order by
limit start,count
三关系(实体之间)
1 - 1 :任意表都可以
1 - n : n所在的表
n - n : 在建一张表存储关系
外键约束
create table scores(
id int auto_increment primary key not null,
stuid int,
subid int,
score decimal(4,1),
foreign key(stuid) references students(id),
foreign key(subid) references subjects(id));
外键级联操作(建议做逻辑删除)
1: restrict --- 默认抛出异常
2 逻辑删除
链接查询
select students.name,subject.title,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id
1: inner join 两个表的交集
2: left join 两个表的交集 + 左表
3:right join 两个表的交集 + 右表
查询总结:
select distinct 列(*表示所有列)
from 表1
inner | left | right join 表名 on 表1.=表2.
where.....
group by..... having......
order by..... asc| desc
limit start,count
连接查询:
男生 姓名 总分
select students.name,sum(scores.score)
from scores
inner join students on students.id=scores.stuid
where gender=1
group by scores.stuid;
科目 平均分
select subjects.title,avg(scores.score)
from scores
inner join subjects on subjects.id=scores.subid
group by scores.subid;
未删除科目名称 最高分 平均分
select subjects.title,max(scores.score),avg(scores.score)
from scores
inner join subjects on subjects.id=scores.subid
where isDelete=0
group by subid;
自关联查询
查询省名称为山西省的所有城市:
select provinces.id,provinces.title,city.*
(from areas as city
inner join areas as provinces on city.pid = provinces.id)相当于重新构建了一张表,这个表包括 provinces.id,provinces.title,city.*
where provinces.title='山西省'
视图(view)---封装查询
create view v_1 as
select stu.*,sub.title,sco.score
from scores as sco
inner join students as stu on stu.id=sco.stuid
inner join subjects as sub on sub.id=sco.subid;
事务(未完成时退回)----引擎必须为innodb(主要) or bdb
使用事务的情况 当数据被更改时 insert delete update
四大特性:1原子性
2一致性
3隔离性--枷锁实现
4持久性
begin 开始-----临时更改 在内存中存在
commit 提交 ------ 对文件产生实际的更改
rollback 回滚 ------ 放弃begin中所作的更改(即:内存中的),实际文件不改变
# commit 和 rollback 只能存在一个
索引(和你的输入数据的顺序有关系) ----提高数据访问性能 对where处进行处理---即跳过无用的数据,直接定位到所需数据的区域
*** 主键和唯一索引都是索引
1 小的数据类型更好-----性能 bit > int >decimal
2 简单数据类型更好------int > char
3 避免null---用特殊值代替
等值写到前面,范围写到后面
单列索引
组合索引
创建索引 create index indexname on table(username(length),username(length))
删除索引 drop index [indexname] on table
查看索引 show index from students
查看执行时间
set profiling=1 ---开启时间监控
show profiles -----查看
python and mysql:
connection 尽量晚打开,尽量早关闭
connect(host,port,user,passwd,db,charset)
cursor()
commit
close
cursor
excute(sql,params)
如果执行insert update delete语句时,需要conn.commit()
fetchone()
fetchall()
closs
数据库
创建数据库 create database 数据库名 charset=ust8
删除数据库 delete database 数据库名
使用某个数据库 use 数据库名
显示所有数据库 show databases
显示当前使用的数据库 select database()
表
创建表 create table 表名(字段,类型,约束)
id int auto_increment primary key not null
varchar
foreign key(stuid) references students(id)
显示所有的表 show tables
显示创建表的语句 show create table scores;
删除表 drop table 表名
修改表 alter table 表名 add|change(修改类型)|drop 列
数据crud
从sql文件中导入数据 source 文件名
插入数据 insert into 表名(字段,类型,约束:不能为空的都要有)values(当不指定插入数据时,每一个都要包括)
修改数据 update 表名 set 字段名=值 where
删除数据 delete from 表名 --- 可添加条件
逻辑删除 isdelete
查询数据 select 列 from 表名 where --- 可添加条件
MySQL 查询
select * from 表名 where --- 可添加条件
select 后面写列名 *表示所有列
例:select id,birthday from students;
例:select distinct gender from students;--- 消除重复的值
条件
条件包括:1 比较用算符 = > < !=
2 逻辑用算符 and or not
3 模糊查询 % 任意多个字符
_ 任意一个字符
例 select * from students where name like '郭%'
4 范围查询 in
例 select * from students where id in(1,5,3)
例 select * from students where id between 3 and 8
5判空查询 null
select * from students where birthday is null
小括号,not ,比较运算符,逻辑运算符
and 比 or 先运算 ,可以使用小括号改变顺序
聚合(目的为统计)---不再看到数据
1 count
select count(*) from students
2 max
select max(id) from students
3 min
select min(id) from students
4avg --平均(默认保留四位小数)
5 sum
分组
select 列1,列2, 聚合......from 表名 group by 列1,列2,列3.......
例 select gender count(*) group by gender;
分组后筛选
having
#where 写入位置在from的后面,having在group by 的后面
#having 和 where 都是对行进行筛选
#where 是对 from后的原始集进行筛选,having 是对select 后的结果集进行筛选
#having 和 where 后的规则一样
例 select gender as xingbie,count(*) from students group by gender having xingbie=0;
排序
select * from 表名 order by 列1 asc|desc,列2 asc|desc......
#按照列1进行排列,列1相同按照列2排列
#默认值从小到大(升序asc)
分页
select * from 表名 limit start,count
#关键字 limit
完整-------:
select distinct *
from 表名 where.....
group by..... having......
order by.....
limit start,count
执行顺序:
( from
join )这两个语句创建了一个新的表格
where
group by
select distinct *
having
order by
limit start,count
三关系(实体之间)
1 - 1 :任意表都可以
1 - n : n所在的表
n - n : 在建一张表存储关系
外键约束
create table scores(
id int auto_increment primary key not null,
stuid int,
subid int,
score decimal(4,1),
foreign key(stuid) references students(id),
foreign key(subid) references subjects(id));
外键级联操作(建议做逻辑删除)
1: restrict --- 默认抛出异常
2 逻辑删除
链接查询
select students.name,subject.title,scores.score
from scores
inner join students on scores.stuid=students.id
inner join subjects on scores.subid=subjects.id
1: inner join 两个表的交集
2: left join 两个表的交集 + 左表
3:right join 两个表的交集 + 右表
查询总结:
select distinct 列(*表示所有列)
from 表1
inner | left | right join 表名 on 表1.=表2.
where.....
group by..... having......
order by..... asc| desc
limit start,count
连接查询:
男生 姓名 总分
select students.name,sum(scores.score)
from scores
inner join students on students.id=scores.stuid
where gender=1
group by scores.stuid;
科目 平均分
select subjects.title,avg(scores.score)
from scores
inner join subjects on subjects.id=scores.subid
group by scores.subid;
未删除科目名称 最高分 平均分
select subjects.title,max(scores.score),avg(scores.score)
from scores
inner join subjects on subjects.id=scores.subid
where isDelete=0
group by subid;
自关联查询
查询省名称为山西省的所有城市:
select provinces.id,provinces.title,city.*
(from areas as city
inner join areas as provinces on city.pid = provinces.id)相当于重新构建了一张表,这个表包括 provinces.id,provinces.title,city.*
where provinces.title='山西省'
视图(view)---封装查询
create view v_1 as
select stu.*,sub.title,sco.score
from scores as sco
inner join students as stu on stu.id=sco.stuid
inner join subjects as sub on sub.id=sco.subid;
事务(未完成时退回)----引擎必须为innodb(主要) or bdb
使用事务的情况 当数据被更改时 insert delete update
四大特性:1原子性
2一致性
3隔离性--枷锁实现
4持久性
begin 开始-----临时更改 在内存中存在
commit 提交 ------ 对文件产生实际的更改
rollback 回滚 ------ 放弃begin中所作的更改(即:内存中的),实际文件不改变
# commit 和 rollback 只能存在一个
索引(和你的输入数据的顺序有关系) ----提高数据访问性能 对where处进行处理---即跳过无用的数据,直接定位到所需数据的区域
*** 主键和唯一索引都是索引
1 小的数据类型更好-----性能 bit > int >decimal
2 简单数据类型更好------int > char
3 避免null---用特殊值代替
等值写到前面,范围写到后面
单列索引
组合索引
创建索引 create index indexname on table(username(length),username(length))
删除索引 drop index [indexname] on table
查看索引 show index from students
查看执行时间
set profiling=1 ---开启时间监控
show profiles -----查看
python and mysql:
connection 尽量晚打开,尽量早关闭
connect(host,port,user,passwd,db,charset)
cursor()
commit
close
cursor
excute(sql,params)
如果执行insert update delete语句时,需要conn.commit()
fetchone()
fetchall()
closs