HABTM relationship MySQL tables for use in Rails


/ Published in: SQL



Copy this code and paste it in your HTML
  1. DROP TABLE IF EXISTS `products_tags`;
  2. DROP TABLE IF EXISTS `tags`;
  3. DROP TABLE IF EXISTS `products`;
  4.  
  5. CREATE TABLE `products` (
  6. `id` INT(11) NOT NULL AUTO_INCREMENT,
  7. `title` VARCHAR(100) NOT NULL,
  8. `price` DECIMAL(10,2) NOT NULL,
  9. PRIMARY KEY (`id`)
  10. )
  11.  
  12. CREATE TABLE `tags` (
  13. `id` INT(11) NOT NULL AUTO_INCREMENT,
  14. `title` VARCHAR(64) NOT NULL,
  15. PRIMARY KEY (`id`)
  16. )
  17.  
  18. CREATE TABLE `products_tags` (
  19. `product_id` INT(11) NOT NULL DEFAULT '0',
  20. `tag_id` INT(11) NOT NULL DEFAULT '0',
  21. PRIMARY KEY (`product_id`,`tag_id`)
  22. )

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.