Created
June 13, 2012 09:10
-
-
Save stephenca/2922957 to your computer and use it in GitHub Desktop.
Simple EAV schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `attributes`; | |
CREATE TABLE `attributes` ( | |
`name` VARCHAR(64) NOT NULL, | |
PRIMARY KEY (`name`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
DROP TABLE IF EXISTS `attr_value_pairs`; | |
CREATE TABLE `attr_value_pairs` ( | |
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, | |
`attr` VARCHAR(64) NOT NULL, | |
`value` TEXT NOT NULL, | |
PRIMARY KEY (`id`), | |
CONSTRAINT `fk_attr` | |
FOREIGN KEY (`attr` ) | |
REFERENCES `attributes` (`name` ) | |
ON DELETE NO ACTION ON UPDATE NO ACTION, | |
UNIQUE KEY `id_attr` ( `id`, `attr`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
DROP TABLE IF EXISTS `targets`; | |
CREATE TABLE `targets` ( | |
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, | |
`email` VARCHAR(128) NOT NULL, | |
`attr_value_id` BIGINT(20) UNSIGNED NOT NULL, | |
KEY `target_email_key` (`email`), | |
UNIQUE KEY `id_email_attr` ( `id`, `email`, `attr_value_id`), | |
PRIMARY KEY (`id`), | |
CONSTRAINT `fk_attr_value_pair` | |
FOREIGN KEY (`attr_value_id` ) | |
REFERENCES `attr_value_pairs` (`id` ) | |
ON DELETE NO ACTION ON UPDATE NO ACTION | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment