0

我用我的英语道歉。我目前正在尝试在 putty 中创建一个数据库,对于我们使用 varchar 的单词,但是如果我们想插入数字呢?例如,我正在创建一个汽车数据库,其中包括汽车的制造年份、价格、制造和型号。

价格的正确语法是什么,当我收到错误时,我没有为它做正确的代码?

谢谢!

这是我的代码:

CREATE TABLE cars(
  cars_id int(10) UNSIGNED not null AUTO_INCREMENT PRIMARY KEY,
  make VARCHAR(25) not null,
  model VARCHAR(25) not null,
  price(value) null float,
  yom varchar(25) not null
);
4

2 回答 2

0

For storing prices you do not want to use a FLOAT. They are reliant on rounding and can have variable precision based on your system's hardware and/or software configuration.

http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html

Prices have a fixed decimal which is why you need a fixed decimal data type like DECIMAL.

To declare a DECIMAL column you'll need to specify the total number of digits, and the number of decimal places you'd like.

eg: to store $199.95 the column would need to be DECIMAL(5,2).

These are essentially stored as an integer 19995 and the decimal point is added in on retrieval. In this way you do not need to worry about gaining/losing cents randomly due to floating point precision issues.

于 2013-10-15T23:45:35.070 回答
-1

对于浮点类型,您必须指定最大位数和小数点右侧的位数,以逗号分隔。因此,对于 999999.99 的最高价格,您可以指定:

price FLOAT(8,2) NULL

这意味着总共 8 位数字,小数点右侧 2 位。

如果您拥有数百万辆汽车(例如 1378599.99),那么我们将需要price FLOAT(9,2) NULL.

于 2013-10-15T22:46:57.170 回答