How to design parent, child, photos relationships in mysql when photos can belong to either parent or child? ...
Semisimplicity of the category of coherent sheaves?
Derivation tree not rendering
How did passengers keep warm on sail ships?
Problems with Ubuntu mount /tmp
Python - Fishing Simulator
Simulating Exploding Dice
Is it ethical to upload a automatically generated paper to a non peer-reviewed site as part of a larger research?
How to test the equality of two Pearson correlation coefficients computed from the same sample?
Can the prologue be the backstory of your main character?
Keeping a retro style to sci-fi spaceships?
Why is superheterodyning better than direct conversion?
Why can't wing-mounted spoilers be used to steepen approaches?
Road tyres vs "Street" tyres for charity ride on MTB Tandem
What is special about square numbers here?
What are these Gizmos at Izaña Atmospheric Research Center in Spain?
How to pronounce 1ターン?
Can the DM override racial traits?
The variadic template constructor of my class cannot modify my class members, why is that so?
What was the last x86 CPU that did not have the x87 floating-point unit built in?
Why did all the guest students take carriages to the Yule Ball?
What aspect of planet Earth must be changed to prevent the industrial revolution?
Single author papers against my advisor's will?
Create an outline of font
What information about me do stores get via my credit card?
How to design parent, child, photos relationships in mysql when photos can belong to either parent or child?
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Unexplained InnoDB timeoutsFinding rows for a specified date rangeAdding index to large mysql tablesShould I create a multi-column UNIQUE index?Getting a Deadlock,Updating a table with a single UPDATE statement vs several UPDATE statementsSimple query is slow on 4M-rows tableWhat's the minimum privilege needed to alter a foreign key constraint?How to improve query count execution with mySql replicate?FK constraint fails while inserting into child on Disjoint tables
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Let's say I have a parent table and a child table with parent_is as foreign key. Now I want to store photos that could either belong to parent or child. so I could have a parent_id and a child_id foreign keys in the photos table: when the photo belongs to parent then the child_id foreign key column would be null and vice-versa. But instead of that I decided to go for a different design: I now have a item
table and I get the parent
table primary key (parent_id
) to also be foreign key referencing the item_id
column in the item
table, and the child
table primary key (child_id
) also as a foreign key to the same item_id
column in the item
table. So now instead of having two different foreign keys ( referencing to child_id
and parent_id
) in my photos
table I only have one (referencing to item_id
):
CREATE TABLE `parent` (
`parent_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`parent_id`),
CONSTRAINT `parent_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `child` (
`child_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`child_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`child_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `item` (
`item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`item_id`))
CREATE TABLE `photo` (
`photo_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10)unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`photo_id`),
CONSTRAINT `photo_ibfk_1` FOREIGN KEY (`item_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I hope that's clear so far! So that works but now my problem is if I want to delete a parent
record: I would delete the corresponding item_id
record in the item
table which would automatically delete the corresponding record in the parent
table thanks to the foreign key, which then would automatically delete the records in the child
table belonging to that parent. All good except that I would be left with the records in the item
table corresponding to the child records that have just been deleted. The item
table would not know that some child records have been deleted unless the item_id
in the item
table was also a foreign key referencing the corresponding child_id
in the child
table. I've got the feeling my design is wrong? What would be the recommended design in that case?
Here are the real world parent (property
) and child (unit
) tables:
CREATE TABLE `property` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`companyID` int(11) unsigned NOT NULL,
`serviceTypeID` tinyint(3) unsigned DEFAULT NULL,
`name` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`postCode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`extraBed` tinyint(3) unsigned DEFAULT NULL,
`checkIn` tinyint(4) unsigned DEFAULT NULL,
`checkOut` tinyint(4) unsigned DEFAULT NULL,
`submissionDate` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `property_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_2` FOREIGN KEY (`companyID`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_3` FOREIGN KEY (`serviceTypeID`) REFERENCES `serviceType` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)
CREATE TABLE `unit` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`propertyID` int(11) unsigned NOT NULL,
`unitTypeID` tinyint(3) unsigned NOT NULL,
`title` varchar(50) NOT NULL,
`smallDescription` varchar(200) DEFAULT NULL,
`largeDescription` varchar(500) DEFAULT NULL,
`submissionDate` bigint(20) unsigned DEFAULT NULL
PRIMARY KEY (`id`),
CONSTRAINT `unit_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_2` FOREIGN KEY (`propertyID`) REFERENCES `property` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_3` FOREIGN KEY (`unitTypeID`) REFERENCES `unitType` (`id`) ON UPDATE CASCADE)
mysql database-design
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
Let's say I have a parent table and a child table with parent_is as foreign key. Now I want to store photos that could either belong to parent or child. so I could have a parent_id and a child_id foreign keys in the photos table: when the photo belongs to parent then the child_id foreign key column would be null and vice-versa. But instead of that I decided to go for a different design: I now have a item
table and I get the parent
table primary key (parent_id
) to also be foreign key referencing the item_id
column in the item
table, and the child
table primary key (child_id
) also as a foreign key to the same item_id
column in the item
table. So now instead of having two different foreign keys ( referencing to child_id
and parent_id
) in my photos
table I only have one (referencing to item_id
):
CREATE TABLE `parent` (
`parent_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`parent_id`),
CONSTRAINT `parent_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `child` (
`child_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`child_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`child_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `item` (
`item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`item_id`))
CREATE TABLE `photo` (
`photo_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10)unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`photo_id`),
CONSTRAINT `photo_ibfk_1` FOREIGN KEY (`item_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I hope that's clear so far! So that works but now my problem is if I want to delete a parent
record: I would delete the corresponding item_id
record in the item
table which would automatically delete the corresponding record in the parent
table thanks to the foreign key, which then would automatically delete the records in the child
table belonging to that parent. All good except that I would be left with the records in the item
table corresponding to the child records that have just been deleted. The item
table would not know that some child records have been deleted unless the item_id
in the item
table was also a foreign key referencing the corresponding child_id
in the child
table. I've got the feeling my design is wrong? What would be the recommended design in that case?
Here are the real world parent (property
) and child (unit
) tables:
CREATE TABLE `property` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`companyID` int(11) unsigned NOT NULL,
`serviceTypeID` tinyint(3) unsigned DEFAULT NULL,
`name` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`postCode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`extraBed` tinyint(3) unsigned DEFAULT NULL,
`checkIn` tinyint(4) unsigned DEFAULT NULL,
`checkOut` tinyint(4) unsigned DEFAULT NULL,
`submissionDate` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `property_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_2` FOREIGN KEY (`companyID`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_3` FOREIGN KEY (`serviceTypeID`) REFERENCES `serviceType` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)
CREATE TABLE `unit` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`propertyID` int(11) unsigned NOT NULL,
`unitTypeID` tinyint(3) unsigned NOT NULL,
`title` varchar(50) NOT NULL,
`smallDescription` varchar(200) DEFAULT NULL,
`largeDescription` varchar(500) DEFAULT NULL,
`submissionDate` bigint(20) unsigned DEFAULT NULL
PRIMARY KEY (`id`),
CONSTRAINT `unit_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_2` FOREIGN KEY (`propertyID`) REFERENCES `property` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_3` FOREIGN KEY (`unitTypeID`) REFERENCES `unitType` (`id`) ON UPDATE CASCADE)
mysql database-design
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08
add a comment |
Let's say I have a parent table and a child table with parent_is as foreign key. Now I want to store photos that could either belong to parent or child. so I could have a parent_id and a child_id foreign keys in the photos table: when the photo belongs to parent then the child_id foreign key column would be null and vice-versa. But instead of that I decided to go for a different design: I now have a item
table and I get the parent
table primary key (parent_id
) to also be foreign key referencing the item_id
column in the item
table, and the child
table primary key (child_id
) also as a foreign key to the same item_id
column in the item
table. So now instead of having two different foreign keys ( referencing to child_id
and parent_id
) in my photos
table I only have one (referencing to item_id
):
CREATE TABLE `parent` (
`parent_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`parent_id`),
CONSTRAINT `parent_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `child` (
`child_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`child_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`child_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `item` (
`item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`item_id`))
CREATE TABLE `photo` (
`photo_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10)unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`photo_id`),
CONSTRAINT `photo_ibfk_1` FOREIGN KEY (`item_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I hope that's clear so far! So that works but now my problem is if I want to delete a parent
record: I would delete the corresponding item_id
record in the item
table which would automatically delete the corresponding record in the parent
table thanks to the foreign key, which then would automatically delete the records in the child
table belonging to that parent. All good except that I would be left with the records in the item
table corresponding to the child records that have just been deleted. The item
table would not know that some child records have been deleted unless the item_id
in the item
table was also a foreign key referencing the corresponding child_id
in the child
table. I've got the feeling my design is wrong? What would be the recommended design in that case?
Here are the real world parent (property
) and child (unit
) tables:
CREATE TABLE `property` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`companyID` int(11) unsigned NOT NULL,
`serviceTypeID` tinyint(3) unsigned DEFAULT NULL,
`name` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`postCode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`extraBed` tinyint(3) unsigned DEFAULT NULL,
`checkIn` tinyint(4) unsigned DEFAULT NULL,
`checkOut` tinyint(4) unsigned DEFAULT NULL,
`submissionDate` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `property_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_2` FOREIGN KEY (`companyID`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_3` FOREIGN KEY (`serviceTypeID`) REFERENCES `serviceType` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)
CREATE TABLE `unit` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`propertyID` int(11) unsigned NOT NULL,
`unitTypeID` tinyint(3) unsigned NOT NULL,
`title` varchar(50) NOT NULL,
`smallDescription` varchar(200) DEFAULT NULL,
`largeDescription` varchar(500) DEFAULT NULL,
`submissionDate` bigint(20) unsigned DEFAULT NULL
PRIMARY KEY (`id`),
CONSTRAINT `unit_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_2` FOREIGN KEY (`propertyID`) REFERENCES `property` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_3` FOREIGN KEY (`unitTypeID`) REFERENCES `unitType` (`id`) ON UPDATE CASCADE)
mysql database-design
Let's say I have a parent table and a child table with parent_is as foreign key. Now I want to store photos that could either belong to parent or child. so I could have a parent_id and a child_id foreign keys in the photos table: when the photo belongs to parent then the child_id foreign key column would be null and vice-versa. But instead of that I decided to go for a different design: I now have a item
table and I get the parent
table primary key (parent_id
) to also be foreign key referencing the item_id
column in the item
table, and the child
table primary key (child_id
) also as a foreign key to the same item_id
column in the item
table. So now instead of having two different foreign keys ( referencing to child_id
and parent_id
) in my photos
table I only have one (referencing to item_id
):
CREATE TABLE `parent` (
`parent_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`parent_id`),
CONSTRAINT `parent_ibfk_1` FOREIGN KEY (`parent_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `child` (
`child_id` int(10) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`child_id`),
CONSTRAINT `child_ibfk_1` FOREIGN KEY (`child_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
CREATE TABLE `item` (
`item_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`item_id`))
CREATE TABLE `photo` (
`photo_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`item_id` int(10)unsigned NOT NULL,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`photo_id`),
CONSTRAINT `photo_ibfk_1` FOREIGN KEY (`item_id`)
REFERENCES `item` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE)
I hope that's clear so far! So that works but now my problem is if I want to delete a parent
record: I would delete the corresponding item_id
record in the item
table which would automatically delete the corresponding record in the parent
table thanks to the foreign key, which then would automatically delete the records in the child
table belonging to that parent. All good except that I would be left with the records in the item
table corresponding to the child records that have just been deleted. The item
table would not know that some child records have been deleted unless the item_id
in the item
table was also a foreign key referencing the corresponding child_id
in the child
table. I've got the feeling my design is wrong? What would be the recommended design in that case?
Here are the real world parent (property
) and child (unit
) tables:
CREATE TABLE `property` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`companyID` int(11) unsigned NOT NULL,
`serviceTypeID` tinyint(3) unsigned DEFAULT NULL,
`name` varchar(50) NOT NULL,
`address` varchar(100) NOT NULL,
`postCode` varchar(20) NOT NULL,
`city` varchar(50) NOT NULL,
`extraBed` tinyint(3) unsigned DEFAULT NULL,
`checkIn` tinyint(4) unsigned DEFAULT NULL,
`checkOut` tinyint(4) unsigned DEFAULT NULL,
`submissionDate` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `property_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_2` FOREIGN KEY (`companyID`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `property_ibfk_3` FOREIGN KEY (`serviceTypeID`) REFERENCES `serviceType` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)
CREATE TABLE `unit` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`propertyID` int(11) unsigned NOT NULL,
`unitTypeID` tinyint(3) unsigned NOT NULL,
`title` varchar(50) NOT NULL,
`smallDescription` varchar(200) DEFAULT NULL,
`largeDescription` varchar(500) DEFAULT NULL,
`submissionDate` bigint(20) unsigned DEFAULT NULL
PRIMARY KEY (`id`),
CONSTRAINT `unit_ibfk_1` FOREIGN KEY (`id`) REFERENCES `item` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_2` FOREIGN KEY (`propertyID`) REFERENCES `property` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `unit_ibfk_3` FOREIGN KEY (`unitTypeID`) REFERENCES `unitType` (`id`) ON UPDATE CASCADE)
mysql database-design
mysql database-design
edited Nov 19 '16 at 14:23
bstenm
asked Nov 19 '16 at 13:05
bstenmbstenm
62
62
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 9 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08
add a comment |
1
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08
1
1
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08
add a comment |
3 Answers
3
active
oldest
votes
Question to answer:
- Are there data differences between child and parent?
- Can a child have child of its own?
- Why not store child and parent in the same table?
- Can a photo belong to a child AND parent?
Added based on answer
- Currently, a photo CAN belong to child a parent
Given the structure know i MAY look into 'Extension' table... Have one for parents, referenced by photo and have a Extension ie. parent_child_ext table containing data for child, reference to parent etc and have photo reference only the parent page then. This solution is based on whole table design and info about it :-/.
Is there any way you could post full info about parent/child columns?
ANSWER:
Depending on business requirements, i would probably go with two mapping tables to join photo and parent/child
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
add a comment |
- A photo has one photo_owner
- property and unit are both photo_owners
- When creating a property or unit, a record in photo_owner must first be created in order to use for the entity's photo_owner_id.
This design allows you to specify exactly which entities can own a photo, and which ones cannot. Do you want a "tree" entity to be able to own a photo? Add a photo_owner_id column to the table. It also avoids having an "item" table, which is too nebulous of an entity definition in my opinion. Additionaly, if there are properties that the owner of a photo must have, you can add them in one place (the photo_owner table) instead of having to add them to multiple tables.
add a comment |
Abandon FOREIGN KEYs
; they do not work for all situations. Do the cascading delete in the client -- or perhaps in a stored procedure.
Your parent
and child
have identical structure; that is usually a no-no in schema design. Your item
has essentially nothing in it; that seems strange. So... Have only one table. It has an id
and a parent_id
, which is enough to implement a 1:many (1 parent to many children) relationship.
Bottom Line:
- Think through whether you have a 1:many, or many:many relationship.
- Build the minimum number of tables required for that. (1 or 3). Do not think about FKs.
- Sketch out the
SELECTs
you will need. - Design the minimum number of indexes needed to make the queries efficient. (Keep in mind that 'composite' indexes are often useful.)
Now see if FKs can help you. Add the ones that work; write code where they don't.- Further refine the queries.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f155765%2fhow-to-design-parent-child-photos-relationships-in-mysql-when-photos-can-belon%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Question to answer:
- Are there data differences between child and parent?
- Can a child have child of its own?
- Why not store child and parent in the same table?
- Can a photo belong to a child AND parent?
Added based on answer
- Currently, a photo CAN belong to child a parent
Given the structure know i MAY look into 'Extension' table... Have one for parents, referenced by photo and have a Extension ie. parent_child_ext table containing data for child, reference to parent etc and have photo reference only the parent page then. This solution is based on whole table design and info about it :-/.
Is there any way you could post full info about parent/child columns?
ANSWER:
Depending on business requirements, i would probably go with two mapping tables to join photo and parent/child
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
add a comment |
Question to answer:
- Are there data differences between child and parent?
- Can a child have child of its own?
- Why not store child and parent in the same table?
- Can a photo belong to a child AND parent?
Added based on answer
- Currently, a photo CAN belong to child a parent
Given the structure know i MAY look into 'Extension' table... Have one for parents, referenced by photo and have a Extension ie. parent_child_ext table containing data for child, reference to parent etc and have photo reference only the parent page then. This solution is based on whole table design and info about it :-/.
Is there any way you could post full info about parent/child columns?
ANSWER:
Depending on business requirements, i would probably go with two mapping tables to join photo and parent/child
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
add a comment |
Question to answer:
- Are there data differences between child and parent?
- Can a child have child of its own?
- Why not store child and parent in the same table?
- Can a photo belong to a child AND parent?
Added based on answer
- Currently, a photo CAN belong to child a parent
Given the structure know i MAY look into 'Extension' table... Have one for parents, referenced by photo and have a Extension ie. parent_child_ext table containing data for child, reference to parent etc and have photo reference only the parent page then. This solution is based on whole table design and info about it :-/.
Is there any way you could post full info about parent/child columns?
ANSWER:
Depending on business requirements, i would probably go with two mapping tables to join photo and parent/child
Question to answer:
- Are there data differences between child and parent?
- Can a child have child of its own?
- Why not store child and parent in the same table?
- Can a photo belong to a child AND parent?
Added based on answer
- Currently, a photo CAN belong to child a parent
Given the structure know i MAY look into 'Extension' table... Have one for parents, referenced by photo and have a Extension ie. parent_child_ext table containing data for child, reference to parent etc and have photo reference only the parent page then. This solution is based on whole table design and info about it :-/.
Is there any way you could post full info about parent/child columns?
ANSWER:
Depending on business requirements, i would probably go with two mapping tables to join photo and parent/child
edited Nov 19 '16 at 15:27
answered Nov 19 '16 at 13:34
Vladislav ZalesakVladislav Zalesak
296111
296111
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
add a comment |
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Hi there, so yes there are data differences between child and parent, so they can not be in the same table, a child can not have a child of his own, and a photo can not belong to a child AND a parent, it's one or the other. Thanks!
– bstenm
Nov 19 '16 at 13:40
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Sure, I've updated my question with the two tables. Thanks!!
– bstenm
Nov 19 '16 at 14:23
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
Is the fact that a photo can belong to only parent OR child a 'HARD' requrement? As it is now, i can see some problems with AUTO INCREMENT PK in parent/child referencing the same item
– Vladislav Zalesak
Nov 19 '16 at 15:26
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
yes sorry there should not be auto increment on parent and child. forgot to remove that. could u elaborate a little on the 2 mapping tables? Cheers!!
– bstenm
Nov 19 '16 at 16:12
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
Have a table mapping photo (or item) to parent (two columns, item_id and parent_id) and another table mapping item to child(two columns, item_id and parent_id) In adition, Ok, i see that property means like house and unit is maybe room? if i am correct then the way i would do this is i would add a new unitTypeId of type 'overview' being an abstract unit and i would assign photos only to unit and photos of property would be assigned to this abstract unit
– Vladislav Zalesak
Nov 19 '16 at 16:59
add a comment |
- A photo has one photo_owner
- property and unit are both photo_owners
- When creating a property or unit, a record in photo_owner must first be created in order to use for the entity's photo_owner_id.
This design allows you to specify exactly which entities can own a photo, and which ones cannot. Do you want a "tree" entity to be able to own a photo? Add a photo_owner_id column to the table. It also avoids having an "item" table, which is too nebulous of an entity definition in my opinion. Additionaly, if there are properties that the owner of a photo must have, you can add them in one place (the photo_owner table) instead of having to add them to multiple tables.
add a comment |
- A photo has one photo_owner
- property and unit are both photo_owners
- When creating a property or unit, a record in photo_owner must first be created in order to use for the entity's photo_owner_id.
This design allows you to specify exactly which entities can own a photo, and which ones cannot. Do you want a "tree" entity to be able to own a photo? Add a photo_owner_id column to the table. It also avoids having an "item" table, which is too nebulous of an entity definition in my opinion. Additionaly, if there are properties that the owner of a photo must have, you can add them in one place (the photo_owner table) instead of having to add them to multiple tables.
add a comment |
- A photo has one photo_owner
- property and unit are both photo_owners
- When creating a property or unit, a record in photo_owner must first be created in order to use for the entity's photo_owner_id.
This design allows you to specify exactly which entities can own a photo, and which ones cannot. Do you want a "tree" entity to be able to own a photo? Add a photo_owner_id column to the table. It also avoids having an "item" table, which is too nebulous of an entity definition in my opinion. Additionaly, if there are properties that the owner of a photo must have, you can add them in one place (the photo_owner table) instead of having to add them to multiple tables.
- A photo has one photo_owner
- property and unit are both photo_owners
- When creating a property or unit, a record in photo_owner must first be created in order to use for the entity's photo_owner_id.
This design allows you to specify exactly which entities can own a photo, and which ones cannot. Do you want a "tree" entity to be able to own a photo? Add a photo_owner_id column to the table. It also avoids having an "item" table, which is too nebulous of an entity definition in my opinion. Additionaly, if there are properties that the owner of a photo must have, you can add them in one place (the photo_owner table) instead of having to add them to multiple tables.
answered Nov 23 '16 at 19:49
Joe BoryskoJoe Borysko
36127
36127
add a comment |
add a comment |
Abandon FOREIGN KEYs
; they do not work for all situations. Do the cascading delete in the client -- or perhaps in a stored procedure.
Your parent
and child
have identical structure; that is usually a no-no in schema design. Your item
has essentially nothing in it; that seems strange. So... Have only one table. It has an id
and a parent_id
, which is enough to implement a 1:many (1 parent to many children) relationship.
Bottom Line:
- Think through whether you have a 1:many, or many:many relationship.
- Build the minimum number of tables required for that. (1 or 3). Do not think about FKs.
- Sketch out the
SELECTs
you will need. - Design the minimum number of indexes needed to make the queries efficient. (Keep in mind that 'composite' indexes are often useful.)
Now see if FKs can help you. Add the ones that work; write code where they don't.- Further refine the queries.
add a comment |
Abandon FOREIGN KEYs
; they do not work for all situations. Do the cascading delete in the client -- or perhaps in a stored procedure.
Your parent
and child
have identical structure; that is usually a no-no in schema design. Your item
has essentially nothing in it; that seems strange. So... Have only one table. It has an id
and a parent_id
, which is enough to implement a 1:many (1 parent to many children) relationship.
Bottom Line:
- Think through whether you have a 1:many, or many:many relationship.
- Build the minimum number of tables required for that. (1 or 3). Do not think about FKs.
- Sketch out the
SELECTs
you will need. - Design the minimum number of indexes needed to make the queries efficient. (Keep in mind that 'composite' indexes are often useful.)
Now see if FKs can help you. Add the ones that work; write code where they don't.- Further refine the queries.
add a comment |
Abandon FOREIGN KEYs
; they do not work for all situations. Do the cascading delete in the client -- or perhaps in a stored procedure.
Your parent
and child
have identical structure; that is usually a no-no in schema design. Your item
has essentially nothing in it; that seems strange. So... Have only one table. It has an id
and a parent_id
, which is enough to implement a 1:many (1 parent to many children) relationship.
Bottom Line:
- Think through whether you have a 1:many, or many:many relationship.
- Build the minimum number of tables required for that. (1 or 3). Do not think about FKs.
- Sketch out the
SELECTs
you will need. - Design the minimum number of indexes needed to make the queries efficient. (Keep in mind that 'composite' indexes are often useful.)
Now see if FKs can help you. Add the ones that work; write code where they don't.- Further refine the queries.
Abandon FOREIGN KEYs
; they do not work for all situations. Do the cascading delete in the client -- or perhaps in a stored procedure.
Your parent
and child
have identical structure; that is usually a no-no in schema design. Your item
has essentially nothing in it; that seems strange. So... Have only one table. It has an id
and a parent_id
, which is enough to implement a 1:many (1 parent to many children) relationship.
Bottom Line:
- Think through whether you have a 1:many, or many:many relationship.
- Build the minimum number of tables required for that. (1 or 3). Do not think about FKs.
- Sketch out the
SELECTs
you will need. - Design the minimum number of indexes needed to make the queries efficient. (Keep in mind that 'composite' indexes are often useful.)
Now see if FKs can help you. Add the ones that work; write code where they don't.- Further refine the queries.
answered Nov 19 '16 at 16:26
Rick JamesRick James
43.8k22360
43.8k22360
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f155765%2fhow-to-design-parent-child-photos-relationships-in-mysql-when-photos-can-belon%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What do parent and child represent? Are these genuine parents (as in mother_of)? Is the relationship recursive, i.e. can a child also be a parent?
– Lennart
Jun 24 '18 at 10:08