SQL Server 2019来自不同表的多列上的外键

发布于 2025-02-08 13:22:53 字数 505 浏览 2 评论 0原文

因此,我必须创建三个表:

  1. 第一表是: 在 ,profname nvarchar(50) ,profusername nvarchar(50));
  2. 第二个是
      create table tblstudent(studentId int Indentity(1,1)主键
                             ,StudentName nvarchar(50)
                             ,NVARCHAR(50));
     

我想创建第三个表格,该表应该有两个外键,第一个键是从表Tblprof中散发出来的,而Secaond的键则是从表TblStudent中的StudentId。 因此,这是两个奇异表的两个外键,这是可能的吗? 如果是这样,我应该如何创建它?

so i have to create three tables :

  1. the first one is :
    create table TblProf(profId int IDENTITY(1,1) PRIMARY KEY
                        ,profName nvarchar(50)
                        ,profUsername nvarchar(50)); 
    
  2. and the second one is
    create table TblStudent(studentId int IDENTITY(1,1) PRIMARY KEY
                             ,studentName nvarchar(50)
                             ,studentUsername nvarchar(50));
    

and i want to create the third table which should have two foreign keys the first one is profID from the table TblProf and the secaond one is studentId from the table TblStudent.
so it's two foreign keys from two diffrent tables ,is it possible ?
if so how should i create it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攒一口袋星星 2025-02-15 13:22:53

这称为桥接

CREATE TABLE bridge(
    refprofId int FOREIGN KEY REFERENCES TblProf(profId ),
    refstudentId  int FOREIGN KEY REFERENCES TblStudent(PersonID)
    ,PRIMARY KEY(refprofIdm,refstudentId)); 

This is called a bridge table

CREATE TABLE bridge(
    refprofId int FOREIGN KEY REFERENCES TblProf(profId ),
    refstudentId  int FOREIGN KEY REFERENCES TblStudent(PersonID)
    ,PRIMARY KEY(refprofIdm,refstudentId)); 

you can add more columns, when the bridge has its own propertoes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文