| | |
Varchar field as foreign reference
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Solved Threads: 0
Ok, i have a database where i'm storing information on plants and in one of the tables is a plants table:
here's the first few fields in the table plants:
plant_id is the pk, family_id, genus_id, species_id are foreign keys.
soon i will change it to
where family_name, genus_name, species_name are foreign references (the name fields are all varchar(50)) is this a good idea tho or should i stick to the ids?
the reason i might change it is there are a little over 2300 records i have to add from an excel file which just uses names id's so i think this would make it easier/faster. What i was wondering though is if the index length of varchar fields might restrict searches on any names at all. So what i'm wondering is:
1) better to use a numerical id/code as foreign key?
2) what is the average/default index length on varchar fields and
3) say for example a user searched for "odoratissimus" for species_name and the index length were say 10, what would MySQL do in this case?
PS:
If this question doesnt seem to make sense let me know and try to elaborate/explain further since i'm not sure i may have gotten my question across clearly
Thanks in advance...
here's the first few fields in the table plants:
MySQL Syntax (Toggle Plain Text)
plant_id, family_id, genus_id, species_id, auth1, auth2, ...
soon i will change it to
MySQL Syntax (Toggle Plain Text)
plant_id, family_name, genus_name, species_name, auth1, auth2, ...
the reason i might change it is there are a little over 2300 records i have to add from an excel file which just uses names id's so i think this would make it easier/faster. What i was wondering though is if the index length of varchar fields might restrict searches on any names at all. So what i'm wondering is:
1) better to use a numerical id/code as foreign key?
2) what is the average/default index length on varchar fields and
3) say for example a user searched for "odoratissimus" for species_name and the index length were say 10, what would MySQL do in this case?
PS:
If this question doesnt seem to make sense let me know and try to elaborate/explain further since i'm not sure i may have gotten my question across clearly
Thanks in advance...
•
•
Join Date: Sep 2007
Posts: 176
Reputation:
Solved Threads: 20
•
•
•
•
Ok, i have a database where i'm storing information on plants and in one of the tables is a plants table:
here's the first few fields in the table plants:
plant_id is the pk, family_id, genus_id, species_id are foreign keys.MySQL Syntax (Toggle Plain Text)
plant_id, family_id, genus_id, species_id, auth1, auth2, ...
soon i will change it to
where family_name, genus_name, species_name are foreign references (the name fields are all varchar(50)) is this a good idea tho or should i stick to the ids?MySQL Syntax (Toggle Plain Text)
plant_id, family_name, genus_name, species_name, auth1, auth2, ...
the reason i might change it is there are a little over 2300 records i have to add from an excel file which just uses names id's so i think this would make it easier/faster. What i was wondering though is if the index length of varchar fields might restrict searches on any names at all. So what i'm wondering is:
1) better to use a numerical id/code as foreign key?
2) what is the average/default index length on varchar fields and
3) say for example a user searched for "odoratissimus" for species_name and the index length were say 10, what would MySQL do in this case?
PS:
If this question doesnt seem to make sense let me know and try to elaborate/explain further since i'm not sure i may have gotten my question across clearly
Thanks in advance...
In other words, I would stick with using an ID instead of a character-based key. But in your case I don't think it would matter either way.
Your other questions are likely answered in the docs.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
"Others make web sites. We make web sites work!"
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Solved Threads: 0
hey thanks
i have another quick question tho, in this same table not every entry will have an "infraspecies_id" or "syn_genus_id" (two fields from the table) and i want to set them as NULL in these cases but MySQL won't let me saying it violates the foreign key relationship likely because in the "infraspecies" table the ID is set as NOT NULL, is it not possible to do this and how if possible can i correct this/get around this?
thanks
i have another quick question tho, in this same table not every entry will have an "infraspecies_id" or "syn_genus_id" (two fields from the table) and i want to set them as NULL in these cases but MySQL won't let me saying it violates the foreign key relationship likely because in the "infraspecies" table the ID is set as NOT NULL, is it not possible to do this and how if possible can i correct this/get around this?
thanks
•
•
Join Date: Sep 2007
Posts: 176
Reputation:
Solved Threads: 20
•
•
•
•
hey thanks
i have another quick question tho, in this same table not every entry will have an "infraspecies_id" or "syn_genus_id" (two fields from the table) and i want to set them as NULL in these cases but MySQL won't let me saying it violates the foreign key relationship likely because in the "infraspecies" table the ID is set as NOT NULL, is it not possible to do this and how if possible can i correct this/get around this?
thanks
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
"Others make web sites. We make web sites work!"
•
•
Join Date: Aug 2004
Posts: 24
Reputation:
Solved Threads: 0
ok i got the data loaded. I was using phpmyadmin's import feature and i was selecting the 'CSV' radio button but i changed it to 'CSV using LOAD DATA' radio button and that worked. I figured something might have been up with the importing because i inserted a single record and left some foreign key fields blank (NULL) and it inserted successfully so my database design should not have been a problem i think seems something was bugging out during the import.
but thanks anyways hope this might help someone having similar problems
but thanks anyways hope this might help someone having similar problems
![]() |
Similar Threads
- Searching a Text Field in a JList (Java)
- unserialize an array! spitting out jibberish (PHP)
- mysql date ALWAYS = 0000-00-00 00:00:00 (PHP)
- <script language=\"javascript\"> Display problem with PHP & MYSQL (PHP)
- Nesting A Table (C#)
- Display field as link (PHP)
- figuring out PK/FK (MS SQL)
Other Threads in the MySQL Forum
- Previous Thread: PHP/MySQL - Need help figuring out if this will work
- Next Thread: how to point the path to IMAGE folder for MYSQL PHP database
| Thread Tools | Search this Thread |
Tag cloud for MySQL
"use" 1 agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright court crm data database design developer development distinct dui eliminate enter enterprise error eudora facebook form foss gartner gnu government gpl greenit groupware hiring hyperic images innerjoins insert ip joebrockmeier join keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron microsoft microsoftexchange mindtouch multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opengovernment opensource operand oracle penelope php priceupdating query referencedesign reorderingcolumns saas search select sharepoint simpledb spotify statement sugarcrm syntax techsupport thunderbird transparency update virtualization





