DDL:数据定义
DML:数据管理
DCL:数据控制
use database 数据库名
drop database 数据库名
show databases;
create table if not exists 数据库名 default charset=utf8;
use 数据库名
create table if not exists 表名(
字段1 字段类型(字段的长度) 字段属性 字段约束,
字段2 字段类型(字段的长度) 字段属性 字段约束,
字段2 字段类型(字段的长度) 字段属性 字段约束,
.....);
desc 表名
show tables
drop table if exists 表名
alter table 旧表名 rename as 新表名
alter table 表名 change 旧字段名 新字段名 字段类型及属性
alter table 表名 modify 字段名 字段类型属性
alter table 表名 add 字段名 字段类型及属性
alter table 表名 drop 字段名
主 键:primary key
唯 一:unique
自增长:auto_increment
外 键:foreign key
create table if not exists `表名`(
字段1 类型(长度) primary key)
create table if not exists `表名`(
字段1 类型(长度) unique)
create table if not exists `表名`(
字段1 类型(长度) auto_increment)
01.建表时添加
CREATE TABLE `表名` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL, PRIMARY KEY (`id`),
CONSTRAINT `外键名` FOREIGN KEY (`从表字段`)
ENGINE=InnoDB DEFAULT CHARSET=utf8
02.建表之后添加
alter table 从表表名 add co nstraint 外键名称
foreign key(从表某字段) REFERENCES 主表(主表的某字段)
03.注意事项
(1)主表中的字段只能有主键约束时才能添加外键
(2)需要添加外键约束的表的类型必须是InnoDB
create table if not exists 表名(
字段1 字段类型(字段的长度) 字段属性 字段约束,
字段2 字段类型(字段的长度) 字段属性 字段约束,
...)engine=InnoDB/MyISAM
create table if not exists 表名(
字段1 字段类型(字段的长度) 字段属性 \
字段约束 commet "字段注释" default 默认值
alter table 表名 drop foreign key '外键名'.
01->指定字段名
insert into 表名(字段1,字段2,字段三....) values(值1,值2,值3...)
02->不指定字段名
insert into 表名 values(值1,值2,值3...)values
中给定数据的个数必须与字段数相同
03->同时添加多条数据
insert into 表名 values(值1,值2,值3...),(值1,值2,值3...).....
01->条件删除
delete from 表名 where condition
02->删除所有数据
delete from 表名
truncate [table] 表名
区别:
delete 删除数据后索引不会重置
truncate 会将索引重置
01->根据条件更新部分数据
update 表名 set 字段1=value1,字段2=value2.... where condition
02->更新所有的数据
update 表名 set 字段1=value1,字段2=value2....
01->无条件查询
001->查询表中所有数据
select * from 表名 # * 代表所有字段
002->查询指定的字段
select 字段1,字段2,字段3... from 表名
02->条件查询
001->语法
select * from 表名 where condition
002->查询条件
(1)between...and 查询字段在某一区间内的所有记录
(2)is null 查询字段值为null的所有记录
(3)is not null 查询字段值不为null的所有记录
(4)like 模糊匹配,查询字段中出现给定字符的所有记录
(5)in 查询字段的值在某个集合内的所有记录("张三","李四")
(6)条件1 and 条件2 and 条件三....条件n
查询同时满足条件1,条件2,条件n的所有记录数
(7)条件1 or 条件2 or 条件三....条件n
查询满足条件1至条件n中某一个条件的所有记录
(8)not 条件1
查询不符合条件1所有记录
(9)条件1 xor 条件2
查询不同时满足条件1和条件2的所有记录
(10)算术运算符
> < >= <= = (!= <>)不等于
003.分组查询
select 字段1,字段2... from 表名 group by 字段x
按照字段x对查询到的数据进行分组
select c_id,c_name,c_madein from commodity group byc_madein
将查询出的商品按照插地进行分组
c_id:商品的id
c_name:商品的名称
c_madein:商品的产地
004.分页查询(每页包含记录数pageNum=5)开始的索引位置, 每页包含的记录数
select * from commodity limit (page-1)*pageNum, pageNum
第一页: 0 5
第二页: 5 5
005.排序
select * from commodity order by c_inprice asc/desc
对查询到的结果按照进价进行升序/降序排列
006.子查询:已知数码产品的商品分类的ct_id为0,
根据该条件从商品表中查询出所有的数码产品
select * from commodity where c_type=
(select ct_id from commoditytype wherect_name="数码产品")
表a数据 m行数据
id name
1 a
2 aa
4 aaaa
表b的数据 n行数据
id name
1 b
2 bb
3 bbb
select * from a left join b on a.id=b.id
结果:
1 a 1 b
2 aa 2 bb
select * from a inner join b on a.id=b.id
id name id name
1 a 1 b
2 aa 2 bb
select * from a right join b on a.id=b.id
id name id name
1 a 1 b
2 aa 2 bb
3 bbb
select * from a full join b
03.函数
001->求和函数:sum()
select sum(c_num) as "所有商品库存" from commodity
查询所有商品进货量的总和
002->求平均:avg()
select avg(c_inprice) as "进价均值" from commodity
查询所有商品进价的均值
003->count()
0001->查询表中所有的记录数
select count(*) from 表名
0002->查询某个字段不为null值的所有记录数
select count(c_num) as "进货量不为null的商品种类数" from commodity
004->max()/min
select max(字段名) from 表名
查询表中某个字段的最大值
select min(字段名) from 表名
查询表中某个字段的最小值
pip install pymysql
导入模块
import pymysql
链接数据库
connect = pymysql.connect(host="",user="",
password="",db="",charset="")
cursor = connect.cursor()
sql = "insert into tableName values('%s','%d')"
执行
cursor.execute(sql%(value1,value2))
提交
connect.commit()
01->获取结果集中所有数据
all = cursor.fetchall()
02->获取结果集中一条数据
one = cursor.fetchone()
03->获取结果集中的多条数据
many = cursor.fetchmany()
fields = cursor.description
cursor.close()
connect.close()
请用sql语句完成以下需求(30分)
(1)使用sql创建出如下图所示的数据表,数据库名为movies,表名为movieRank,表中包含MovieName、boxOffice、percent、days、totalBoxOffice五个字段,(添加数据)
create database movies;
create table if not exists movieRank(
MovieName varchar(255) primary key,
boxOffice float NOT NULL,
percent float NOT NULL,
days int(11) NOT NULL,
totalBoxOffice float NOT NULL);
(2)使用sql语句向movieRank表中添加若干条数据(材料中已提供movieData.txt)
insert into movieRank values('21克拉',1031.92,0.1518,2,2827.06),
('狂暴巨兽',2928.28,0.4307,9,57089.20),
('起跑线',161.03,0.0237,18,19873.43),
('头号玩家',1054.87,0.1552,23,127306.41),
('红海行动',45.49,0.0067,65,364107.74);
(3)使用sql语句查询movieRank表中的数据并按照totalBoxOffice字段进行排序
select * from movierank;
select * from movierank order by totalBoxOffice;
(4) 使用sql语句计算出字段totalBoxOffice字段的总和
select sum(totalBoxOffice) as sum from movierank ;