我正在为我的网站设计数据库架构。我有一个job_history
表,其中包含与工作相关的信息。这是表结构:
CREATE TABLE IF NOT EXISTS `website`.`job_history` (
`job_history_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`designation` VARCHAR(255) NULL,
`started` DATE NULL,
`left` DATE NULL,
`work_summary` VARCHAR(3000) NULL,
`company_id` INT UNSIGNED NULL,
`looking_job_change` TINYINT(1) NULL,
PRIMARY KEY (`job_history_id`),
INDEX `fk_job_employment_company1_idx` (`company_id` ASC),
CONSTRAINT `fk_job_employment_company1`
FOREIGN KEY (`company_id`)
REFERENCES `website`.`company` (`company_id`)
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
我感到困惑的地方:由于MYSQL现在具有JSON的功能,为什么我不应该在JSON内放置几列。例如designation
,started
,left
,work_summary
是一种元数据字段,仅解释将(或非常不可能)将要搜索的(工作)详细信息使用WHERE
子句。而且,如果要进行搜索(很少),MySQL对此也具有受支持的功能。
为什么不将它们作为JSON
数据类型,如:
{
"work": {
"designation": "developer",
"started": "may, 2018",
"left": "june, 2019",
"summary": "I was a software developer here"
}
}
新结构为:
CREATE TABLE IF NOT EXISTS `website`.`job_history` (
`job_history_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT UNSIGNED NOT NULL,
`work` JSON NULL,
`company_id` INT UNSIGNED NULL,
`looking_job_change` TINYINT(1) NULL,
PRIMARY KEY (`job_history_id`),
INDEX `fk_job_employment_company1_idx` (`company_id` ASC),
CONSTRAINT `fk_job_employment_company1`
FOREIGN KEY (`company_id`)
REFERENCES `website`.`company` (`company_id`)
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
我为什么要这样做?-从前端添加新字段而无需触摸数据库都很容易。假设将来我想在前端添加一个新字段:why you left
。我什至不需要更改表结构。
以这种方式使用它的优缺点是什么?
请提出您的宝贵意见。谢谢