MySQL Numeric Type - mysql

Hi I need to add a column to my table where I should insert a value that indicates the year relative to the row. So it will just be a number, always integer, always bigger than 0, always 4 digits... so what's the best numeric type for the column and how would the phh query to add the column would be?

Try the SMALLINT the minimum value is -32768 and the maximum value 32767 an interger can be used for bigger numbers between: -2147483648 and 2147483647
for example:
SMALINT(3)
means a number from -32768 until 32767 the (3) represents the display width of the field

I would suggest datatype YEAR()
Note: Values allowed in four-digit format: 1901 to 2155.
Otherwise SMALLINT() should be fine.
Statement would be:
ALTER TABLE table_name
ADD column_name datatype

Related

The minimum value of an SQL data type (programmatically)

In a MySQL database, I have a table with a column of type varbinary(255) (CHARSET is set to binary). The values in the column loo like these:
-8999130301
NULL
-7010978
-16139671
-2
I interpret the values as signed integers.
Now I want to replace all NULL values with the minimum value for the varbinary(255) data type using the COALESCE() function.
The minimum signed integer that can be stored in a varbinary(255) attribute is "-9999..." (with 254 nines), but I do not want to hard code the value.
How can I compute the minimum value for a data type with SQL to use it in COALESCE()?

MySQL insert into table with 00001 automatically converts to 1

I have a table with a column id INT(11). When I do:
insert into table (id) VALUES ('00001');
it converts it to 1. I don't mind this behavior but I am wondering if this will change in future versions of mysql. Is there documentation or a reason on why this works?
This is because of the ZEROFILL flag on the column. If the flag is set, it adds zeros as padding to your numbers so that they meet the total "width" of the number. So if you have an INT(3) and you INSERT 1, you get 001.
In your case, you probably have an INT(1) so it is just truncating your leading zeros because they do not affect the value of the number—only the display of the number.
What is the benefit of zerofill in MySQL?

Out of range value for column 'contact_no' at row 1

I was trying to add a number with a length of 11 but when I input it in the database it can't. If I try to add a number with a length of 10 it can.
This is the error:
ERROR 1264: 1264: Out of range value for column 'contact_no' at row 1
SQL Statement:
INSERT INTO `mcs`.`new_table` (`id`, `contact_no`) VALUES ('1', '12345678901')
It's not clear what question you are asking.
Why is this error being returned?
Likely, the contact_no column is declared with datatype of INT, a 32-bit integer, and you attempted to assign a value larger than the supported maximum value.
The maximum value for a signed 32-bit integer is 2,147,483,647. (that decimal value of 2^31-1. For an unsigned 32-bit integer, maximum value is 2^32.)
Some other question that wasn't asked
If you need to store values larger than the maximum supported by the INT datatype, you could define the column as a different datatype.
For example, BIGINT gives a maximum value of 2^63-1 for unsigned.
If you need a maximum of 12 decimal digits, you could use a DECIMAL(12) datatype.
Change your data type of contact_no to BIGINT.
Check range of different data type at MYSQL official website.
I personally recommend you to use varchar, as you don't need to compare contact number with any field.
Please check datatype of contact_no column.
'12345678901' is exceeding its size.

Mysql INSERT recording wrong value

This must be a simple one but got no idea why is this happening.
Under this query:
INSERT INTO assist_reg (ar_id,ar_subid,ar_date) VALUES ('','2431052014','2014-05-31');
Field ar_subid on DDBB always records this value you can see on this screenshot:
ar_subid is a INT field with maximum of 20 characters, non-null with no predeterminate valur. this table is under UTF8-generalci.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html - you are exceeding the int data type's max value. You need a bigint

make all auto increment integers the same number of digits with leading zeros

I was wondering if there was a way to set my id column(auto-incremented) to always have 5 digits using leading zeros in my mysql table. So the first value would be 00001 then 00002 and so on up to 99999 instead of 1,2,3 up to 99999. Thanks
Try adding ZEROFILL attribute to the field;
Declaration:
`ID` INT(5) ZEROFILL AUTO_INCREMENT NOT NULL
Your ID column is a numeric value, so it's always going to have the same value. If you want the integer to be formatted a certain way, then you're going to have to do that formatting as part of the SELECT. Look at the LPAD function.
LPAD with leading zero