Mysql integer data type deep parsing _mysql

Source: Internet
Author: User
Here we give int char no width, and the system assigns it a width by default.
m indicates the maximum display width. The maximum effective display width is 255. The display width is independent of the range of values that the storage size or type contains
Let's do the test.
Copy Code code as follows:

MySQL (root@localhost:test 03:19:00) >create Table C (
-> ID int NOT NULL,
-> name char not NULL);
Query OK, 0 rows affected (0.25 sec)
MySQL (root@localhost:test 03:19:34) >desc C;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID | Int (11) | NO | | NULL | |
| name | char (1) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in Set (0.00 sec)

So we can see here, the system will automatically give our data type a default broadband value, where this width is in fact only in the role of Zerofill can play a certain role. Below we'll see what the other defaults are,
Copy Code code as follows:

MySQL (root@localhost:test 03:34:53) >alter table C Modify ID smallint;
Query OK, 0 rows affected (0.05 sec)
records:0 duplicates:0 warnings:0
MySQL (root@localhost:test 03:39:39) >desc C;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | smallint (6) | YES | | NULL | |
| name | varchar (10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in Set (0.00 sec)
MySQL (root@localhost:test 03:39:44) >alter table C Modify ID bigint;
Query OK, 4 rows affected (0.23 sec)
Records:4 duplicates:0 warnings:0
MySQL (root@localhost:test 03:40:12) >desc C;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID | bigint (20) | YES | | NULL | |
| name | varchar (10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in Set (0.01 sec)

Here we look at the case where the insertion value is greater than the range of values of the data type:
Copy Code code as follows:

MySQL (root@localhost:test 03:25:58) >insert into C values (+, ' Chen ');
Query OK, 1 row affected, 2 warnings (0.08 sec)
MySQL (root@localhost:test 03:26:20) >show warnings;
+---------+------+---------------------------------------------+
| Level | Code | message |
+---------+------+---------------------------------------------+
| Warning | 1264 | Out of range value for column ' ID ' at row 1 |
| Warning | 1265 | Data truncated for column ' name ' at row 1 |
+---------+------+---------------------------------------------+
2 rows in Set (0.00 sec)
MySQL (root@localhost:test 03:26:27) >select * from C;
+------+------+
| ID | name |
+------+------+
| 127 | C |
+------+------+
1 row in Set (0.02 sec)
MySQL (root@localhost:test 03:26:40) >insert into C values ("The", ' Chen ');
Query OK, 1 row affected, 2 warnings (0.05 sec)
MySQL (root@localhost:test 03:26:53) >select * from C;
+------+------+
| ID | name |
+------+------+
| 127 | C |
| 127 | C |
+------+------+
2 rows in Set (0.00 sec)

The tinyint here is a byte, an integer that can represent the range from 0-255, but why not here until 127, The reason is that we didn't give him an unsigned type.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.