primary key and foreign key

前端 未结 4 2106
滥情空心
滥情空心 2021-01-25 05:57

I have 3 tables

   Student    Loan    Book
 - StudentID  LoanID  BookID

which foreign keys do i need to set so when given the student name, sea

4条回答
  •  有刺的猬
    2021-01-25 06:21

    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)
    );
    

提交回复
热议问题