joomla组件的mysql innodb基本设计
我有四个实体:
- 人员
- 中心
- 活动
- 地址
所以我的想法是:
- 一个人可能有一个关联的地址。
- 中心可能有关联的地址。
- 活动可能有关联的地址。
这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要将人员、中心和活动的 id 放入地址表中,而是将 address_id 放入其他每个表(人员、中心和活动)中。然后,您可以在其他表中以及人员、中心和活动的不同组合中重复使用相同的地址。
关于级联,我认为MySQL参考手册解释了很好:
因此,如果您确实将 address_id 更改为其他每个表中的值,然后应用了外键约束,
那么当您删除地址表中的行(没有外键约束的行)时,任一人、中心中的匹配行或活动(定义外键的地方)将被删除。但是,我猜这可能不是您想要的,因为您说一个人、中心和活动“可能”有一个地址。因此,如果您删除了该地址,则不一定要删除正在使用该地址的人员、中心或活动。如果这是真的,那么我会将 3 个表中每个表中的外键约束更改为:
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:
So if you did change address_id to be in each of the other tables and then you applied the foreign key constraint
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: