主键和外键 [英] primary key and foreign key

查看:143
本文介绍了主键和外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张桌子

I have 3 tables

   Student    Loan    Book
 - StudentID  LoanID  BookID

哪些外键需要设置,因此在给定学生名称时,搜索来自该学生的所有贷款并显示书籍详细信息

which foreign keys do i need to set so when given the student name, search all loan from that student and display the book detail

推荐答案

以下是一些含糊的要求:

Here's a start with such vague requirements:

CREATE TABLE dbo.Students
(
  StudentID INT PRIMARY KEY
  -- , other columns about students
);

CREATE TABLE dbo.Loans
(
  LoanID    INT PRIMARY KEY,
  StudentID INT NOT NULL FOREIGN KEY REFERENCES dbo.Students(StudentID)
  -- , other columns about loans
);

CREATE TABLE dbo.Books
(
  BookID INT PRIMARY KEY,
  -- , other columns about books
);

CREATE TABLE dbo.StudentBooks
(
  StudentID INT NOT NULL FOREIGN KEY REFERENCES dbo.Students(StudentID),
  BookID    INT NOT NULL FOREIGN KEY REFERENCES dbo.Books(BookID)
);

这篇关于主键和外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆