MySQL Null和Joins vs Tables

时间:2016-12-21 19:10:48

标签: php mysql sql database

在此代码中:

CREATE TABLE `institution` (
    `id` SMALLINT UNSIGNED AUTO_INCREMENT,
    `name` VARCHAR(70) NOT NULL,
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE announcement (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `institution` SMALLINT UNSIGNED NOT NULL,
    `content` TEXT NOT NULL,
    CONSTRAINT `fk_announcement_institution`
        FOREIGN KEY (`institution`) REFERENCES `institution` (`id`),
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE `reaction` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT,
    `announcement` INT UNSIGNED NOT NULL,
    `user` INT UNSIGNED,
    `content` TEXT NOT NULL,
    CONSTRAINT `fk_reaction_announcement`
        FOREIGN KEY (`announcement`) REFERENCES `announcement` (`id`),
    CONSTRAINT `fk_reaction_user`
        FOREIGN KEY (`user`) REFERENCES `user` (`id`),
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

反应可以由用户或机构(首先创建公告的人)添加,我决定使用“用户”列来确定添加反应的学生,如果是< strong> null 我将确定它是该机构。

这没关系吗?以这种方式使用null?或者我应该这样做(所以我离开加入以确定哪个属于用户以及哪个属于该机构):

CREATE TABLE `institution` (
    `id` SMALLINT UNSIGNED AUTO_INCREMENT,
    `name` VARCHAR(70) NOT NULL,
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE announcement (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `institution` SMALLINT UNSIGNED NOT NULL,
    `content` TEXT NOT NULL,
    CONSTRAINT `fk_announcement_institution`
        FOREIGN KEY (`institution`) REFERENCES `institution` (`id`),
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE `reaction` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT,
    `announcement` INT UNSIGNED NOT NULL,
    `content` TEXT NOT NULL,
    CONSTRAINT `fk_reaction_announcement`
        FOREIGN KEY (`announcement`) REFERENCES `announcement` (`id`),
    PRIMARY KEY (`id`))
ENGINE = InnoDB;

CREATE TABLE `reaction_user` (
    `reaction` BIGINT UNSIGNED,
    `user` INT UNSIGNED,
    CONSTRAINT `fk_reaction_user_reaction`
        FOREIGN KEY (`reaction`) REFERENCES `reaction` (`id`),
    CONSTRAINT `fk_reaction_user_user`
        FOREIGN KEY (`user`) REFERENCES `user` (`id`),
    PRIMARY KEY (`reaction`, `user`))
ENGINE = InnoDB;

另外两个问题:

哪个表更好,连接次数更少,连接次数多,表少?

和...

我在堆栈溢出中读到很多问题,InnoDB只存储1位的空值,但是在手册中说:“SQL NULL值在记录目录中保留一个或两个字节。如果存储在可变长度列中,则SQL NULL值在记录的数据部分中保留零字节。在固定长度列中,它在记录的数据部分中保留列的固定长度。保留固定的NULL值的空间允许将列的更新从NULL更新为非NULL值,而不会导致索引页碎片化“。那么谁是对的?

谢谢。

0 个答案:

没有答案