MySQL Error 1005?

后端 未结 1 776
猫巷女王i
猫巷女王i 2021-01-24 18:26

I\'m trying to create a database, but am getting a strange error... This is my code

DROP TABLE IF EXISTS `Person`;
DROP TABLE IF EXISTS `Address`;
DROP TABLE IF          


        
相关标签:
1条回答
  • 2021-01-24 18:48

    When creating a foreign key, the data types should match that of the primary key. Change the PrimaryContact field in the Customer table to match the data type of PersonId, in this case, int:

    CREATE TABLE Customer
    (
    CustomerID INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(CustomerID),
    CustomerCode VARCHAR(255),
    CustomerType VARCHAR(255),
    PrimaryContact int,
    FOREIGN KEY `fk_Customer_to_Person` (PrimaryContact) REFERENCES Person(PersonID),
    CustomerName VARCHAR(255),
    CustomerAirlineMiles FLOAT NOT NULL
    );
    

    The columns must be of the same type as seen in the manual page entitled Using FOREIGN KEY Constraints:

    Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

    0 讨论(0)
提交回复
热议问题