I am designing DB schema for my website.I have a job_history
table which will contain the job related information. This is the table structure:
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
What I am confused in: since the MYSQL has now the power of JSON, why shouldn't I place a few columns inside the JSON. e.g designation
, started
, left
, work_summary
are kind of metadata fields explaining only the details(of job) which will not(or very unlikely) be going to be searched using WHERE
clause. And if they are going be searched(rarely) MySQL has well-supported functions for that as well.
Why not put them as a JSON
datatype as:
{
"work": {
"designation": "developer",
"started": "may, 2018",
"left": "june, 2019",
"summary": "I was a software developer here"
}
}
New structure would be:
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 I want to do it? - it would be easy to add new fields from frontend without even touching the Database. Suppose in future I want to a add new field: why you left
at the front-end. I don't even need to change the table structure.
What are the pros and cons of using it this way?
Please give your valuable thoughts. Thanks