# --复合主键内连接


  1.  select * from tdx_order t inner join [Tdx_Safety] s on t.id = s.[OrderId] 
  2. ---

  3. if exists(select [id] from sysobjects where name='student')  
  4.     drop table student  
  5. go  
  6.   
  7. create table student  
  8. (  
  9.     sname varchar(20) not null,  
  10.     sclass varchar(20) not null  
  11. )  
  12. go  
  13.   
  14. alter table student add constraint pk_student primary key(sname,sclass);  
  15. go  
  16.   
  17. if exists(select [id] from sysobjects where name='score')  
  18.     drop table score  
  19. go  
  20.   
  21. create table score  
  22. (  
  23.     scoreid int not null primary key identity(1,1),  
  24.     sname varchar(20) not null,  
  25.     sclass varchar(20) not null,  
  26.     score float  
  27. )  
  28. go  
  29.   
  30. alter table score add constraint fk_score foreign key(sname,sclass) references student(sname,sclass);  
  31. go  
  32.   
  33. insert into student values('aaa','1');  
  34. insert into student values('ccc','2');  
  35. insert into student values('bbb','2');  
  36. insert into student values('ddd','2');  
  37. insert into student values('ccc','3');  
  38. insert into student values('ddd','4');  
  39.   
  40. insert into score(sname,sclass,score) values('aaa',1,66);  
  41. insert into score(sname,sclass,score) values('bbb',2,77);  
  42. insert into score(sname,sclass,score) values('ccc',2,88);  
  43. insert into score(sname,sclass,score) values('ddd',4,99);  
  44.   
  45. /*  
  46. --外键插入null值  
  47. insert into score(score) values(66);  
  48. insert into score(score) values(77);  
  49. insert into score(score) values(77);  
  50. */  
  51.   
  52. select * from student;  
  53. select * from score;  
  54.   
  55. --复合主键内连接  
  56. select * from student t inner join score s on t.sname = s.sname and t.sclass = s.sclass;  
  57.   
  58. --复合主键左外连接  
  59. select t.sname,t.sclass from student t left outer join score s on t.sname = s.sname and t.sc
posted @ 2008-10-28 12:40  window5549-accp  阅读(514)  评论(1编辑  收藏  举报