MYSQL的基础操作

2018-08-16  本文已影响0人  65f0ee1aa08d

备份一下基础命令,以便以后查阅
:P

一、数据库操作
二、表操作
三、数据操作
四、关系
五、内置函数
六、备份与恢复
七、与Python交互

一、数据库操作


二、表操作

create table student(
id int auto_increment primary key not null,
name varchar(10) not null,
gender bit default 1,
birthday datetime
);
alter table student add birthday datetime;

三、数据操作

查询

select * from 表名;
select 列名,... from 表名 where 条件;

增加
修改

update 表名 set 列1=值1,... where 条件

删除
alter table 表名 add isDelete bit default=0;
update 表名 set isDelete=1 where 条件;
聚合
分组
select 列1,..,聚合 from 表名
group by 列1,...
having 列1,...聚合
排序

order by 列1 asc|desc, 列2 asc|desc
asc升序(默认)

分页

select * from 表名 limit start, count;
从start开始获取count条数据
start从0开始

完整的select语句(顺序应该如下所示)
select distinct * 
from 表名
where 条件
group by 列名 having 条件 
order by 列名
limit start,count

四、关系

外键引用

创建表时
foreign key(当前表的列名) references 外表(外表主键)
创建表后
alter table 表名 add constraint 约束名 forign key(列名) reference 外表名(列名)

外键的级联操作
连接
select student.name, sum(scores.score)
from scores
inner join student on scores.stuid=student.id
where student.gender=1
group by student.name;
自关联
create table areas(
id int primary key auto_increment not null,
title varchar(20),
pid int,
foreign key(pid) references areas(id)
);
select city.* from areas as city
inner join areas as province on city.pid=province.id
where province.title='山西'
从sql文件中导入数据
source areas.sql;
视图
create view v_1 as
select stu.*,sco.score,sub.title from scores as sco
inner join student as stu on sco.stuid=stu.id
inner join subject as sub on sco.subid=sub.id;
alter view v_1 as
select stu.*,sco.score,sub.title from scores as sco
inner join student as stu on sco.stuid=stu.id
inner join subject as sub on sco.subid=sub.id
where stu.isdelete=0 and sub.isdelete=0;
事务

使用事务的情况: 当数据被更改时,包括insert,update,delete

索引

开启时间监测
set profiling=1;
查看执行时间
show profiles;
可以用来查看建立索引前后时间差距


五、内置函数

字符串函数
数学函数
类型转换函数

cast(value as type)
convert(value,type)


六、备份与恢复

备份
恢复

七、与Python交互

上一篇 下一篇

猜你喜欢

热点阅读