Can a table have two foreign keys?

前端 未结 4 1804
一整个雨季
一整个雨季 2021-02-02 10:02

I have the following tables (Primary key in bold. Foreign key in Italic)

Customer table

  • ID---Name---Balance---
相关标签:
4条回答
  • 2021-02-02 10:39

    The foreign keys in your schema (on Account_Name and Account_Type) do not require any special treatment or syntax. Just declare two separate foreign keys on the Customer table. They certainly don't constitute a composite key in any meaningful sense of the word.

    There are numerous other problems with this schema, but I'll just point out that it isn't generally a good idea to build a primary key out of multiple unique columns, or columns in which one is functionally dependent on another. It appears that at least one of these cases applies to the ID and Name columns in the Customer table. This allows you to create two rows with the same ID (different name), which I'm guessing you don't want to allow.

    0 讨论(0)
  • 2021-02-02 10:51

    Yes, MySQL allows this. You can have multiple foreign keys on the same table.

    Get more details here FOREIGN KEY Constraints

    0 讨论(0)
  • 2021-02-02 11:05
    CREATE TABLE User (
    user_id INT NOT NULL AUTO_INCREMENT,
    userName VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    userImage  LONGBLOB NOT NULL, 
    Favorite VARCHAR(255) NOT NULL,
    PRIMARY KEY (user_id)
    );
    

    and

    CREATE TABLE Event (
        EventID INT NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY (EventID),
        EventName VARCHAR(100) NOT NULL,
        EventLocation VARCHAR(100) NOT NULL,
        EventPriceRange VARCHAR(100) NOT NULL,
        EventDate Date NOT NULL,
        EventTime Time NOT NULL,
        EventDescription VARCHAR(255) NOT NULL,
        EventCategory VARCHAR(255) NOT NULL,
        EventImage  LONGBLOB NOT NULL,     
        index(EventID),
        FOREIGN KEY (EventID) REFERENCES User(user_id)
    );
    
    0 讨论(0)
  • 2021-02-02 11:06
    create table Table1
    (
      id varchar(2),
      name varchar(2),
      PRIMARY KEY (id)
    )
    
    
    Create table Table1_Addr
    (
      addid varchar(2),
      Address varchar(2),
      PRIMARY KEY (addid)
    )
    
    Create table Table1_sal
    (
      salid varchar(2),`enter code here`
      addid varchar(2),
      id varchar(2),
      PRIMARY KEY (salid),
      index(addid),
      index(id),
      FOREIGN KEY (addid) REFERENCES Table1_Addr(addid),
      FOREIGN KEY (id) REFERENCES Table1(id)
    )
    
    0 讨论(0)
提交回复
热议问题