joomla组件的mysql innodb基本设计

发布于 2024-12-10 09:05:10 字数 1422 浏览 0 评论 0原文

我有四个实体:

  • 人员
  • 中心
  • 活动
  • 地址

所以我的想法是:

  • 一个人可能有一个关联的地址。
  • 中心可能有关联的地址。
  • 活动可能有关联的地址。

这个 mysql 设计对于 joomla 组件正确吗?

CREATE TABLE `#__person` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__center` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__activity` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__address` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`line1` varchar(30),
`line2` varchar(30),
`locality` varchar(10),
`region` varchar(10),
`country` varchar(10),
`postcode` varchar(10),
`person_id` int(10),
`center_id` int(10),
`activity_id` int(10),
PRIMARY KEY (id),
FOREIGN KEY `person_id` REFERENCES `#__person` (id) ON DELETE CASCADE,
FOREIGN KEY `center_id` REFERENCES `#__center` (id) ON DELETE CASCADE,
FOREIGN KEY `activity_id` REFERENCES `#__activity` (id) ON DELETE CASCADE,
other fields...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

也就是说:

  • 如果我删除一个人,他/她对应的地址会自动删除吗?
  • 如果我删除一个地址,他们的参考资料会怎样? (希望没有)
  • 如果同一个地址是中心地址和活动地址,会发生什么?
  • 外键是否在正确的表中,或者我应该在人员、中心和活动中放置一个address_id字段?

我对我的设计有点困惑。

感谢您的建议。

I have four entities:

  • person
  • center
  • activity
  • address

So my idea is:

  • A person may have an associated address.
  • A center may have an associated address.
  • An activity may have an associated address.

Is this mysql design correct for a joomla component?

CREATE TABLE `#__person` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__center` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__activity` (
`id` int(10) NOT NULL AUTO_INCREMENT,
other fields...
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `#__address` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`line1` varchar(30),
`line2` varchar(30),
`locality` varchar(10),
`region` varchar(10),
`country` varchar(10),
`postcode` varchar(10),
`person_id` int(10),
`center_id` int(10),
`activity_id` int(10),
PRIMARY KEY (id),
FOREIGN KEY `person_id` REFERENCES `#__person` (id) ON DELETE CASCADE,
FOREIGN KEY `center_id` REFERENCES `#__center` (id) ON DELETE CASCADE,
FOREIGN KEY `activity_id` REFERENCES `#__activity` (id) ON DELETE CASCADE,
other fields...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;  

So i.e:

  • If I delete a person, automatically his/her corresponding address will be deleted?
  • And if I delete an address, what will happen to their references? (hope nothing)
  • What happens if the same address is an address for a center and for an activity?
  • Are the foreign keys in the correct table or should i put an address_id field in person,center and activity?

I'm a bit confused about my design.

Thank you for your suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美不过初阳 2024-12-17 09:05:10

我建议不要将人员、中心和活动的 id 放入地址表中,而是将 address_id 放入其他每个表(人员、中心和活动)中。然后,您可以在其他表中以及人员、中心和活动的不同组合中重复使用相同的地址。

关于级联,我认为MySQL参考手册解释了很好

CASCADE:从父表中删除或更新行,并且
自动删除或更新子表中的匹配行。
支持删除级联和更新级联。之间
两个表,不要定义多个ON UPDATE CASCADE子句
在父表或子表的同一列上。

因此,如果您确实将 address_id 更改为其他每个表中的值,然后应用了外键约束,

FOREIGN KEY `address_id` REFERENCES `#__address` (id) ON DELETE CASCADE

那么当您删除地址表中的行(没有外键约束的行)时,任一人、中心中的匹配行或活动(定义外键的地方)将被删除。但是,我猜这可能不是您想要的,因为您说一个人、中心和活动“可能”有一个地址。因此,如果您删除了该地址,则不一定要删除正在使用该地址的人员、中心或活动。如果这是真的,那么我会将 3 个表中每个表中的外键约束更改为:

FOREIGN KEY `address_id` REFERENCES `#__address` (id) ON DELETE SET NULL

Instead of placing an id for person, center and activity into the address table, I would suggest placing an address_id into each of the other tables (person, center, and activity). Then, you could reuse the same address across the other tables and in different combinations of person, center, and activity.

And about cascade, I think the MySQL Reference manual explains it well:

CASCADE: Delete or update the row from the parent table, and
automatically delete or update the matching rows in the child table.
Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between
two tables, do not define several ON UPDATE CASCADE clauses that act
on the same column in the parent table or in the child table.

So if you did change address_id to be in each of the other tables and then you applied the foreign key constraint

FOREIGN KEY `address_id` REFERENCES `#__address` (id) ON DELETE CASCADE

then when you deleted the row in the address table (the one WITHOUT the foreign key constraint), the matching rows in either person, center, or activity (where the foreign keys are defined), would be deleted. BUT, I am guessing that this might not be what you want, because you said a person, center and activity "may" have an address. So if you deleted the address, you do not necessarily want to delete the person, center or activity that was using that address. If that is true, then I would change your foreign key constraint in each of the 3 tables to:

FOREIGN KEY `address_id` REFERENCES `#__address` (id) ON DELETE SET NULL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文