Reputation: 57176
I wonder if any better practice or any principle when come to design a lookup table.
I intend to design an abstract lookup table which can serve many different situations.
For instance, I call my lookup table as a masters and slaves
table,
CREATE TABLE IF NOT EXISTS `masters_slaves` (
`mns_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`master_id` varchar(255) DEFAULT NULL COMMENT 'user id or page id',
`slave_id` varchar(255) DEFAULT NULL COMMENT 'member id or user id or page id',
`cat_id` varchar(255) DEFAULT NULL COMMENT 'category id',
`mns_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`mns_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`mns_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
so this lookup table can server the relationship of these types like,
Admins and Members
Admins and Pages
Admins and Posts
Post Categories and Posts
Page Parents and Pages
etc
the cat_id
in masters and slaves
table will describe and differentiate these categories. for instance, cat_id
1
is Admins and Members and so on.
And I will insert:
master_id
and member id into
slave_id
columnmaster_id
and child page id into
slave_id
columnmaster_id
and page id into
slave_id
columnBut I am sure about it whether I should go for it or not:
Thanks.
Upvotes: 0
Views: 528
Reputation: 12833
It sounds an awful lot like the anti-pattern One True Lookup Table. Google it!
Here is a list of bad things I came up with in less than 1 minutes:
I could easily come up with more, but I don't think it's really needed ;)
When one doesn't have much database experience, it feels like a good thing to do, to reuse stuff and use "common" structures, but databases rarely benefit from it.
Use separate tables for your relationships. Eventually, you will do that anyway.
Upvotes: 4
Reputation: 2809
From my experience it is better to create a lookup table for each category. Here are the benefits as i see them:
I think there are more pros for having a few smaller tables than one big one.
Upvotes: 1