如何更新小數列以在 MySQL 中允許更多數字?


要更新小數列以允許更多數字,請使用 MODIFY COLUMN。語法如下所示

ALTER TABLE MODIFY COLUMN yourColumnName DECIMAL(yourIntValue,yourIntValue);

為了理解上述語法,我們建立一個表。建立表的查詢如下所示

mysql> create table allowDecimalWithMoreDigit
   -> (  
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Salary DECIMAL(3,2),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.64 sec)

現在可以使用 DESC 命令獲取表的描述。語法如下所示

DESC yourTableName;

現在可以使用上述命令獲取表的描述。查詢如下所示

mysql> desc allowDecimalWithMoreDigit;

以下是輸出

+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| Id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| Salary | decimal(3,2) | YES  |     | NULL    |                |
+--------+--------------+------+-----+---------+----------------+
2 rows in set (0.18 sec)

檢視示例輸出,salary 列具有 DECIMAL(3,2)。現在,您可以使用 ALTER 命令和 MODIFY COLUMN 將 DECIMAL(10,4) 更改為 DECIMAL(3,2)。查詢如下所示

mysql> alter table allowDecimalWithMoreDigit modify column Salary DECIMAL(10,4);
Query OK, 0 rows affected (2.46 sec)
Records: 0 Duplicates: 0 Warnings: 0

現在再次查看錶描述。查詢如下所示

mysql> desc allowDecimalWithMoreDigit;

以下是輸出

+--------+---------------+------+-----+---------+----------------+
| Field  | Type          | Null | Key | Default | Extra          |
+--------+---------------+------+-----+---------+----------------+
| Id     | int(11)       | NO   | PRI | NULL    | auto_increment |
| Salary | decimal(10,4) | YES  |     | NULL    |                |
+--------+---------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

檢視示例輸出,Salary 資料型別 DECIMAL(3,2) 已更改為 DECIMAL(10,4)。

更新於: 30-Jul-2019

1K+ 次瀏覽

啟動你的職業生涯

完成課程並獲得認證

開始學習
廣告