解决MS SQL Server2005中多表查询遇到的重复问题的实例

这是一个关于解决MS SQL Server2005中多表查询遇到的重复问题的实例,以下是例子
例如我们建了以下三个表
表一

create table Student(
sid int constraint PK_Student_sid primary key identity not null,--编号
sno int constraint UK_Student_sno unique not null,--学号(唯一标识)例如:10001
sname varchar(20) not null,--姓名
sage int constraint PK_Student_sage check (sage <= 150 and sage >= 0),--年龄
ssex char(2) default '男' check(ssex in('男','女'))--性别
)
go
 


表二

create table Course(
cid int constraint PK_Course_cid primary key identity not null,--课程编号
cno int constraint UK_Course_cno unique not null,--课程号(唯一)例如:20001
cname varchar(50) not null,--课程名
cteacher varchar(20) not null--教师
)
go
 


表三

create table Grade(
gid int constraint PK_Grade_gid primary key identity not null,--编号
gsno int constraint FK_Grade_gsno_Student_sno foreign key(gsno) references Student(sno) not null,--学号
gcno int constraint FK_Grade_gcno_Course_cno foreign key(gcno) references Course(cno) not null,--课程号
ggrade int constraint DK_Grade_ggrade default 0 constraint CK_Grade_ggrade check(ggrade >= 0) --成绩
)
go
 



当我们用select * from Student, Course, Grade;查询时都会遇到重复的问题,而解决方案是用这个语句:select * from Student, Course, Grade
where gsno = sno and gcno = cno;
呵呵~上面是我这几天刚接触MS SQL Server2005遇到的一个很纠结的问题,虽然对于很多“高手”而言不足问道,但是我还是想把自己遇到的一些问题拿出来,希望对那些刚接触MS SQL Server2005没几天的朋友们有所帮助。。。

猜你喜欢

转载自etwo.iteye.com/blog/1463207