banana_man — 2012-11-28T09:24:37-05:00 — #1
I am using MySQL workbench for the first time. I have created an InnoDB relational database. When i connect two tables with a many to one connection it automatically adds a new row in the many table that references the row_id in the other table. When it adds this new row it inserts it as a Primary row. So now i have a table with 2 primary rows. Is this how things should be setup? I though every table should only have one primary row? Should i set the newly added relational row so that it's not a primary row?
On a side note. I am using phpMyAdmin through MAMP. I have read some tutorials about connecting MySQL workbench to MAMP's phpMyAdmin. In the tutorials it states that i should configure MySQL workbench to use the file located at /Applications/MAMP/tmp/mysql/mysql.sock but when i go to this location their is no file with this name (http://phpprotip.com/2011/10/using-mysql-workbench-with-mamp/). Anyone know where i can find this file?
r937 — 2012-11-28T12:21:50-05:00 — #2
2 primary rows??? perhaps you meant column?
please do a SHOW CREATE TABLE for each table, so we can see what the workbench created
banana_man — 2012-11-28T13:55:54-05:00 — #3
Your right about the columns. Here's an example of a table create statement. It is declaring 3 columns as being primary (
CREATE TABLE IF NOT EXISTS
row_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
mytable_farmers_row_id INT(11) UNSIGNED NOT NULL ,
mytable_farmers_mytable_users_row_id INT(11) UNSIGNED NOT NULL ,
open_day_title VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
open_day_description TEXT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
open_day_start_date DATE NOT NULL ,
open_day_start_time TIME NOT NULL ,
open_day_finish_time TIME NOT NULL ,
PRIMARY KEY (
mytable_farmers_mytable_users_row_id ASC) ,
FOREIGN KEY (
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 85
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
r937 — 2012-11-28T14:04:25-05:00 — #4
looks broken to me
in mysql, an auto_increment must be (part of) the primary -- but it doesn't make sense to have other columns included as well
perhaps there is something about how you're relating tables that makes workbench do this (i dunno, i'm not a workbench user)
hopefully there's an easy way to fix it
felgall — 2012-11-28T15:43:12-05:00 — #5
Looks like whoever created that product believes in generating unnecessary autoincrement keys for everything. The second and third fields in that key should be all that is actually required. It is quite common for a table that has a many to one relationship to another table to require one more column in the primary key than the table it relates to.
kylewolfe — 2012-11-28T15:56:24-05:00 — #6
I can see using an AI ID, but not using that AI as part of the primary key.
PRIMARY KEY (`mytable_farmers_row_id`, `mytable_farmers_mytable_users_row_id`) ,
And leaving the AI row along makes sense to me to restrict 1 of each... what ever that is... and then be able to use row_id as the row pointer, though not completely necessary..
I agree withe everyone else though, this is just broke, starting with the naming convention.
An example of why you might use a non primary key'd AI:
user_id AI, PK
This would ensure only one role per user is allowed in, and I could perhaps use the role_id to delete certain rows, but should really be handled though dual criteria on the PKs.
felgall — 2012-11-28T16:29:38-05:00 — #7
[QUOTE=K. Wolfe;5251726]This would ensure only one role per user is allowed in/QUOTE]
If only one role per user is allowed then it would make more sense for the role to be a field in the user table and not a separate table.
If you properly normalize your data you shouldn't end up with any one to one tables - of course if it were a huge field and the relationship were one to zero or one then it would be a different matter but even if only a small percentage have a role(assuming a role can fit in a VARCHAR) then having a field for it in the user table would be reasonable.
r937 — 2012-11-28T16:51:01-05:00 — #8
normalization rules do not, if you will excuse the pun, rule out one-to-one tables
you might find one-to-one tables unattractive for whatever reasons, but they do conform to normalization
and by the way, there really is no such thing as strictly one-to-one, it's always one-to-zero-or-one
kylewolfe — 2012-11-29T09:57:48-05:00 — #9
That was also a one to many example provided. Each user may have more than one UNIQUE role.
banana_man — 2012-11-29T10:20:30-05:00 — #10
Thanks for the input! I think i need to start from scratch and first do a bit of reading up on MySQL relational database naming conventions.
r937 — 2012-11-29T11:53:05-05:00 — #11
you will find conflicting conventions, some making more sense than others
felgall — 2012-11-29T16:25:11-05:00 — #12
If you had read my entire post you would have seen that the next paragraph covered one-to-zero-or-one as being a different situation to strictly one-to-one.
Perhaps the official normalization rules don't rule out strict one-to-one but if they don't then it is because the rule is too obvious to need stating - otherwise you could legitimately have lots of tableswith only one field that isn't a part of the primary key where each field apart from the primary key has its own table that associateds that field with the key - you'd then need to join all the tables together in order to extract all the one to one data.
Strict one-to-one relationships should involve placing the fields in the same table regardless of whether or not the written normalization rules require it as not doing so would rarely provide any benefit.
As I said before - with one-to-zero-or-one it would depend on how often the field has a value and how big the field needs to be as to whether or not the same table should be used - if the normalization rules don't specify one way or the other then the conversion of the logical design to a physical design should take care of it.
Also there is such a thing as strict one-to-one - for example if you have a width field and a height field then they will have a strict one-to-one relationship where there is nothing that can have a width that does not also have a height. Setting up two tables with the width in one and the height in the other would be silly.
kylewolfe — 2012-11-29T16:36:45-05:00 — #13
Tell that to a financial organization that stores literally thousands of 1-1 relational columns on customers. I would not be a happy camper.
r937 — 2012-11-29T17:24:22-05:00 — #14
what i meant was, you cannot actually implement a strict one-to-one relationship in tables
declare the foreign keys to relate them, and all you will accomplish is that the child must have a parent (one-to-zero-or-one)
there's nothing in PK/FK syntax that ~requires~ a parent to have a child
go ahead, test it for yourself
as for that other stuff you wrote, i can't seem to digest your overly long sentences
system — 2014-10-07T23:18:38-04:00 — #15
This topic is now closed. New replies are no longer allowed.