I am building a table that will receive different values in a field, think of a log table that the "value" field can be a number, a tiny string or a big text etc.
So I was wonderig if I should create that "value" field as Text or create two fields, one for small inputs, like date, numbers and some string and another one only for the Texts inputs.
so, my question is this:
Should this "value" field be a Varchar along with some other "value2" as Text or create one field Text that the mysql will manage this corretcly?
I am afraid that creating only one Text field can be a bad thing for performance.
EDIT: the number, datetime etc are going to be cast as string before insertion, thats not the point
Thanks,
Joe
Do you know how large the largest input will be? If you impose a limit, or know how large the maximum input will be, then you could use varchar (which caps at 255 characters in versions < 5.0.3, and 65,535 in versions >= 5.0.3). Otherwise, you're probably better off with Text, since it holds significantly more (65,535*2^16-1).
As an alternative, if users are creating things that already have tables (such as adding events to a calendar), you could just put an "is_approved" column on the table and only display approved ones, or search through everything to check for duplicates.
http://dev.mysql.com/doc/refman/5.0/en/string-type-overview.html
If there is a limit to the length of that the data stored use varchar() (as MySQL 5.0.3 varchar max length can be up to 65,535)
If there is no concrete limit then use a 'text' field type.
The varchar field can handle the different input's that you are mentioning but as a string, not as an integer or datetime.
Related
I have a content management system in which a user can dynamically create an html form. I have all the tables setup for that. But there's one thing that troubles me and that is, the text size for the entered values.
Some forms have textareas and some textareas contain quite an amount of data. But there are also just simple textfields on the form, which contain just a few characters of data.
My database table for saving the values kind of looks like this:
form_values
id | form_id | fk_element_id | value
So in this case the value column will hold the entered data for a specific field type. So in the case of a textfield this will only be a few characters. But in the case of a textarea, this can be alot of data.
That means the value column must be at least of type TEXT. Eventhough most types will not exceed 255 characters (MAX VARCHAR).
Now i'm not sure if this is the right approach. Will this give a performance penalty when i query the table? Do i have to save the textarea data in another table..? Or something like that?
Or can i simply change the value column to a TEXT type without any problems..?
VARCHAR maximum length is 255 prior to MySQL 5.0.3 , from MySQL 5.0.3 onwards the maximum length is 65535 , which is the maximun row size, so that maximum value is shared among all fields in the row.
See dev.mysql.com/doc/refman/5.0/en/char.html
When using MySQL, are there times that it would make sense to use a textual field (varchar, text, etc) instead of a numeric field (int, float, etc.) even if all data to be stored is numeric?
For example would it be better, faster or more efficient to store '5 digit zip code' data in a 5 char 'varchar' field rather than in an explicitly numeric field?
If yes, how can I determine when it is best to use a text field for numeric data?
I'm using InnoDB if relevant.
EDIT: I'm using a zipcode as an example above, but 'in general' when should choose a textual datatype over a numeric datatype when all data to be stored is numeric data.
Depends on how you are using the data. For Zip codes I would use a VARCHAR - simply because it is a part of an address and will not be using it for calculations. Also you may wish to include other countries in the future (in the UK we have post codes that are alpha numeric).
Ed's answer pretty much covers everything, as a general rule:
don't store numeric values in varchar columns - unless there are chances that other characters will have to be stored (examples are zip codes and phone numbers)
When you need computations/comparisons on the stored values, always use numerical data types (in string comparison 2 is bigger than 10)
I can't help but believe this topic has been written about over and over again but I'm having trouble finding any good, solid information.
What data type should I use to store 200 to 400 words of text? What about longer articles that could approach two or three thousand words?
What options should affect my decision? I don't plan to search this data but I can't completely rule out the possibility that I may want to do that later.
Unfortunately my background is MS Access where the only option for this was a memo field. It doesn't appear to be quite so simple with MySQL.
If you're using MySQL 5.0.3 or later, go VARCHAR. It can hold 65k bytes. As long as you have only 1 long VARCHAR per row, you should be fine.
Otherwise go with text.
From the mysql manual:
BLOB and TEXT differ from VARBINARY
and VARCHAR in the following ways:
There is no trailing-space removal for
BLOB and TEXT columns when values are
stored or retrieved. Before MySQL
5.0.3, this differs from VARBINARY and VARCHAR, for which trailing spaces are
removed when values are stored.
On comparisons, TEXT is space extended
to fit the compared object, exactly
like CHAR and VARCHAR.
For indexes on BLOB and TEXT columns,
you must specify an index prefix
length. For CHAR and VARCHAR, a prefix
length is optional. See Section 7.5.1,
“Column Indexes”.
BLOB and TEXT columns cannot have
DEFAULT values.
Also nice to know (from the manual):
Instances of BLOB or TEXT columns in
the result of a query that is
processed using a temporary table
causes the server to use a table on
disk rather than in memory because the
MEMORY storage engine does not support
those data types
which you really should take into account when formulating queries which use TEXT.
A TEXT field should be big enough to store most articles. Seems to be about equivalent to Access's Memo type. It can hold up to 65535 chars, which would be somewhere around...i dunno...10-12,000 words, on average?
The TEXT data type is a safe bet for your situation, VARCHARs are usually used when they need to be indexed or there is a well-defined value to be stored (IP address, zip code, etc).
I have a map from strings to integers. To store this map in a MySQL database I created the following table:
CREATE TABLE map(
Argument TEXT NOT NULL,
Image INTEGER NOT NULL
)
I chose the TEXT type for the argument because its length is unpredictable, currently the longest record has 2290 chars and the average length is 88 chars.
After I'd met the performance troubles I tried to add index on Argument column, but found that I must to specify length, so to avoid this limitation I added a new integer column containing hash values (md5 or else) of Argument column values.
ALTER TABLE map ADD COLUMN ArgumentHash INTEGER;
And combined index
CREATE INDEX argument_index USING HASH ON map(ArgumentHash, Argument(80));
Since that time the problems with performance has disappeared. I'd like to ask whether it is a correct way to solve this problem.
I don't think there is a "correct" way, it depends what you are using the column for.
In my experience, it is unusual to have to/want to select on a large text column; the text is usually data retrieved by some other key (unless indexed in some other way - egs. full text, Lucene - but that doesn't appear to be what you are doing)
If you do in fact need an exact match on a large field, then it may be more efficient to use the hash as it will likely let you keep the index smaller. My guess is that if you need to use an index size larger than the size of the hash (depends on how close to the start of the TEXT the values generally differ), use the hash.
Your best bet is to try it and see. Profile both approaches with representative data and find out.
So I was creating a table for comments, and I was wondering. What would be a good type for comment details? I put longtext. Well then, why would people need varchar if longtext can handle it? Also, which type would I want for usernames?
What is the purpose of "primary" for index? What is the purpose of index?
Update:
Let's say a comment was actually a review.
It is true that TEXT can handle any input you'd place in VARCHAR or CHAR field. In fact TEXT could handle and data you might want to put in DECIMAL, INT, or almost any other type as well. Following this logic we might as well make every column a TEXT type.
But this would be a mistake. Why? Because using the appropriate column type for the expected input allows the database to better optimize queries, uses less disk space and makes the data model easier to understand and maintain.
In regards to the questions: a username column should use VARCHAR(20), since you would want and expect that most usernames are going to short, usually no more than 10 - 20 characters long. For a review column (like a movie review or book review) a TEXT type would be appropriate as reviews can span a single paragraph to several pages.
In regards to indexes, try this link:
http://20bits.com/articles/interview-questions-database-indexes/
That depends on what a "comment" is in your system. Typically VARCHAR is pretty standard for both comments and usernames. This limits you to about 255 characters, which is generally pretty acceptable. If you need more characters in your comments, you can bump it up to a text, which gives you a little over 65k chars.
For more information, see the String Types Reference.
TEXT NOT NULL. That gives sufficient room, has a 2 byte overhead, and generally presents no problems.
Regarding TEXT
On comparisons, TEXT is space extended
to fit the compared object, exactly
like CHAR and VARCHAR.
For indexes on BLOB and TEXT columns,
you must specify an index prefix
length. For CHAR and VARCHAR, a prefix
length is optional. See Section 7.4.2,
“Column Indexes”.
BLOB and TEXT columns cannot have
DEFAULT values.
If you use the BINARY attribute with a
TEXT data type, the column is assigned
the binary collation of the column
character set.
Regarding VARCHAR:
Values in VARCHAR columns are
variable-length strings. The length
can be specified as a value from 0 to
255 before MySQL 5.0.3, and 0 to
65,535 in 5.0.3 and later versions.
The effective maximum length of a
VARCHAR in MySQL 5.0.3 and later is
subject to the maximum row size
(65,535 bytes, which is shared among
all columns) and the character set
used.
More at: http://dev.mysql.com/doc/refman/5.0/en/blob.html
Have a look at this web page, it lists all the MySQL field types and describes what they are and how they're different from each other.