SQL 嵌套查询 创建约束

------------------------嵌套查询------------------------
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'))--检查约束
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值