I was using the MySQL INT(11) data type, but had to change to using CHAR(45) because I was dealing with large integers.
Now the CHAR is allowing empty strings to be submitted rather than return errors like it should. Is there another data type I can use?
You could use BIGINT or UNSIGNED BIGINT.
Anyways, you should do some validation at application level, so it will show the user some meaningful error message when the data he sent is not valid. And this is regardless the datatype used.
i would use BigInt, check this page: http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
if INT doesn't have the range you need, you can 'upgrade' to BIGINT
Generally speaking, if the value is number, you should stick to the numeric data classes-- not doing so usually leads down the road of hard to find bugs and issues.
a half step would be specifing INT as UNSIGNED (basically doubles the range by not including the negative side of the number line in the data space)
See the documentation for more info on numeric types.
Related
I can't understand why should we set field length while creating SQL table fields.
INT(11),
VARCHAR(255),
Data lengths are another form of data validation - they allow you to easily constrain the data in the table and not allow values that are larger than your program's logic would allow.
These are two different cases.
INT(11)
The "length" of an integer is almost meaningless, and MySQL 8.0 deprecates this syntax. An INT is always a 32-bit integer, regardless of the length argument. The only practical use of the length argument is if you use:
INT(11) ZEROFILL
This pads the number with zeroes when you fetch it, not as it is stored. The number is still stored exactly the same as INT(2) or INT(50), as a 32-bit integer. See my answer to Types in MySQL: BigInt(20) vs Int(20)
VARCHAR(255)
It's necessary to define a length because that's how data types must work in relational theory. The definition of a data type is "a named, finite set of values." It can't be a finite set if the strings have infinite length.
There is also the practical reason: to store a string, MySQL precedes it with one or two bytes encoding the length, so it knows how many characters to read. One byte of length information is used if the length is up to 255. Two bytes of length information is used if the length is up to 65535. That's the maximum length supported for VARCHAR in MySQL.
I have an old database table with column, which type is BIGINT. There's a lot of stored procedures and views that use that table and that column.
For some reason I need to change the type of that column to NUMERIC(38,0).
Is it safe to do it? Should I cast in any stored procedure and view existing BIGINT to NUMERIC(38,0)?
According to me numeric data type is identical with decimal which represents a fixed precision number, which will scale numeric data from -10^38 +1 through 10^38 –1
I don't think that the number types you mention are using fixed precision number and therefore BIGINT is probably the most efficient way to store the number especially if you want to perform some computation in your application.
I don't see really any use for computation with those number and therefore you may even use a string of appropriate length which requires more space in the database but you may be able to allow grouping characters in the numbers.
using BIGINT datatype instead of string you can create efficient indexes.
As you write you're already using numeric datatype and therefore if you upgrade to SQL 2008R2 / 2012 you should consider switching to BIGINT as you don't need fraction in your number. The BIGINT data type is intended for use when integer values might exceed the range that is supported by the int data type.
EDIT:
You can change the data type from BIGINT to NUMERIC(38,0) but be ensure that a Arthimetic overflow error shouldn't occur while converting.
Yes, it is.
According to this table on MSDN an numeric(38,0) has an higher capacity than a bigint.
I calculated the maximum values based on the numbers in the matrix:
9223372036854775808 (bigint, 2^63-1, 8 bytes)
1000000000000000000000000000000000000000 (numeric(38,0), 10^38–1, 17 bytes)
I do realize that it is better for a column to be an Integer if one has to perform mathematical calculations on it.
I probably have to perform mathematical calculations on the "year" column but minimally. So would it be better to store it as a String or Integer?
Thanks.
Save it as an integer.
While there may be some application where you are reading and serving this data so frequently that the int->string conversion is a problem... that is going to be an edge case.
On the other side
Integers provide smaller options than strings in data storage (such as TINYINT)
You avoid conversions due to math
It's going to confuse/annoy/frustrate all the developers that come after you when they query a data type that is naturally a number and get a string.
If you are not expecting your YEAR variable to ever contain non-digit values then yes you should store it as a number.
I would not store it as INT since I doubt year will reach the limit that INT has to offer. I would save it as SMALLINT or even TINYINT either should be unsigned.
SMALLINT UNSIGNED gives you max value of 65535, unless you are storing years that exceed the year 65535 this should suffice.
You could go crazy and save it as a YEAR!
This limits you to 1901-2155.
If this is too restrictive, I prefer a CHAR(4) to an INT; MySQL DATETIME comparisons are done in a string like manner..
You can do things like
WHERE year < CURDATE()
without worries then.
I use the Navicat MySQL GUI and have noticed there are Length and Decimal settings for all columns:
I understand the how the length and decimals settings work for float and decimal data types, but do they matter for other column types such as int and datetime?
MySQL doesn't use the term "Decimals", so that must be a term decided upon in the GUI tool you're using when defining DECIMAL or FLOAT types. For authoritative information, consult the GUI tool's documentation on how these settings affect types they wouldn't normally seem to apply to.
Searching around Navicat's documentation turns up little of any use on how it expects "Length" and "Decimals" to apply to integer and date types:
se the Length edit box to define the length of the field and use Decimals edit box to define the number of digits after the decimal point (the scale) for Floating Point data type.
...so not really helpful.
MySQL has a few options for storage length of integer types (which limit the maximum size of the integer that the column can hold), but those limits are specified by the name of the data type rather than a numeric length specified in the column definition.
This table of INT types explains the numeric ranges possible for each named type.
MySQL also offers an option on the integer types in the form of a display length like INT(11) which affects the displayed value rather than the stored value when using the ZEROFILL option.. Your GUI editor appears to map its "Length" option to the integer zerofill attribute.
See also: What is the benefit of ZEROFILL in MySQL?
How to find out, absent good documentation:
However, when working with a GUI client and not understanding what it is doing under the hood, the best advice I can offer would be to try out different settings and then examine the output of SHOW CREATE TABLE <tablename> to see what DDL statement the GUI ultimately constructed and sent to the RDMBS.
Syntax reference: SHOW CREATE TABLE
For datetime, it specifies the precision, and is a number up to 6. 3 will give you milliseconds and 6 will give you microseconds (millionth of a second). The length of an INT changes how the value is displayed, but it will still store the same value.
I'm a bit confused with the MySQL Documentation with regards to the storage requirements for various fields. I'm currently working with redesigning a database and I'm seeing TINYINT(4) as they data type. Previously I've never given any thought to this, but will this require one byte and just truncate the last digit off the number, or will it actually require 2 bytes and be converted to a SMALLINT internally?
EDIT - I know that the number represents the amount of digits that will be displayed, like TINYINT(2) will only show 2 digits or whatever, but what if that number is more than the data type can actually hold?
As you stated correctly the TINYINT type uses 1 byte of storage for 256 possible integer values (-128 through 127) or UNSIGNED 0-255. See that -128? This is (along with ZEROFILL) the reason for (4). But it won´t get converted to a SMALLINT automatically, so choose your data type accordingly.
See this link, this blog deals with the topic (as mentioned in answer here).