I'm making a recipe database, and need to have DB fields in mySQL that
will have lists of values that would get sent to an array to be worked
on.
I anticipate that at times a comma will need to be used in the value
itself. So, what's a commonly used symbol or something that people tend
to use as a separator of a list that would then be used in the split()
function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any
suggestions?
Thanks!
Liam 15 1626 ne**@celticbear.com wrote: I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Thanks! Liam
Usually the vertical bar '|' is a good choice.
-david- ne**@celticbear.com wrote: I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Thanks! Liam
I usually use an ASCII control character. 0x1E is designated as a
record seperator. Don't know if MySQL would accept it or not.
Chung Leong wrote: ne**@celticbear.com wrote: I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Thanks! Liam
I usually use an ASCII control character. 0x1E is designated as a record seperator. Don't know if MySQL would accept it or not.
Interesting.
I had to do a search on control characters and that 0x1e, because I
have no idea what they are.
(Well, except in Windows I know I can use ALT+(numpad) to write ASCII
characters, but that's beside the point.
Evidently 0x1e is shown as two carrets: ^^
When I create the full value that would go into the field, would I
simply insert ^^ between the different values before it gets sent to
mySQL, and then use ^^ as the split() seperator?
Or do I have to use that 0x1e label in some way?
Does the ^^ tell mySQL or PHP something special?
I can't seem to find anything with Google on it.
Thanks for the reply!
-Liam ne**@celticbear.com wrote: need to have DB fields in mySQL that will have lists of values
Typically, one would use escape characters. For example, | would be
used to seperate two fields. A literal | would be escaped: \|. This
makes it possible to use the seperation character in the data.
Another, probably better option, would be to make a seperate table.
Instead of storing the ingredients seperated by a character like this:
1, Chinese Chicken, chicken|salt|pepper|mushrooms
Think about making a ingredients table and storing it there:
[Recipes]
1, Chinese Chicken
2, Meatloaf
[Ingredients]
1, chicken
1, salt
1, pepper
1, mushrooms
2, meat
2, loaf
This makes it easy to search and it is more logical from a DB point of
view. ne**@celticbear.com wrote: Chung Leong wrote: ne**@celticbear.com wrote: I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Thanks! Liam
I usually use an ASCII control character. 0x1E is designated as a record seperator. Don't know if MySQL would accept it or not.
Interesting. I had to do a search on control characters and that 0x1e, because I have no idea what they are. (Well, except in Windows I know I can use ALT+(numpad) to write ASCII characters, but that's beside the point.
Evidently 0x1e is shown as two carrets: ^^ When I create the full value that would go into the field, would I simply insert ^^ between the different values before it gets sent to mySQL, and then use ^^ as the split() seperator? Or do I have to use that 0x1e label in some way?
Does the ^^ tell mySQL or PHP something special? I can't seem to find anything with Google on it.
Do something like implode("\x1B", $record);. The character is
definitely not two carets.
Sjoerd wrote: ne**@celticbear.com wrote: need to have DB fields in mySQL that will have lists of values
Typically, one would use escape characters. For example, | would be used to seperate two fields. A literal | would be escaped: \|. This makes it possible to use the seperation character in the data.
Another, probably better option, would be to make a seperate table. Instead of storing the ingredients seperated by a character like this: 1, Chinese Chicken, chicken|salt|pepper|mushrooms Think about making a ingredients table and storing it there:
[Recipes] 1, Chinese Chicken 2, Meatloaf
[Ingredients] 1, chicken 1, salt 1, pepper 1, mushrooms 2, meat 2, loaf
This makes it easy to search and it is more logical from a DB point of view.
Well, since you brought it up, speaking of the DB, that's kind of what
I had in mind, but, differemt.
The tbl_ingredients would have two columns, ID and NAME like:
1, garlic
2, chicken
3, pepper
4, salt
etc...
Then the tbl_recipes would have colums like:
ID, NAME, INGREDIENTS, MEASUREMENTS, NOTES
with data like:
1, Chinese Chicken, 1|2|3|4, 1 clove|1 lbs. breast| 1 tsp.| 1 T., more
stuff here....
Where the ingredients would be the ingredient's ID in a CSV, (but
obviously using something other than a comma,).
That's the best I could come up with to be able to have a list of
unknown and constantly changing number of ingredients that would save
space in the table.
The problem I'm seeing with your suggestion is that the [ingredients]
table would have a lot of redundant info.
Every recipe that required pepper would have a row with a recipie ID
and a value of "garlic," which would be repeated in the database n
number of times.
I suppose of course, if the database remains small, that amount of
redundant data isn't going to hurt performance at all.... and would
possibly be easier to manage....
Thanks for the reply and the feedback!!
-Liam ne**@celticbear.com wrote: I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Thanks! Liam
Liam,
This violates first normal form (more than one value in a row/column).
Rather, create a second table containing the recipe id and a single value, then
add each of the values to this second table.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp. js*******@attglobal.net
================== ne**@celticbear.com wrote: Sjoerd wrote: ne**@celticbear.com wrote: need to have DB fields in mySQL that will have lists of values Typically, one would use escape characters. For example, | would be used to seperate two fields. A literal | would be escaped: \|. This makes it possible to use the seperation character in the data.
Another, probably better option, would be to make a seperate table. Instead of storing the ingredients seperated by a character like this: 1, Chinese Chicken, chicken|salt|pepper|mushrooms Think about making a ingredients table and storing it there:
[Recipes] 1, Chinese Chicken 2, Meatloaf
[Ingredients] 1, chicken 1, salt 1, pepper 1, mushrooms 2, meat 2, loaf
This makes it easy to search and it is more logical from a DB point of view.
Well, since you brought it up, speaking of the DB, that's kind of what I had in mind, but, differemt. The tbl_ingredients would have two columns, ID and NAME like: 1, garlic 2, chicken 3, pepper 4, salt etc...
Then the tbl_recipes would have colums like: ID, NAME, INGREDIENTS, MEASUREMENTS, NOTES with data like: 1, Chinese Chicken, 1|2|3|4, 1 clove|1 lbs. breast| 1 tsp.| 1 T., more stuff here....
Where the ingredients would be the ingredient's ID in a CSV, (but obviously using something other than a comma,). That's the best I could come up with to be able to have a list of unknown and constantly changing number of ingredients that would save space in the table.
The problem I'm seeing with your suggestion is that the [ingredients] table would have a lot of redundant info. Every recipe that required pepper would have a row with a recipie ID and a value of "garlic," which would be repeated in the database n number of times.
I suppose of course, if the database remains small, that amount of redundant data isn't going to hurt performance at all.... and would possibly be easier to manage....
Thanks for the reply and the feedback!! -Liam
How about this instead?
Three tables: Recipe, Quantity, Ingredient
Recipe
ID, NAME, DESCRIPTION, AUTHOR, ...
Ingredient
ID, DESCRIPTION
Quantity
RECIPE_ID, INGREDIENT_ID, AMOUNT, UNIT
That way you could have:
Recipe:
1, 'Chinese Chicken', 'A neat recipe that is easy to make', ...
Ingredient:
1, 'Garlic'
2, 'Chicken'
3, 'Salt'
4, 'Pepper'
Quantity:
1, 1, 1, 'clove'
1, 2, 1, 'lb white meat'
1, 3, 1, 'tsp'
1, 4, 1, 'tbsp'
Then, let's say you have another recipe that calls for salt.
Recipe:
2, 'Mashed Potatoes', 'Light and fluffy mashed potatoes'
Ingredient:
5, 'Potatoes'
6, 'Buttermilk'
Quantity:
2, 3, 1, 'tbsp'
2, 5, 2, 'lbs cubed'
2, 6, .25, 'cup'
2, 4, 0, 'to taste'
etc.
-david-
David Haynes wrote: ne**@celticbear.com wrote: Sjoerd wrote: ne**@celticbear.com wrote: need to have DB fields in mySQL that will have lists of values Typically, one would use escape characters. For example, | would be used to seperate two fields. A literal | would be escaped: \|. This makes it possible to use the seperation character in the data.
Another, probably better option, would be to make a seperate table. Instead of storing the ingredients seperated by a character like this: 1, Chinese Chicken, chicken|salt|pepper|mushrooms Think about making a ingredients table and storing it there:
[Recipes] 1, Chinese Chicken 2, Meatloaf
[Ingredients] 1, chicken 1, salt 1, pepper 1, mushrooms 2, meat 2, loaf
This makes it easy to search and it is more logical from a DB point of view.
Well, since you brought it up, speaking of the DB, that's kind of what I had in mind, but, differemt. The tbl_ingredients would have two columns, ID and NAME like: 1, garlic 2, chicken 3, pepper 4, salt etc...
Then the tbl_recipes would have colums like: ID, NAME, INGREDIENTS, MEASUREMENTS, NOTES with data like: 1, Chinese Chicken, 1|2|3|4, 1 clove|1 lbs. breast| 1 tsp.| 1 T., more stuff here....
Where the ingredients would be the ingredient's ID in a CSV, (but obviously using something other than a comma,). That's the best I could come up with to be able to have a list of unknown and constantly changing number of ingredients that would save space in the table.
The problem I'm seeing with your suggestion is that the [ingredients] table would have a lot of redundant info. Every recipe that required pepper would have a row with a recipie ID and a value of "garlic," which would be repeated in the database n number of times.
I suppose of course, if the database remains small, that amount of redundant data isn't going to hurt performance at all.... and would possibly be easier to manage....
Thanks for the reply and the feedback!! -Liam
How about this instead?
Three tables: Recipe, Quantity, Ingredient
Recipe ID, NAME, DESCRIPTION, AUTHOR, ...
Ingredient ID, DESCRIPTION
Quantity RECIPE_ID, INGREDIENT_ID, AMOUNT, UNIT
That way you could have:
[..] -david-
Huh, OK. I hadn't thought of it that way. Intuitively it wouldn't seem
like you'd want to have the unique data that makes up the components of
the recipe in a "quantity" table as opposed to the table that contains
the recipe name and details, etc.
That erroneous thinking is what led me to the silliness of trying to
shove multiple data values into one field.
This solves (makes irrelevant) my array/CSV problem.
Thanks for the clue-by-four!
-Liam ne**@celticbear.com wrote: David Haynes wrote: ne**@celticbear.com wrote: Sjoerd wrote: ne**@celticbear.com wrote: > need to have DB fields in mySQL that > will have lists of values Typically, one would use escape characters. For example, | would be used to seperate two fields. A literal | would be escaped: \|. This makes it possible to use the seperation character in the data.
Another, probably better option, would be to make a seperate table. Instead of storing the ingredients seperated by a character like this: 1, Chinese Chicken, chicken|salt|pepper|mushrooms Think about making a ingredients table and storing it there:
[Recipes] 1, Chinese Chicken 2, Meatloaf
[Ingredients] 1, chicken 1, salt 1, pepper 1, mushrooms 2, meat 2, loaf
This makes it easy to search and it is more logical from a DB point of view. Well, since you brought it up, speaking of the DB, that's kind of what I had in mind, but, differemt. The tbl_ingredients would have two columns, ID and NAME like: 1, garlic 2, chicken 3, pepper 4, salt etc...
Then the tbl_recipes would have colums like: ID, NAME, INGREDIENTS, MEASUREMENTS, NOTES with data like: 1, Chinese Chicken, 1|2|3|4, 1 clove|1 lbs. breast| 1 tsp.| 1 T., more stuff here....
Where the ingredients would be the ingredient's ID in a CSV, (but obviously using something other than a comma,). That's the best I could come up with to be able to have a list of unknown and constantly changing number of ingredients that would save space in the table.
The problem I'm seeing with your suggestion is that the [ingredients] table would have a lot of redundant info. Every recipe that required pepper would have a row with a recipie ID and a value of "garlic," which would be repeated in the database n number of times.
I suppose of course, if the database remains small, that amount of redundant data isn't going to hurt performance at all.... and would possibly be easier to manage....
Thanks for the reply and the feedback!! -Liam How about this instead?
Three tables: Recipe, Quantity, Ingredient
Recipe ID, NAME, DESCRIPTION, AUTHOR, ...
Ingredient ID, DESCRIPTION
Quantity RECIPE_ID, INGREDIENT_ID, AMOUNT, UNIT
That way you could have:
[..] -david-
Huh, OK. I hadn't thought of it that way. Intuitively it wouldn't seem like you'd want to have the unique data that makes up the components of the recipe in a "quantity" table as opposed to the table that contains the recipe name and details, etc. That erroneous thinking is what led me to the silliness of trying to shove multiple data values into one field. This solves (makes irrelevant) my array/CSV problem. Thanks for the clue-by-four! -Liam
Glad to help.
The 'Quantity' table is better known as a 'many-to-many' join table and
is used quite a lot in database schema design. It is called
'many-to-many' since a recipe may use many ingredients and an ingredient
may be used in many recipes.
-david-
>I'm making a recipe database, and need to have DB fields in mySQL that will have lists of values that would get sent to an array to be worked on.
If you're putting a list of values in a single field, that represents
a one-to-many relationship, and calls for use of another table.
For example, a table called Ingredients which contains the
ingredient, quantity, and ID of the recipe to which it belongs.
I anticipate that at times a comma will need to be used in the value itself. So, what's a commonly used symbol or something that people tend to use as a separator of a list that would then be used in the split() function to send the elements into an array?
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
Use another table.
Gordon L. Burditt
On Mon, 17 Apr 2006 12:09:24 -0700, ne**@celticbear.com wrote: Then the tbl_recipes would have colums like: ID, NAME, INGREDIENTS, MEASUREMENTS, NOTES with data like: 1, Chinese Chicken, 1|2|3|4, 1 clove|1 lbs. breast| 1 tsp.| 1 T., more stuff here....
You *could* do that but typically a reational database will use *another*
table to "link" the one-to-many relationship together.
table 1: ingredients
table 2: recipe
table 3: recipe-to-ingredients-linker
Table 1 and 2 would be as you described but table 3 would be (roughly) as
follows:
create table recipe_ingredients (
id int()
recipe_id int()
ingredient_id int()
... additional_columns ...
)
You then would join all three tables using recipe_id to join recipes to
the linker and then using ingredient_id to join to the ingredients.
This way, you can have an unlimited number of ingredients.
It is clunky from a *human* point of view but fast, flexible, scalable,
etc. from a machine point of view.
(The additional_columns could be things like "quantity" or other modifiers)
later...
--
JDS | je*****@example.invalid
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/
Yeah I've used triple-colons in the past :::
for me it doesn't hurt my eyes to see that. :-)
--------
I typically lurk here as I keep trying to get my hands dirty w/ both
PHP & MySQL - but one issue I have - a road block really - is the setup
of a database and how it should be broken up, etc... There is some
nice insight here.
Thanks,
Scott
JDS wrote: On Mon, 17 Apr 2006 08:12:58 -0700, ne**@celticbear.com wrote:
Nearly every punctuation has a chance of being in the actual data. Any suggestions?
You can use a multi-character separator.
@@@
or @!@
or
THIS_IS_THE_SEPARATOR
IMO (and in my experience), relying on the assumption that certain
combinations characters won't appear in the actual data is just an
accident waiting to happen (I've had disastrous experiences based on
that assumption). Admittedly, @@@ or whatever is extremely unlikely to
appear in an ingredients list, but nevertheless...
If one must violate 1NF like this, I would recommend using a single
delimiter, such as a comma. If a literal comma ever occurs in the
actual data, it should be delimited by some means, e.g.:
Eye of toad
Leg, arm of newt
Brain of goat
becomes:
Eye of toad, Leg\, arm of newt, brain of goat
Separating the data on a read will become slightly more complicated
than a simple explode(), but it will be guaranteed to work in all
situations (I think).
But having said all this, by far the best option is not violating 1NF
in the first place, by storing this information atomically in a
separate table.
--
Oli This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: dixie |
last post by:
I have some code that adds new records into a table for each ID in a list
box when a button on a form is clicked. This works fine. My problem now is
that I wish to be able to edit all the records...
|
by: Jesper Denmark |
last post by:
Hi,
I use the double.Parse functionality to convert a number
in a text file into a double. However, while this works
fine on one computer it doesn't on another. I've found
out that it is...
|
by: Drum2001 |
last post by:
I have a database where I need to query multiple items. Is it possible
to run a query based on a textbox where the information is delimited by
a comma.
Example:
Show me all names where...
|
by: Peter Larsen [] |
last post by:
Hi,
How do i convert a string to float/double if the decimal-seperator doesn't
follow the local language settings ??
Eg.
string s = load a value from a file - "1234,12";
float f =...
|
by: lstuyck73 |
last post by:
Hi, I'm using a menu control that uses a SiteMapDatasource to display
menu items in a horizontal menu. I want a seperator between each menu
item. If I use the StaticBottomSeperatorImageUrl...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |