------------------------嵌套查询------------------------
use student
select *
from dbo.stu ----not in
where StudentName not in (select Student from dbo.students)
--- 在这里还有一个关键字 exists 有记录的时候返回true 否则就是返回false 不知道怎么用
--下面介绍了一些嵌套用到的一些修改的比较操作符
-- >all 大于最大值
-- >any 大于列表中的最小值
-- =any 与in关键字一样的意思
-- <>any 表示不等于列表中的值 ??
-- <>all 表示不等于列表中的所有值 它与not in 一样的意思
--使用嵌套查询 最高可以哟32级 当然前提是你服务器的内存足够大
----------其次的就是使用相关子查询
--例如:
use AdventureWorks
select EmployeeID,Title,VacationHours
from HumanResources.Employee e1
where e1.VacationHours > (
select avg(e2.VacationHours)
from HumanResources.Employee e2 where e1.Title = e2.Title)
-----------------------创建约束-----------------------------
use student
drop table ss
create table ss
(
[name] varchar(50) not null,
[id] int identity(1,1) not null,
startTime datetime not null default getDate(),--默认约束
constraint aa primary key clustered--主键约束
(id ),
constraint bb unique --唯一约束
([name]),
constraint cc --外键约束
foreign key ([name])
references dbo.tt, --指定表dbo.tt中的主键name来作为这个表的外键
constraint ee check ([name] in ('a','b','c','d'))--检查约束
)