范式和高级数据查询

范式:Normal Format,是一种离散数学中的知识,是为了解决一种数据的存储与优化的问题,它的终极目标是为了减少数据的冗余

范式是一种分层结构的规范,分为六层:

1NF、2NF、3NF、……6NF

1NF是最底层,要求最低

6NF是最高层,要求最严格

第一范式:要求字段的数据具有原子性,就是需要的数据是否需要拆分才能得到

第二范式:(2NF):要解决表的设计不允许出现部分依赖,给表添加一个id唯一

第三范式(3NF):解决传递依赖的问题

逆规范化:磁盘利用率与效率的对抗,空间换时间

高级数据操作:

新增数据

基本语法:insert into 表名 [(字段列表)] values(值列表);

主键冲突(Duplicate key)

当主键存在冲突的时候,可以选择性地进行处理,进行更新和替换

更新操作:insert into 表名 [(字段列表)] values(值列表) on duplicate key update 字段=新值;

替换:replace insert into 表名 [(字段列表)] values(值列表);

-- 给班级表增加主键

alter table my_classadd primary key (name);

-- 插入数据

insert into my_class

values ('python1910','b409');

insert into my_class

values ('python1910','b501');

-- 主键冲突:更新

insert into my_class

values ('python1910','b510')

-- 冲突处理

on duplicate key update

-- 更新教室

room ='b510';

insert into my_class

values ('python1907','b406');

-- 主键冲突:替换

replace into my_class

values ('python1907','a203');

replace into my_class

values ('python1903','b408');

replace into my_class

values ('python1811','b407');



where子句:返回结果0或1,0代表false,1代表true

判断条件

比较运算符:>、<、>=、<=、!=、<>、=、like、between、and、in/not in

逻辑运算符:&&(and)、||(or)、!(not)


-- 复制创建表

create table my_copylike my_class;

-- 蠕虫复制

insert into my_copyselect * from my_class;

-- 删除主键

alter table my_copydrop primary key ;

insert into my_copyselect * from my_copy;

-- 更新部分1811变成1911

update my_copyset name='python1911' where name='python1811' limit 3;

-- 删除数据:限制记录数为5

delete from my_copywhere name='python1903' limit 5;

-- 给my_student表增加主键

alter table my_studentmodify idint primary key

auto_increment;

-- 清空表:重置自增长

truncate my_student;

-- select 选项

select * from my_copy;

select all * from my_copy;

-- 去重

select distinct * from my_copy;

-- 向学生表插入数据

insert into my_student

values (null,'bc20200001','张三','男'),

(null,'bc20200002','李四','男'),

(null,'bc20200003','王五','男'),

(null,'bc20200004','小明','男');

-- 字段别名

select id, number as 学号, name as 姓名, sexas 性别from my_student;

-- 多表数据源

select * from my_student,my_class;

-- 子查询

select * from (select * from my_student)as s;

-- 增加age年龄和height身高字段

alter table my_student add age tinyint unsigned;

alter table my_student add height tinyint unsigned;

-- 增加字段值:rand取得一个0到1之间的随机数,floor向下取整

update my_student set age=floor(rand()*20+20),height=floor(rand()*20+170);

-- 找学生id为1、3、5的学生

select * from my_student where id=1 || id=5 || id=3;-- 逻辑判断

select * from my_student where id in (1,3,5); -- 落在集合中

-- 找身高在185到190之间的学生

select * from my_student where height>=185 and height<=190;

select * from my_student where height between 185 and 190;

select * from my_student where height>=185 and height<=190;

select * from my_student where 1; -- 所有条件都满足

-- 根据性别分组

select * from my_student group by sex;

-- 分组统计:身高高矮、平均年龄、总年龄

select sex,count(*),max(height),min(height),avg(age),sum(age)from my_student group by sex;

-- 修改id为1的记录,把年龄设置为null

update my_student set age=null where id=4;

select sex,count(*),count(age),max(height),min(height),avg(age),sum(age)from my_student group by sex;

-- 修改id为1的记录,把性别置位女

update my_student set sex='女' where id=1;

-- nan nv

-- 倒序

select sex,count(*),count(age),max(height),min(height),avg(age),sum(age)from my_studentgroup by sexdesc;

-- 删除班级表主键

alter table my_class drop primary key ;

-- 给班级表增加主键id

alter table my_class add id int primary key auto_increment;

-- 给学生表增加一个班级表的id

alter table my_student add c_id int;

update my_student set c_id=ceil(rand()*4);

insert into my_student values (6,'bc20200006','小芳','女',18,160,2);

-- 多字段分组:先班级,后男女

select c_id,sex,count(*)from my_student group by c_id,sex;-- 多字段排序

select c_id,sex,count(*),group_concat(name)from my_student group by c_id,sex;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容