0

I am stuck with this situation: I have my main system settings table to hold all the default system values. An example is default language is English. default filter is to have age restriction on for filtering user content, etc. There are about 40-ish different values to maintain for now.

Now all these values are stored in multiple lookup tables and also in the master field_value table which has all the small lookups. Language codes are in the language lookup table. Age filter has its own lookup table, and many others. Other smaller settings are all in the field_value table.

So I need to reference the setting_value field to all these tables unless i have 1 colunm per setting and keep the rest as NULL.

Currently my schema is this

CREATE TABLE `settings` (
  `setting_id` int(6) NOT NULL,
  `description` varchar(32) NOT NULL,
  `code` int(6) DEFAULT NULL,
  `created` datetime NOT NULL,
  `updated` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`setting_id`),
  KEY `code` (`code`),
  CONSTRAINT `settings_ibfk_1` FOREIGN KEY (`code`) REFERENCES `field_values` (`fv_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And it does not work to handle settings from multiple tables across the system.

2 Answers 2

0

You could try super-type/subtype. Keep all common columns in the Setting table.

alt text

create table Setting (
      SettingID   integer     not null
    , SettingType char(1)     not null
    , SettingName varchar(32) not null
    , Description varchar(32) not null
    , Created     timestamp default CURRENT_TIMESTAMP
    -- other common-to-all-setups columns here
);
alter table Setting add constraint pk_setting primary key (SettingID);


create table LanguageSetting (
      SettingID   integer     not null
    , Updated     timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
    -- other columns specific to this setup-type here
);
alter table LanguageSetting 
  add constraint pk_langset  primary key (SettingID)
, add constraint fk1_langset foreign key (SettingID) references Setting(SettingID);


create table AgeFilterSetting (
      SettingID   integer     not null
    , Updated     timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
    -- other columns specific to this setup-type here
);
alter table  AgeFilterSetting 
  add constraint pk_ageflt  primary key (SettingID)
, add constraint fk1_ageflt foreign key (SettingID) references Setting(SettingID);


create table OtherSetting (
      SettingID   integer     not null
    , Updated     timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP
    -- other columns specific to this setup-type here
);
alter table  OtherSetting 
  add constraint pk_othset  primary key (SettingID)
, add constraint fk1_othset foreign key (SettingID) references Setting(SettingID);

.

1
  • Hm, this is what i need. Let me try and map it out, there are so many tables so I need to design it out and see if it works. But thanks.
    – Manni
    Jan 6, 2011 at 22:32
0

You might use inheretance in your data model. The parent table has a primary-key and the children table have their primary-key as a foreign-key to the parent table. Here is another SO question with a similar thread.

MSSQL DDL Example:

CREATE TABLE [Child1](
    [ParentId]  int    NOT NULL,
    CONSTRAINT [PK2] PRIMARY KEY CLUSTERED ([ParentId])
)

CREATE TABLE [Child2](
    [ParentId]  int    NOT NULL,
    CONSTRAINT [PK3] PRIMARY KEY CLUSTERED ([ParentId])
)

CREATE TABLE [Parent](
    [ParentId]  int    NOT NULL,
    CONSTRAINT [PK1] PRIMARY KEY CLUSTERED ([ParentId])
)


ALTER TABLE [Child1] ADD CONSTRAINT [RefParent1] 
    FOREIGN KEY ([ParentId])
    REFERENCES [Parent]([ParentId])

ALTER TABLE [Child2] ADD CONSTRAINT [RefParent3] 
    FOREIGN KEY ([ParentId])
    REFERENCES [Parent]([ParentId])
4
  • How to make a colunm have a PK and FK together? I thought it is one or the other, not both together?
    – Manni
    Jan 6, 2011 at 19:55
  • I added DDL from a tool that doesn't output to mysql. This example has a parent table with two children. I omitted any other columns, but it should show the idea. Jan 6, 2011 at 20:12
  • @Manni, a column can be both a PK and an FK if the tables are in a one-to-one relationship, in fact that helps preserve the data from becoming a one-to-many when you don't want it to.
    – HLGEM
    Jan 6, 2011 at 22:38
  • Yes, i just found that while googling around on fks.
    – Manni
    Jan 6, 2011 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.