SQL设计问题,急需各位帮忙

vivvan 2007-12-09 09:06:07
希望‘客户’表中的交易量、交易额改变时,参照‘级别’表中的‘交易量标准’、‘交易额标准’

自动地更改‘客户’的‘级别’,本人试过用规则绑定,错误好多,来这求帮忙啊~


下面是创的表和索引:

create table 级别 (
级别名 varchar(10) not null,
折扣率 float not null,
交易量标准 int null,
交易额标准 float null,
constraint PK_级别 primary key (级别名)
)
create table 客户 (
客户名 varchar(10) not null,
交易额 float null,
交易量 int null,
constraint PK_客户 primary key (客户名)
)
create index 打折2_FK on 打折 (
级别名 ASC
)
alter table 属于
add constraint FK_属于_属于_级别 foreign key (级别名)
references 级别 (级别名)
add constraint FK_属于_属于2_客户 foreign key (客户名)
references 客户 (客户名)
...全文
77 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2007-12-10
  • 打赏
  • 举报
回复
create   table   客户   ( 
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)

alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)


insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go

create trigger trigger_update
on 客户
for update
as
if Update(交易量) or Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 >= 交易量标准 and i.交易额 >= 交易额标准 order by 交易量标准 desc)
from 客户 a
right join inserted i
on a.客户名 = i.客户名

end

go



select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'

select * from 客户
update 客户
set 交易额 = 20100
where 客户名 = '张三'

select * from 客户
update 客户
set 交易量 = 51
where 客户名 = '张三'

select * from 客户
drop table 客户,级别

/*


客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00

(所影响的行数为 3 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 普通会员 4 1000.00

(所影响的行数为 3 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 普通会员 4 20100.00

(所影响的行数为 3 行)


(所影响的行数为 1 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 白金会员 51 20100.00

(所影响的行数为 3 行)


*/
-狙击手- 2007-12-10
  • 打赏
  • 举报
回复
create   table   客户   ( 
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)

alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)


insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go

create trigger trigger_update
on 客户
for update
as
if Update(交易量)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 > 交易量标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名

end
else if Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易额 > 交易额标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名

end
go



select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'

select * from 客户
update 客户
set 交易额 = 1100
where 客户名 = '张三'

select * from 客户

drop table 客户,级别

/*


客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00

(所影响的行数为 3 行)


(所影响的行数为 3 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 普通会员 4 1000.00

(所影响的行数为 3 行)


(所影响的行数为 3 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 VIP 4 1100.00

(所影响的行数为 3 行)
*/
-狙击手- 2007-12-10
  • 打赏
  • 举报
回复
create   table   客户   ( 
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)

alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)


insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000
go

create trigger trigger_update
on 客户
for update
as
if Update(交易量)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量 > 交易量标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名

end
go



select * from 级别
select * from 客户
update 客户
set 交易量 = 4
where 客户名 = '张三'

select * from 客户

drop table 客户,级别

/*


客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 普通会员 2 1500.00
王风 VIP 10 16000.00
张三 VIP 8 1000.00

(所影响的行数为 3 行)


(所影响的行数为 3 行)


(所影响的行数为 1 行)

客户名 级别名 交易量 交易额
---------- ---------- ----------- ------------
林清 NULL 2 1500.00
王风 NULL 10 16000.00
张三 普通会员 4 1000.00
*/
vivvan 2007-12-10
  • 打赏
  • 举报
回复
下面是表,和数据。

我要实现的是修改每个人的交易量和交易额时,判断他是哪一级会员

就是说,现在的张三是VIP,如果把他的交易量改为3后,他能立即更新变成‘普通会员’或者是交易额改为800时能立即更新变成‘普通会员’,而其它数据不变

create table 客户 (
客户名 varchar(10) not null,
级别名 varchar(10) null,
交易量 int not null,
交易额 decimal(10,2) not null,
constraint PK_客户 primary key (客户名)
)
create index 属于_FK on 客户 (
级别名 ASC
)
create table 级别 (
级别名 varchar(10) not null,
折扣率 decimal(3,2) not null,
交易量标准 int not null,
交易额标准 float not null,
constraint PK_级别 primary key (级别名)
)

alter table 客户
add constraint FK_客户_属于_级别 foreign key (级别名)
references 级别 (级别名)


insert 级别 select '普通会员',1,0,0
insert 级别 select 'VIP',0.98,5,1000
insert 级别 select '黄金会员',0.95,15,8000
insert 级别 select '白金会员',0.92,50,20000
insert 客户 select '张三','VIP',8,1000
insert 客户 select '林清','普通会员',2,1500
insert 客户 select '王风','VIP',10,16000

vivvan 2007-12-09
  • 打赏
  • 举报
回复
弄错了。
都是用power designer 生成的
属于在这:
/*==============================================================*/
/* Index: 属于_FK */
/*==============================================================*/
create index 属于_FK on 属于 (
级别名 ASC
)
go

/*==============================================================*/
/* Index: 属于2_FK */
/*==============================================================*/
create index 属于2_FK on 属于 (
客户名 ASC
)
go
-狙击手- 2007-12-09
  • 打赏
  • 举报
回复
一个触发器搞定呀
搞不清你的关系 ,写一个简单的

create   table   级别   ( 
级别名 varchar(10) not null,
折扣率 float not null,
交易量标准 int null,
交易额标准 float null

)
create table 客户 (
客户名 varchar(10) not null,
交易额 float null,
交易量 int null,
级别名 varchar(10)

)


go
insert 级别 select 'a',10,100,1000
insert 级别 select 'b',10,1000,100000
insert 客户 select 'asdf',0,0,NULL
go

create trigger trigger_update
on 客户
for update
as
if Update(交易额)
begin
update 客户
set 级别名 = (select top 1 级别名 from 级别 where i.交易量<交易量标准 and i.交易额<交易额标准)
from 客户 a
left join inserted i
on a.客户名 = i.客户名

end
go


update 客户 set 交易额 = 1000,交易量 = 100
select * from 客户
/*


客户名 交易额 交易量 级别名
---------- ----------------------------------------------------- ----------- ----------
asdf 1000.0 100 b

(所影响的行数为 1 行)
*/


drop table 客户,级别
-狙击手- 2007-12-09
  • 打赏
  • 举报
回复
属于 在哪儿呀

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧