Learn Mysql:mysql with Mr. Wang decimal type of data type
Teacher: Wang Shaohua QQ Group No.: 483773664
Learning content
What are decimal types and their range of values
Define the meaning of M and d in the syntax
The difference between fixed-point and floating-point types
First, Introduction
In MySQL, you use a floating-point type and a fixed-point number type to represent decimals.
Floating-point types include single-precision floating-point numbers (float type) and double-precision floating-point numbers (double).
The fixed-point number type is decimal.
Second, byte number, value range
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/A5/wKiom1d5vN6CJyhWAABYCfYvEfI502.png "alt = "Wkiom1d5vn6cjyhwaabycfyvefi502.png"/>
Iii. Definition of grammar
M: Accuracy, total length of data
D: Scale, length after the decimal point
Note:
The decimal point does not occupy the length eg:float (6,2) 1234.56
M affects storage range, Eg:float (6,2) -9999.99~~~9999.99
If the insertion value is more accurate than the actual defined precision, the system automatically rounds the processing. (Here the rounding takes the form of a bank, i.e. if the 5 front number is 1279, i.e. 5 is not in, if 5 front is 2468, then 5 into)
Iv. example (i) float
1 Build a commodity table goods
1 2 3 4 |
create table goods(
name varchar (10),
price float (6,2)
);
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/A4/wKioL1d5vN6TxYUJAAAZy6HKJMg541.png "alt = "Wkiol1d5vn6txyujaaazy6hkjmg541.png"/>
2 inserting values
1 |
insert into goods( name ,price) values ( ‘跑步机‘ ,688.6);
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/83/A5/wKiom1d5vN6CEriLAAAYgBCrg0Q704.png "alt = "Wkiom1d5vn6cerilaaaygbcrg0q704.png"/>
(ii) fixed-point and floating-point differences 1 decimal relative more precise
1. Add two columns
1 2 |
alter table goods add bigprice float (9,2);
alter table goods add deciprice decimal (9,2);
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/83/A4/wKioL1d5vN7xAmcQAAAzow0khQk676.png "alt = "Wkiol1d5vn7xamcqaaazow0khqk676.png"/>
2 Inserting data
1 |
insert into goods( name ,bigprice,deciprice) values ( ‘自行车‘ ,1234567.23,1234567.23);
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/83/A5/wKiom1d5vN6S5OxxAAAigB866Js890.png "alt = "Wkiom1d5vn6s5oxxaaaigb866js890.png"/>
Cause: Decimal is stored as characters in the database, and if you want to compare the accuracy of the data, or select the fixed-point number
2 default accuracy is not the same
If you do not specify precision, the float and fixed point numbers have their default precision.
float and double types save actual precision by default
Decimal type default integer bit is 10, decimal place is 0
To add a field:
1 2 3 |
alter table goods add a float ;
alter table goods add b double ;
alter table goods add d decimal ;
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/83/A5/wKiom1d5vN7Bz8RHAAAa7pVzO_4481.png "alt = "Wkiom1d5vn7bz8rhaaaa7pvzo_4481.png"/>
Insert data:
1 |
insert into goods ( name ,a,b,d) values ( ‘汽车‘ ,3.143,3.143,3.143);
|
650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/83/A4/wKioL1d5vN_gGVSdAAAnO5MGn1g088.png "alt = "Wkiol1d5vn_ggvsdaaano5mgn1g088.png"/>
V. Teaching VIDEO
Http://edu.51cto.com/course/course_id-6420.html
This article is from "Learn programming with Mr. Wang" blog, please be sure to keep this source http://teacherwang.blog.51cto.com/10946447/1795475
Learn Mysql:mysql with Mr. Wang decimal type of data type