demo 外键

limit子句

方案一:只用来限制长度,即数据量:limit 数据量;

方案二:限制起始位置,限制数量:limit 起始位置,长度;

limit offset,length;

length:每页显示的数据量,基本不变

offset = (页码-1)*每页显示量

多字段排序

group_concat(字段);

回溯统计

with rollup;

having子句

与where子句一样,是进行条件判断的

having能够使用字段别名

order by子句

基本语法:order by 字段名 [asc|desc]

-- 求出所有班级人数大于等于2的学生人数

select c_id,count(*)from my_studentgroup by c_idhaving count(*)>=2;

select c_id,count(*)as totalfrom my_studentgroup by c_idhaving total>=2;

-- having子句进行条件查询

select name as 名字,number as 学号from my_studenthaving 名字like '张%';-- 正确的

select name as 名字,number as 学号from my_studenthaving 名字like '张%';-- 错误的

-- 排序

select * from my_studentgroup by c_id;

select * from my_studentorder by c_id;

-- 多字段排序:先班级排序,后性别排序

select * from my_studentorder by c_id,sexdesc;

-- 查询学生:前两个

select * from my_studentlimit 2;

select * from my_studentlimit 0,2;-- 记录数是从0开始编号

-- 更改id班级表的第一列

alter table my_classchange id id int first ;

-- 交叉连接

select * from my_studentcross join my_class;-- my_student cross join my_class是数据源

-- 内连接

select * from my_studentinner join my_classon my_student.c_id = my_class.c_id;-- 完整写法

select * from my_studentinner join my_classon c_id = my_class.c_id;-- 可以,只有学生表有c_id

select * from my_studentinner join my_classon c_id =id;-- 不行,两张表都有id字段

-- 字段和表别名

select * from my_studentas sinner join my_classas con s.c_id = c.id;

select s.*,c.name as c_name,c.room -- 字段别名

from my_studentas sinner join my_classas c-- 表别名

on s.c_id = c.id;

-- 把学生表id为5的记录c_id设置为null

update my_studentset c_id=null where id=5;

-- 不写on条件,就是笛卡尔积

select s.*,c.name as c_name,c.room -- 字段别名

from my_studentas sinner join my_classas c-- 表别名

where s.c_id = c.id;

-- where 代替 on

select s.*,c.name as c_name,c.room -- 字段别名

from my_studentas sinner join my_classas c-- 表别名

where s.c_id = c.id;

-- 左连接

select s.*,c.name as c_name,c.room -- 字段别名

from my_studentas sleft join my_classas c-- 左表为主表:最终记录至少不少于左表已有的记录数

on s.c_id = c.id;

-- 右连接

select s.*,c.name as c_name,c.room -- 字段别名

from my_studentas sright join my_classas c-- 右表为主表:最终记录至少不少于右表已有的记录数

on s.c_id = c.id;

select s.*,c.nameas c_name,c.room-- 字段别名

from my_studentas sright join my_classas s

on s.c_id = c.id;

-- 自然内连接

select * from my_studentnatural join my_class;

-- 修改班级表name字段为c_name

alter table my_classchange name c_name varchar(20)not null ;

-- 自然左外连接

select * from my_studentnatural left join my_class;

-- 外连接模拟自然外连接:using

select * from my_studentleft join my_classusing (id);

-- 创建外键

create table my_foreign1(

id int primary key auto_increment,

    name varchar(20)not null comment '学生姓名',

    c_id int comment '班级id',-- 普通字段

-- 增加外键

    foreign key (c_id)references my_class(id)

)charset utf8;

-- 创建表

create table my_foreign2(

id int primary key auto_increment,

    name varchar(20)not null comment '学生姓名',

    c_id int comment '班级id'-- 普通字段

)charset utf8;

-- 增加外键、

alter table my_foreign2add

    -- 指定外键名

constraint student_class_1

-- 指定外键字段

foreign key (c_id)

-- 引用父表主键

references my_class(id);

-- 删除外键

alter table my_foreign1drop foreign key my_foreign1_ibfk_1;

-- 插入数据:外键字段在父表中不存在

insert into my_foreign2values (null,'郭富城',5); -- 没有5班级

insert into my_foreign2values (null,'项羽',1);

insert into my_foreign2values (null,'刘邦',2);

insert into my_foreign2values (null,'韩信',2);

-- 更新父表记录

update my_classset id=5 where id=1; -- 失败 :id=1的班级记录已经被学生引用

update my_classset id=5 where id=3; -- 可以:没有引用

-- 插入数据

insert into my_foreign1values (null,'马超',3);

-- 增加外键

alter table my_foreign1add foreign key (c_id)references my_class(id);

-- 创建外键

create table my_foreign3(

id int primary key auto_increment,

    name varchar(20)not null ,

    c_id int,

    -- 增加外键

    foreign key (c_id)

-- 引用表

        references my_class(id)

-- 指定删除模式

        on delete set null

        -- 指定更新模式

         on update cascade

)charset utf8;

-- 插入数据

insert into my_foreign3values (null,'刘备',1),

(null,'曹操',1), (null,'孙权',1), (null,'诸葛亮',2), (null,'周瑜',2);

-- 解除my_foreign2表的外键

alter table my_foreign2drop foreign key student_class_1;

-- 更新父表的主键

update my_classset id=3 where id=1;

-- 删除父表主键

delete from my_classwhere id=2;

你可能感兴趣的:(demo 外键)