I have the code to create a table:
DROP TABLE IF EXISTS `database`.`creatures_kinds` ;
CREATE TABLE IF NOT EXISTS `database`.`creatures_kinds` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` INT UNSIGNED NULL,
`name_en` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`, `parent_id`),
INDEX `parent_id` (`parent_id` ASC),
CONSTRAINT `parent`
FOREIGN KEY (`parent_id`)
REFERENCES `database`.`creatures_kinds` (`id`)
ON DELETE RESTRICT
ON UPDATE CASCADE)
ENGINE = InnoDB;
What surprise me is that the newly created table looks like this:
Why the parent_id
is set to NOT NULL
when I wrote NULL
in the CREATE TABLE
?
Does it has something to do with the PRIMARY KEY
or FOREIGN KEY
?
I guess that parent_id
is an identifiable relation (it points to more general creature kind, e.g. from pigeon
to bird
, and it's NULL
for top-level creature kinds).
A
PRIMARY KEY
is a unique index where all key columns must be defined asNOT NULL
. If they are not explicitly declared asNOT NULL
, MySQL declares them so implicitly (and silently). MySQL Manual
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments