我有一个T-SQL问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 09:12:04
查询出指定时间内指定的会员卡详细的消费信息
好像要用内表链结。
但不知道怎么用。
有交易记录,会员卡帐号,员工表两个表。
一下是所有我写的.
<<<<-------------------------------------->>>>
create database 联华超市管理系统
on
(
name="联华_mdf",
filename="D:\寒假作业\S2Y04作业(林)\联华超市管理系统.mdf",
size=5MB,
maxsize=10mb,
filegrowth=15%
)
log on
(
name="联华_ldf",
filename="D:\寒假作业\S2Y04作业(林)\联华超市管理系统.ldf",
size=5MB,
filegrowth=1
)
go

use 联华超市管理系统
create table 员工表
(
员工编号 varchar(50) not null,
员工级别 varchar(30) not null,
员工身份证号 varchar(50) not null
)

alter table 员工表
add constraint UQ_联华_mdf unique (员工编号)

alter table 员工表
add constraint PK_联华_mdf primary key (员工编号)

use 联华超市管理系统
create table 交易记录
(
交易编号 varchar(50) not null,
时间 datetime not null,
物品 char not null,
售出时间 datetim

问题一
use 联华超市管理系统
create table 交易记录
(
交易编号 varchar(50) not null,
时间 datetime not null,
物品 char not null,
售出时间 datetime not null,
当班员工号 varchar(50) not null,
售出件数 int not null,
售出价格 int not null,
)

alter table 交易记录
add constraint FK_当班员工号
foreign key(当班员工号) references 员工表(员工编号)

alter table 交易记录
add constraint PK_消费会员卡号 primary key(消费会员卡号)

你的 交易记录 没有 消费会员卡号 这列

2.你的 会员卡帐号 表没PK,建一个
alter table 会员卡帐号
add constraint PK_会员卡帐号 primary key (卡号)

3.给 交易记录 表,加个FK
alter table 交易记录
add constraint FK_会员卡帐号_交易记录 Foreign key (消费会员卡号) references 会员卡帐号 (卡号)

4.表的结构修改完毕,建立查询
Select * from 交易记录 as a join 会员卡帐号 as b on a.消费会员卡号 = b.卡号 where 售出时间 between 时间A and 时间B
(记着格式别弄错了)

建议使用视图,这样可以防止你数据的泄露

你那交易信息表中好像没有家交易会员卡号那个字段
如果有 单个表查询 就可以了 用select * from 交易表 where 交易时间between 时间1 and 时间2

尽量用 英文命名