1.要求在yuangong表上增加一列为录入员,并将该字段设为默认约束,值为:张红.请用SQL语句写出从创建默认约束到删除的一系列过程.
要求两种方法
<1>
create table yuangong
(
姓名 varchar(10),
性别 char(2),
年龄 int
)
alter table yuangong add 录入员 varchar(10) constraint zh default ('张红')
<2>
create default 默认值名
as
值
alter table yuangong add 录入员 varchar(10)
create default 姓名 as '张红'
SP_BINDEFAULT '姓名','yuangong.姓名'
2.现在你们公司在北京设置了一个专卖店,这个专场店只能针对北京五大区的用户进行销售。
也就是说填写客户资料时,客户所在地只能是海淀区,朝阳区,通州区、崇文区、顺义区,并且客户邮编必须正确(全部是数字),
每一条记录都会有一个录入日期字段,取当取日期,请自拟建表实现,使用SQL语句写出实现过程
create table zmd
(
姓名 varchar(8),
地址 varchar(16),
邮编 char(8),
日期 datetime,
constraint dz check (地址='海淀区' or 地址='朝阳区' or 地址='崇文区' or 地址='通州区' or 地址='顺义区'),
constraint yb check (邮编 like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
)
alter table zmd add constraint ww check (日期=getdate())
alter table zmd add constraint yb check (邮编 like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
以下为一个大题:
3 .创建用于存储收货人资料的recipient表。下面是要存储到表中的收货人资料
--创建表
create table Recipient
( OrderNo char(6) not null,
userName varchar(20)not null,
Address varchar(20) null,
City char(15) null,
CountryId char(3) null, 国家
ZipCode char(10) null, 邮编
Phone char(15) null 电话
)
考虑上面的表结构:
就以下问题参考这些表结构:
a.创建Category(种类)表。在创建表时实施下面的数据完整性规则:
CategoryID属性应作为主健
Category属性应为唯一,但不是主健
Description的描述可存储NULL值
----答案:
create table Category
(
CategoryId int not null primary key,
Category varchar(10),
Description varchar(50) null,
constraint cate unique (category)
)
b.创建ToyBrand(商标)表。在创建表时实施下面的数据完整性规则:
BrandID属性应作为主健
BrandName属性应为唯一,但不是主健
----答案:
create table toybrand
(
brandid int not null primary key ,
brandname varchar(10),
constraint brand unique (brandname)
)
c.创建Toys表。在创建表时实施下面的数据完整性规则:
ToysID属性应作为主健
ToyName和ToyDescription属性不可取NULL值
玩具的siToyQoh现有数量应在0到200之间
玩具的最低年龄LowerAge缺省为1
CategoryId属性的值应出现在Category表中
----答案:
create table toys
(
toysid char(6) not null primary key,
toyname varchar(10) not null,
toydescription varchar(10) not null,
siToyQoh varchar(10) constraint si check (siToyQoh between 1 and 200),
Lowerage int constraint lowerage default 1,
categoryID char(6) constraint FK foreign key references category(categoryid)
)
d.修改Toys表。实施下面的数据完整性规则:
BrandID属性的值应出现在ToyBrand表中
alter table toys add brandid char(6) constraint www foreign key (brandid) references toybrand(brandid)
----答案:
4. 现有两个表学生表和学生借书记录表
结构如下:
Create Table Student( --学生表
StudentID int primary key, --学号
studentname varchar(10) -- 学生姓名
)
Create Table BorrowRecord( --学生借书记录表
BorrowRecord int identity(1,1), --流水号
StudentID int , --学号
BorrowDate datetime, --借出时间
ReturnDAte Datetime, --归还时间 )
要求使用触发器实现以下功能
A. 如果更改了学生的学号,希望他的借书记录仍然与这个学生相关(也就是同时更改借书记录表的学号);
B. 如果该学生已经毕业,希望删除他的学号的同时,也删除它的借书记录。
答:
Create Table Student
(
StudentID int primary key,
studentname varchar(10)
)
Create Table BorrowRecord
(
BorrowRecord int identity(1,1),
StudentID int ,
BorrowDate datetime,
ReturnDAte Datetime,
)
create trigger tri
on student for update
as
begin
if exists (select * from inserted)
begin
update borrowrecord set studentid=(select studentid from inserted )
where studentid=(select studentid from deleted)
end
end
create trigger trigg
on student for delete
as
begin
if exists (select * from deleted)
begin
delete from borrowrecord where studentid=(select studentid from deleted)
end
end
5. 要求:建立2个表:cust_test和order_test)
cust_test: CustomerID char(5) PK order_test: CustomerID char(5)--对应关系
Custcity 所在城市 Orderid PK 订单序号
Custname 产品名称 OrderNames 订购的产品名称
CStatus int 库存状态 OStatus int --状态
Cstorage int 库存量 Orders int --定购
Cdate date 日期 Odate date--日期
作业1:
在cust_test表中建立删除触发器,实现上述2表的级联删除。
create table cust_test
(
CustomerID char(5),
Custcity varchar(20),
Custname varchar(10)
)
create table order_test
(
CStatus int,
Cstorage int ,
Cdate datetime
)
alter table order_test add constraint waijian foreign key (customer) references cust_test (customerid)
create trigger tri
on cust_test for delete
as
if exists (select * from deleted)
begin
delete from order_test where customerid in (select customerid from deleted)
print '删除已成功!'
end
作业2:
在order_test表上建立一个插入触发器,在添加一个订单时,减少cust_test表的相应货物的记录的库存量。
create trigger tri on order_test for insert
as
begin
if (select count(*) from inserted)>0
begin
update cust_test set cstorage=cstorage-1
end
end
作业3:
在order_test表上建立一个插入触发器,规定订单日期(Odate)不能修改。
create trigger tri on order_test for update
as
begin
if exists(select * from updated where Cdate='datetime')
begin
rollback
print'日期不可修改!!!'
end
end
作业4:
要求订购的物品一定要在仓库中有的,并且数量足够。
create trigger tri
on order_test for insert
as
if exists(select * from inserted) and
exists(select * from cust_test where customerid in (select customerid from inserted)) and
exists(select * from cust_test where custname in (select ordernames from inserted) and cstorage>=(select orders from inserted))
begin
commit
print '你的订单已存入数据库!'
end
else
begin
rollback
print '很抱歉!此产品已经不足或已经销售完!!!'
end
本文出自 “梦想与缘分” 博客,谢绝转载!