mysql> #浮点与定点列;
mysql> #浮点数,float(M,D) decimal(M,D);
mysql> #浮点数在计算机中的表示:
mysql> #234.87 23.487
mysql> #小数点左边的范围,小数点右边的范围;
mysql> #M和D的意思:
mysql> #M代表总位数,
mysql> #D是小数点右边的位数
mysql> #创建一个薪资登记表
mysql> use class
ERROR 1049 (42000): Unknown database 'class'
mysql> use test;
Database changed
mysql> create table salary(
-> sname varchar(20) not null default '',
-> gongzi float(6,2)
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.06 sec)
mysql> desc salary;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sname | varchar(20) | NO | | | |
| gongzi | float(6,2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> insert into salary values('zhangsan',-9999.99);
Query OK, 1 row affected (0.04 sec)
mysql> insert into salary values('lisi',9999.99);
Query OK, 1 row affected (0.00 sec)
mysql> select * from salary;
+----------+----------+
| sname | gongzi |
+----------+----------+
| zhangsan | -9999.99 |
| lisi | 9999.99 |
+----------+----------+
2 rows in set (0.00 sec)
mysql> alter table salary add bonus float(5,2) unsigned not null default 0.00;
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc salary;
+--------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| sname | varchar(20) | NO | | | |
| gongzi | float(6,2) | YES | | NULL | |
| bonus | float(5,2) unsigned | NO | | 0.00 | |
+--------+---------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> select * from salary;
+----------+----------+-------+
| sname | gongzi | bonus |
+----------+----------+-------+
| zhangsan | -9999.99 | 0.00 |
| lisi | 9999.99 | 0.00 |
+----------+----------+-------+
2 rows in set (0.00 sec)
mysql> insert into salary (sname,bonus) values('wangwu',888.88);
Query OK, 1 row affected (0.00 sec)
mysql> select * from salary;
+----------+----------+--------+
| sname | gongzi | bonus |
+----------+----------+--------+
| zhangsan | -9999.99 | 0.00 |
| lisi | 9999.99 | 0.00 |
| wangwu | NULL | 888.88 |
+----------+----------+--------+
3 rows in set (0.00 sec)
mysql> insert into salary (sname,bonus) values('wangwu',-88.88);
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from salary;
+----------+----------+--------+
| sname | gongzi | bonus |
+----------+----------+--------+
| zhangsan | -9999.99 | 0.00 |
| lisi | 9999.99 | 0.00 |
| wangwu | NULL | 888.88 |
| wangwu | NULL | 0.00 |
+----------+----------+--------+
4 rows in set (0.00 sec)
mysql> #加上unsigned不能为负,,,,,;
mysql> #浮点数占的空间:
mysql> #浮点数能存10^38, 小数点后能精确到1/(10^38);
mysql> #是系统定的,不能改变;
mysql> #用来表示数学中的小数,除了浮点,还有定点
mysql> #定点是把整数部分和小数部分分开存储,比float更精确
mysql> #decimal是变长类型;
mysql> create table account(
-> id int not null default 0,
-> acc1 float(9,2) not null default 0.00,
-> acc2 decimal(9,2)not null default 0.00
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.04 sec)
mysql> desc account;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| acc1 | float(9,2) | NO | | 0.00 | |
| acc2 | decimal(9,2) | NO | | 0.00 | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into account
-> values
-> (1,1234567.23,1234567.23);
Query OK, 1 row affected (0.00 sec)
mysql> select * from account;
+----+------------+------------+
| id | acc1 | acc2 |
+----+------------+------------+
| 1 | 1234567.25 | 1234567.23 |
+----+------------+------------+
1 row in set (0.00 sec)
mysql> #float 有时会损失精度,
mysql> #因此像账户这种敏感信息,要用decimal存储
mysql> #利用×××存储,单位是分
mysql> #float 占四个或八个;
mysql> #123456789.987654321 占八个字节;
mysql> #把整数和小数分开,