Ive made my first database in MySQL,
ive learnt how to create tables, some of the guidance around primary keys (mainly, if its not under your control, dont use a string), loading in CSVs etc
ive come to a problem that i am unsure of as to how to proceed.
apologies in advance if i have written things in the following that are not conventional
lets say i have 2 tables tbl_a and tbl_b
tbl_a is top level, and each record links to many in tbl_b
i have my tables each with a primary key (PK as an auto increment integer)
tbl_a has a PK a_id
but it also has a unique index top_id (i didnt make this a PK as it is possible it could change)
tbl_b has a PK b_id
it also has a unique index bottom_id (i didnt make this a PK for same reason as above)
and has top_id (not a unique index as the link is one to many from tbl_a)
what it doesnt have is a copy of a_id to create what i gather would be a typical PK-FK link
in this situation do i,,,
create a link from top_id to top_id between the two tables when neither are keys
somehow create a field for a_id in tbl_b and then have a_id in tbl_b as a FK - i expect this is the correct procedure?
Apologies if this doesnt make sense!
Edit,
i think i am right in needing this reference as every row in a tbl_b MUST refer to a SINGLE row in tbl_a
also i did toy with the idea of making top_id in tbl_a a PK and top_id in tbl_b an FK, this would have made this problem trivial, but as said, top_id isnt set in stone, but will always be unique ..but this seesm to be a bad idea, hence not doing it
ive learnt how to create tables, some of the guidance around primary keys (mainly, if its not under your control, dont use a string), loading in CSVs etc
ive come to a problem that i am unsure of as to how to proceed.
apologies in advance if i have written things in the following that are not conventional
lets say i have 2 tables tbl_a and tbl_b
tbl_a is top level, and each record links to many in tbl_b
i have my tables each with a primary key (PK as an auto increment integer)
tbl_a has a PK a_id
but it also has a unique index top_id (i didnt make this a PK as it is possible it could change)
tbl_b has a PK b_id
it also has a unique index bottom_id (i didnt make this a PK for same reason as above)
and has top_id (not a unique index as the link is one to many from tbl_a)
what it doesnt have is a copy of a_id to create what i gather would be a typical PK-FK link
in this situation do i,,,
create a link from top_id to top_id between the two tables when neither are keys
somehow create a field for a_id in tbl_b and then have a_id in tbl_b as a FK - i expect this is the correct procedure?
Apologies if this doesnt make sense!
Edit,
i think i am right in needing this reference as every row in a tbl_b MUST refer to a SINGLE row in tbl_a
also i did toy with the idea of making top_id in tbl_a a PK and top_id in tbl_b an FK, this would have made this problem trivial, but as said, top_id isnt set in stone, but will always be unique ..but this seesm to be a bad idea, hence not doing it
Last edited: