Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Last active August 1, 2024 02:13
Show Gist options
  • Save andrewrcollins/ac994fa48786080033658160f2627733 to your computer and use it in GitHub Desktop.
Save andrewrcollins/ac994fa48786080033658160f2627733 to your computer and use it in GitHub Desktop.
Create entity group attribute value tables.
CREATE TABLE `entity` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `group` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `entity_group` (
`entity_id` INT NOT NULL,
`group_id` INT NOT NULL,
PRIMARY KEY (`entity_id`,`group_id`)
);
CREATE TABLE `entity_attribute` (
`id` INT NOT NULL AUTO_INCREMENT,
`group_id` INT NOT NULL,
`attribute` varchar(128) NOT NULL,
PRIMARY KEY (`id`,`group_id`,`attribute`)
);
CREATE TABLE `entity_value` (
`entity_id` INT NOT NULL,
`entity_attribute_id` INT NOT NULL,
`value` varchar(128) NOT NULL,
PRIMARY KEY (`entity_id`,`entity_attribute_id`)
);
ALTER TABLE `entity_group` ADD CONSTRAINT `entity_group_fk0` FOREIGN KEY (`entity_id`) REFERENCES `entity`(`id`);
ALTER TABLE `entity_group` ADD CONSTRAINT `entity_group_fk1` FOREIGN KEY (`group_id`) REFERENCES `group`(`id`);
ALTER TABLE `entity_attribute` ADD CONSTRAINT `entity_attribute_fk0` FOREIGN KEY (`group_id`) REFERENCES `group`(`id`);
ALTER TABLE `entity_value` ADD CONSTRAINT `entity_value_fk0` FOREIGN KEY (`entity_id`) REFERENCES `entity`(`id`);
ALTER TABLE `entity_value` ADD CONSTRAINT `entity_value_fk1` FOREIGN KEY (`entity_attribute_id`) REFERENCES `entity_attribute`(`id`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment