SQL one-to-many

陌路散爱 提交于 2019-12-01 08:28:11

"One-to-many" means that many items (may) reference one item. If it's one channel to many fixtures, then fixtures should reference channels, not the other way round, which means the reference column should be in the fixtures table:

CREATE TABLE channel(
  id INT NOT NULL PRIMARY KEY
  );

CREATE TABLE fixtures(
  id INT NOT NULL PRIMARY KEY,
  channel_id INT NOT NULL FOREIGN KEY REFERENCES channel (id)
  );

You can add a CONSTRAINT just to check it. Sorry for not pasting a snippet... I don't know anything about H2 specifics.

Or you could also avoid the fixture-set concept at all. Then you would just need:

  • channel table, with just the id (plus other fields not involved on that matter, of course)
  • a channelfixtures table, with channelId and fixtureId. Primary key would be composed of (channelId, fixtureId)
  • a fixture table, only if you need it.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!