这是2018.1.8 22:24的更新笔记 start:
(这里的笔记有误被删除)
这是2018.1.8 22:24的更新笔记 end:
-- 多列组合外键,必须用表级约束的方法
-- 自关联,自引用(一般用与,需要查询多个相同的列名的情况下)
create table tree
(id int auto_increment PRIMARY key,
name1 varchar(20),
parent_id int,
FOREIGN key(parent_id) references tree(id)
)
–级联删除:删除主表的数据,关联的从表数据也删除,则需要在建立外键约束的后面增加on delete cascade 或on delete set null,前者是级联删除,后者是将从表的关联列的值设置为null。
-- 增加 on delete cascade 或 on delete set null,前者是级联删除,后者是将从表关联
-- 的列的值设置为null;
create table student4
(id int auto_increment primary key,
name4 varchar(8),
classes_name varchar(20),
classes_number int,
foreign key(classes_name,classes_nunber) references classes1(name1,number) on delete cascade
)
-- check 检查约束,无效果,mysql版本不支持;
create table mm
(id int auto_increment primary key,
name1 varchar(8),
aeg int,
check(age>20)
)
– 修改列
-- 增加add列名
-- 1)alter table 表 add 列 列的类型 列的参数
alter table mm add username char(20) not null DEFAULT ''
-- 2)alter table 表 add 列 列的类型 列的参数 after 某列(把新列加某列后面)
alter table mm add username char(20) not null DEFAULT '' after username
-- 3)alter table 表 add 列 列的类型 列的参数 after 某列(把新列加在最前面)
alter table mm add username char(20) not null DEFAULT '' firstj
– 分组
-- having 一般和group by连用,在结果组后再来设置条件
select ssex as '性别',count(*)
from student
group by ssex
HAVING ssex='男'
-- 查询选修了三门课以上的学生的学号
-- 一定不能是 * ,而是某一个列或者某个列的聚合函数,
-- 顺序:where
-- GROUP BY
-- HAVING
-- ----------------------------------
select语句的执行顺序:
FROM
-- ON
-- JOIN
WHERE -- where的优先级优于聚集函数;
GROUP BY
-- WITH CUBE or WITH ROLLUP
HAVING -- 聚集函数优于having先执行;
SELECT
DISTINCT
ORDER BY
limit
---------------------------------
select * from sc
select sno,count(*)
from sc
group by sno
having count(cno)>3
-- inner join 是比较运算符,只返回符合条件的行。
-- 当在查询之中,不同的表中有了相同字段名称的时候,访问这些字段必须加上表名称,即“表.字段”
--------------------------------------------------
– 多表连接查询
select student.*,sc.*
from student,sc
where student.sno=sc.sno
-- 等同于
select student.*,sc.*
from student INNER JOIN sc ON student.sno=sc.sno -- 条件用on引出
当有多个内连接时,执行顺序是从前往后执行的;