在Django或者Flask中支持数据库驱动: pymysql
在 _init_ .py文件进行伪装
import pymysql
pymysql.install_as_MySQLdb()
常用命令:
1. net start mysql57 - - - - - - 开启数据库服务
2. mysql -uroot -p - - - - - - 登陆mysql 服务
3. select version() - - - - - - 查看当前版本
4. show databases; - - - - - - 查看当前有哪些数据库
5. use mydb; - - - - - - 使用mydb数据库
6. create database (数据库名) charset=’utf8’; - - - - - -创建数据库
7. drop database (数据库名) ; - - - - - - 删除数据库
8. drop table(表名); - - - - - - 删除表
9. show tables; - - - - - -查看数据库有哪些表
创建一个表
create table student(
id int auto_increment primary key;
name varchar(20) not null;
age int not null
);
查
- show tables;- - - - - - 查看数据库中有哪些表
- desc student; - - - - - -查看student表的结构
- select * from student - - - - - -查看student表的所有数据
select name,age from student; - - - - - -查看属性name,age
范围查询:
select * from student where age > 30;
select * from student where age in (10,20,30);
select * from student where age between 24 and 30;模糊查询: like
select * from student where name like ‘ne%’; - - - - - -查询以’ne’开头记录
select * from student where name like ‘%ne%’; - - - - - - 查询包含’ne’的记录
select * from student where name like ‘_ne’; - - - - - -查询第一个字符之后是’ne’的记录
分页:
select * from student limit 0(初始位置),3(显式条数);
排序:
select * from student order by age; - - - - - -(默认)从小到大排序
select * from student order by age desc;- - - - - -从大到小排序
需求: 统计男生和女生各有多少个
select gender,count(*) from student group by gender;
需求: 统计所有女生的个数
select gender,count(*) from student group by gender having gender=1;
需求:查找每一个客户的总金额。
select customer,sum(orderPrice) from table group by customer.
customer–客户;orderPrice–金额;table–表名;group by–对客户进行组合
需求:希望查找订单总金额少于 2000 的客户
select customer,sum(orderPrice) from table group by customer having sum(orderPrice)<2000;
需求:希望查找订单总金额最大的3个客户
select top 3 table.customer from (select customer,sum(orderPrice) from table group by customer) order by orderPrice desc;
增:
alter table student add phone int; - - - - - - 添加新的phone字段,类型为int
insert into student(name,age) values(‘朱大’,23),(’赵云’,34);- - - - - - 给student表插入数据
删:
drop database (数据库名) - - - - - - 删除数据库
drop table (表名) - - - - - - 删除表
alter table student drop phone - - - - - - 删除字段
delete from student where id=1; - - - - - - 删除表student中一条数据
改:
rename table student to stu; - - - - - - 修改表名为stu
alter table student modify name varchar(20); - - - - - - 修改字段类型
alter table student add phone int; - - - - - - 添加新的phone字段,类型为int
alter table student change name number int; - - - - - - 修改原有的name字段为number,且类型为int
alter table student drop phone - - - - - - 删除字段
update student set age=23,name=’吴建’ where id=1;- - - - - - 修改 id=1 的 age,name数据
一对多
create table class(
id int auto_increment primary key,
classname varchar(20) not null
);
create table student(
id int auto_increment primary key,
name varchar(20) not null,
age int(3),
classid int not null,
外键:foreign key(classid) references class(id)
);
多表联合查询分类
关联查询:
一对多
需求:查出某个班的学生
select student.name,class.classname from student,class where student.classid = class.id and class.id = 1;
多对多
需求:查询出选修记录表对应的学生名和课程名?
select stu.*,course.*,sc.* from stu,course,sc where stu.id = sc.stuid and course.id = sc.courseid;
内连接:
选出两个表存在连接关系的字段符合连接关系的那些记录。
外连接:
会选出其他不匹配的记录,分为外左连接和外右连接。
隐式内连接:
基本语法:select 表1.字段 [as 别名], 表n.字段 from 表1 [别名], 表n where 条件;
select username,name from user,goods where user.gid=goods.gid;
查询用户表中哪些用户购买过商品,并将商品信息显示出来(没有join)
显式内连接:
基本语法:select 表1.字段 [as 别名],表n.字段 from 表1 inner join 表2 on 条件;
select username,name from user inner join goods on user.gid=goods.gid;
查询用户表中哪些用户购买过商品,并将商品信息显示出来
外连接(左连接)
基本语法:select 表1.字段 [as 别名], 表n.字段 from 表1 left join 表n on 条件;
select * from user left join goods on user.gid = goods.gid;
以左边为主,查询哪些用户购买过商品,并将商品信息显示出来
外连接(右连接)
基本语法:select 表1.字段 [as 别名], 表n.字段 from 表1 right join 表n on 条件;
select * from user right join goods on user.gid = goods.gid;
以右边为主,查询哪些商品有用户购买,并将用户信息显示出来