mysql jdbc: what is column size for BIGINT? [duplicate] - mysql

This question already has answers here:
Types in MySQL: BigInt(20) vs Int(20)
(6 answers)
Closed 5 years ago.
mysql jdbc: what is column size for BIGINT?
jdbc:
id BIGINT(20)
ResultSet columns = databaseMetaData.getColumns("foo", "bar", "table", "id");
columns.next();
int columnSize = columns.getInt(7);
the columnSize is 19. Is it 19 bytes?

Q: What is column size for BIGINT?
The MySQL BIGINT datatype is a 64-byte signed integer. From a JDBC resultset, that can be handled in Java as a long.
With the unsigned variant, the MySQL BIGINT UNSIGNED dataype, that could be handled as java.math.BigInteger. (The maximum value of MySQL BIGINT UNSIGNED exceeds the maximum value of Java long.)
Q: Is it 19 bytes?
The longest string value that MySQL will return for a BIGINT would be 20 characters. The lowest possible value for a MySQL BIGINT, represented as a character string is '-9223372036854775808'. (That's 19 digits, but an extra character is required for the minus sign.)
The largest value of BIGINT would have a string representation of 19 characters.
For the unsigned variant BIGINT UNSIGNED the largest value would be represented as 20 decimal digits, thus 20 characters.
In terms of storage in the MySQL database, the BIGINT datatype requires eight bytes.

Related

How is Postgres table size GREATER than Mysql Table Size?

On comparing Mysql and Postgres table sizes we found that:
Postgre Table size (4758390 rows) (vanilla postgres): 1402MB
Data Length = 1063MB
Index Length = 339MB
Mysql Table Size (4758390 rows) (with Inno DB): 1056MB
Data Length = 845MB
Index Length = 211MB
The tables have the following schema:-
The schema:-
MySQL
int(11)
varchar(15)
datetime
float
float
float
float
float
double
double
double
float
longtext
double
double
int(11)
double
float
int(11)
int(11)
float
int(11)
int(11)
int(11)
int(11)
varchar(50)
int(11)
int(11)
int(11)
Postgres
serial
varchar
timestamp
double precision
double precision
double precision
double precision
double precision
numeric
numeric
numeric
double precision
varchar
numeric
numeric
double precision
numeric
double precision
integer
integer
double precision
integer
integer
integer
integer
varchar
integer
integer
integer
The query used to calculate the sizes for the tables are:-
MySQL
SELECT table_name AS `Table`,data_length, index_length,
round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB`
FROM information_schema.TABLES
WHERE table_schema = "DB_NAME
AND table_name = "TABLE_NAME";
Postgres
SELECT pg_size_pretty(pg_total_relation_size('TABLE_NAME'));
Edit:-
Indexes in MySQL: Size
(varchar(15),datetime) -> 133 MB
(datetime) -> 78 MB
Indexes in Postgres: Size
(varchar,timestamp) -> 339 MB
I am new to databases and wondering how is this possible.
You're using data types with different sizes:
Mysql floats are 4 bytes vs postgresql doubles which are 8 bytes
Mysql datetime looks like 4 bytes (I was unable to find clear documentation) whereas postgreql timestamp is 8 bytes.
Mysql integer(11) is 4 bytes, while for Postgresql numeric The actual storage requirement is two bytes for each group of four decimal digits, plus three to eight bytes overhead

INT Datatype in MySQL

Does the INT, TINYINT, MEDIUMINT, BIGINT in MySQL Accepts the character '-' ? I used the data type INT in a column and the data inside has the character '-'. And somehow it didn't accept it. If it's not possible for INT data types, should I use VARCHAR?
The values for an integer in SQL are:
-2147483648 through 2147483647
And the byte size is 4 bytes.
Other maximum values:
BigInt: -9223372036854775808 through 9223372036854775807 (8 bytes)
SmallInt: -32768 through 32767 (2 bytes)
TinyInt: 0 through 255 (1 byte)
it dosent accept any char or string values like "-"
use varchar datatype if u want to insert "-"
Use VARCHAR(45) or TEXT datatype, if you want to insert characters.
you have to use Varchar DataType for this.
It will support both Int and Special Symbol like '-'
If you are not going to do any arithmetic operaion means then you can
use Varchar or text DataType

When to use the different numeric data types - TINYINT / SMALLINT / MEDIUMINT / INT / BIGINT - MySQL

I read the answers given here: What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL? , so I now know how they store the data, but I'm still not sure how to set my database up. For example, if I want a field to be either 0 or 1 (sort of binary, 0 = off, 1 = on), do I use TINYINT with a length of 1?
My main question is, what does the LENGTH setting determine? As each NUMERIC data type already has their own associated data size.
Also, what is the difference between SIGNED and UNSIGNED, and why should I choose one over the other?
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
Diffrence between SIGNED and UNSIGNED is with UNSIGNED you can store only positive numbers.
For example :
about INT (Normal INTEGER) values
The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
If you are using PK auto_increment value then you should use UNSIGNED in this case.
http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
This shows storage and range for each INTEGER types.
For binary fields use BIT.
the length of numerics specifies the precision before and after the comma. See here
An integer variable has 32 bits to store the integer value.
In signed integer the first bit is reserved to store positive or negative sign. So, a signed integer can use only 31 bits to store a value and hence its range will be −2,147,483,648 to +2,147,483,647.
Suppose if your program needs to store only positive integer greater than +2,147,483,647. You need to consider the long integer that will take 8 bits that will cause the wastage of memory.
Instead you can go with unsigned integer. In an unsigned integer no bit is reserved for the sign so now you have 32 bits to store the value. The only limitation with an unsigned integer is that you cannot use it to store negative values. The range of an unsigned integer of 32 bits will be 0 to 4,294,967,295.
Hope it clears your concept of signed and unsigned integer.

Issues faced with int(11) datatype in MYSQL

I have a table in MYSQL in with a primary key id int(11) (auto-incremented). Now I have a program which reads some text file and enters the value in id column.
So my table should contains :
id
897413
791783
But what happens is that, I can't find big numbers in my table. Is this because of maximum value that int(11) can hold? Increased int(11) to int(20) still facing same problem. I can't change the datatype to big int as I have already implemented a lot of code.
EDIT:
I tried to insert a single record with id as 523826 and it got saved in DB as 450258. Why so?
Definition from mysql manual for the int data type:
A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.
The int type is always 4 bytes (32 bits). The 11 in int(11) is just the "display width", that only matters for UNSIGNED ZEROFILL columns. More details on this blog post.
INT in MySQL is 32 bits. INT(11) likely means you have a signed INT, which for an ID is useless. Changing it to an unsigned INT will automatically double the number of IDs available (since nobody uses a negative ID). Even though 11 "seems" bigger, it's because it takes into consideration the "length" of the number if it's the maximum negative number (-2147483648) which is 11 characters long.
BIGINT will let you go up to 64 bits, signed or unsigned. Again, unsigned will allow you twice as many IDs (>0).
As Andrew mentioned above, if your PHP does not support 64 bit integers then you will not be able to easily use them.
Hope that helps.

Representing n-bit unsigned integers in mysql

How do I represent this data in mysql?
16 bit unsigned integer -----Range: 0x0000 - 0xFFF7
64 bit unsigned int. Depicted as xx:xx:xx:xx:xx:xx:xx:xx -----Range: 0x0000 - 0xFFFFFFFFFFFFFFFF
2 bits ----- 00 - None, 01 - Residential Security, 10 - High Security
32 bit unsigned int
Should I convert everything to string and convert it at application layer?
According to MySQL's Overview of Numeric Types:
UNSIGNED SMALLINT: range is 0 to 65535. This would be sufficient for 16-bit unsigned ints.
UNSIGNED TINYINT: range is 0 to 255. Sufficient for 2-bit unsigned int. It appears you would need to preserve leading zeroes, so use ZEROFILL too. To keep the value to just two characters wide, you can specify UNSIGNED ZEROFILL TINYINT(2).
UNSIGNED INT: range is 0 to 4294967295. Sufficient for 32-bit unsigned int.
UNSIGNED BIGINT: range is 0 to 18446744073709551615. See below:
The last one, the 64-bit unsigned int, has a couple of caveats, from the above linked page:
All arithmetic is done using signed BIGINT or DOUBLE values, so you should not use unsigned big integers larger than 9223372036854775807 (63 bits) except with bit functions! If you do that, some of the last digits in the result may be wrong because of rounding errors when converting a BIGINT value to a DOUBLE.
You can always store an exact integer value in a BIGINT column by storing it using a string. In this case, MySQL performs a string-to-number conversion that involves no intermediate double-precision representation.
The -, +, and * operators use BIGINT arithmetic when both operands are integer values. This means that if you multiply two big integers (or results from functions that return integers), you may get unexpected results when the result is larger than 9223372036854775807.
MySQL support several data types. See MySQL Data Types
UNSIGNED BIGINT : 8-byte (64-bit) integer
UNSIGNED INT : 4-byte (32-bit) integer
UNSIGNED SMALLINT : 2-byte (16-bit) integer
For the 2-bit type, you may use the TINYINT (8-bit) or the ENUM datatype.