简体   繁体   中英

Can't I assign a foreign key to a table that isn't a primary to another table?

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'officials_ibfk_1' in the referenced table 'resident'

I created a superclass table with two subclasses table that has a relationship with each other. Why did this error occur? Can't I assign a foreign key to a table that isn't a primary to another table?

CREATE TABLE User (
    `userid` int NOT NULL AUTO_INCREMENT,
    `username` varchar(30) NOT NULL UNIQUE,
    `password` varchar(30) NOT NULL,
    `emailaddress` varchar(50) NOT NULL UNIQUE,
    `lastname` varchar(20),
    `firstname` varchar(20),
    `birthday` date,
    `sex` varchar(7) CHECK (sex IN ('Male', 'Female')),
    `address` varchar(100),
    `billingproof` varchar(100),
     PRIMARY KEY (userid)
);

CREATE TABLE Resident (
    `residentid` int NOT NULL,
    `userid` int,
    `groupid` int,
    `accntstatus` VARCHAR(13) CHECK (accntstatus IN ('Approved', 'For Approval', 'Disapproved')),
    `residenttype` VARCHAR(40) CHECK (residenttype IN ('Individual Resource', 'Service Provider', 'Individual Resource and Service Provider')),
    `householdid` INT,
    `elected` boolean,
     FOREIGN KEY (userid) REFERENCES User(userid)
);  

CREATE TABLE Officials (
    `electedid` int NOT NULL,
    `userid` int,
    `position` varchar(15) CHECK (position IN ('Staff', 'Village Officer')),
    `isSystemAdmin` boolean,
    `startDate` date,
    `endDate` date,
    `residentid` int,
    FOREIGN KEY (residentid) REFERENCES Resident(residentid),
    FOREIGN KEY (userid) REFERENCES User(userid)
);

In MySQL it is not necessary that the referenced key is a primary key, but it must be guaranteed to be unique through an index (see MySQL documentation on foreign key constraints ):

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan.

This is also clearly indicated in the error message you got:

Failed to add the foreign key constraint. Missing index for constraint 'officials_ibfk_1' in the referenced table 'resident'

So to solve it, change:

`residentid` int NOT NULL,

to:

`residentid` int NOT NULL UNIQUE,

A FOREIGN KEY is a key used to link two tables together.

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table. source

CREATE TABLE Resident (
`residentid` int NOT NULL,
`userid` int,
`groupid` int,
`accntstatus` VARCHAR(13) CHECK (accntstatus IN ('Approved', 'For Approval', 'Disapproved')),
`residenttype` VARCHAR(40) CHECK (residenttype IN ('Individual Resource', 'Service Provider', 'Individual Resource and Service Provider')),
`householdid` INT,
`elected` boolean,
 PRIMARY KEY (residentid),
 FOREIGN KEY (userid) REFERENCES User(userid));  
  • You need to make residentid a primary key, so you can foreign key from Officials to Resident

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM