Foreign Key Creation

Jason210's profile image Jason210 posted 3 years ago in General Permalink

I noticed sometimes, when I add a foreign key to a column using the GUI, Heiedi generates an index as well. And sometimes it doesn't.

Here's an example of where it does create an index:

CREATE TABLE `dog` (
    `flatNo` VARCHAR(5) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `dogName` VARCHAR(25) NOT NULL COLLATE 'utf8mb4_unicode_ci',
    `race` VARCHAR(25) NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    `sex` ENUM('MALE','FEMALE') NULL DEFAULT NULL COLLATE 'utf8mb4_unicode_ci',
    INDEX `FK_dog_resident` (`flatNo`),
    CONSTRAINT `FK_dog_resident` FOREIGN KEY (`FLATNO`) REFERENCES `resident` (`FLATNO`)
);
And here's an example of where it does not:

CREATE TABLE `contents` (
    `product_code` INT(11) NOT NULL,
    `part_name` CHAR(25) NOT NULL,
    PRIMARY KEY (`product_code`, `part_name`),
    CONSTRAINT `FK_contents_lenses` FOREIGN KEY (`product_code`) REFERENCES `lenses` (`product_code`)
);

I don't understand why it sometimes creates this index and sometimes it doesn't and was hoping someone would explain that. Many thanks.

Please login to leave a reply, or register at first.