MySQL Tutorial/Data Types/Decimal

Материал из SQL эксперт
Версия от 09:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DECIMAL(7,2)

mysql>
mysql>
mysql> CREATE TABLE myTable
    -> (
    ->    ID SMALLINT,
    ->    Price DECIMAL(7,2),
    ->    Weight FLOAT(8,4)
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql>
mysql> desc myTable;
+--------+--------------+------+-----+---------+-------+
| Field  | Type         | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| ID     | smallint(6)  | YES  |     | NULL    |       |
| Price  | decimal(7,2) | YES  |     | NULL    |       |
| Weight | float(8,4)   | YES  |     | NULL    |       |
+--------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql>
mysql> drop table myTable;
Query OK, 0 rows affected (0.00 sec)
mysql>


DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]

A packed exact fixed-point number.

M is the total number of digits.

D is the number of digits after the decimal point.

The decimal point and (for negative numbers) the "-" sign are not counted in M.

The maximum number of digits (M) is 65.

The maximum number of decimals (D) is 30.

If D is omitted, the default is 0.

If M is omitted, the default is 10.



DEC[(M[,D])] [UNSIGNED] [ZEROFILL], NUMERIC[(M[,D])] [UNSIGNED] [ZEROFILL], FIXED[(M[,D])] [UNSIGNED] [ZEROFILL] are synonyms for DECIMAL.


For inserts into a DECIMAL or integer column

mysql>
mysql>
mysql> CREATE TABLE t (d DECIMAL(10,0));
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> INSERT INTO t VALUES(2.5),(2.5E0);
Query OK, 2 rows affected, 2 warnings (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 2
mysql> SELECT d FROM t;
+------+
| d    |
+------+
|    3 |
|    3 |
+------+
2 rows in set (0.00 sec)
mysql>
mysql> drop table t;
Query OK, 0 rows affected (0.01 sec)
mysql>