简体   繁体   English

使用SQL Developer将DECIMAL类型插入Oracle DB

[英]Inserting DECIMAL type to Oracle DB with SQL Developer

Friends, i'm new in SQL and i can't find answer to my question 朋友,我是SQL新手,找不到我的问题的答案

I'm trying to insert decimal number with SQL Developer (version 4, build 15), but SQL Developer automaticaly rounding my number. 我正在尝试使用SQL Developer(版本4,内部版本15)插入十进制数字,但是SQL Developer自动将我的数字四舍五入。 My test DB is OracleXE112_Win64. 我的测试数据库是OracleXE112_Win64。

Example: 例:

CREATE TABLE salespeople
  (
    snum  INTEGER NOT NULL PRIMARY KEY,
    sname CHAR(15) NOT NULL,
    city  CHAR(15) NOT NULL,
    comm  DECIMAL
  );

then 然后

insert into SALESPEOPLE 
values (1001, 'Peel', 'London', .12);
insert into SALESPEOPLE 
values (1002, 'Serres', 'San Jose', .13);
insert into SALESPEOPLE 
values (1004, 'Motika', 'London', .11);
insert into SALESPEOPLE 
values (1007, 'Rifkin', 'Barcelona', .15);
insert into SALESPEOPLE 
values (1003, 'Axelrod', 'New York', .10);

As as result i see 0 in comm column. 结果,我在comm列中看到0 When i'm trying to insert number 1.35 , it's rounding to 1. 当我尝试插入数字1.35时 ,将四舍五入为1。

Thanks 谢谢

The decimal data type needs a scale and precision. decimal数据类型需要decimal和精度。 "Scale" is the number of digits after the decimal point. “比例”是小数点后的位数。

When you don't specify the value for scale, then it defaults to 0 -- essentially an integer. 如果不指定scale的值,则默认为0-本质上是整数。 (And precision defaults to 5.) (精度默认为5。)

Typically, Oracle databases use number instead of decimal . 通常,Oracle数据库使用number而不是decimal But, if you want decimal , specify the appropriate scale and precision. 但是,如果要使用decimal ,请指定适当的decimal和精度。

Checking the Oracle documentation for DECIMAL , it says: 查看Oracle文档中的DECIMAL ,它说:

"If the scale is not specified, the default scale is 0. If the precision is not specified, the default precision is 5." “如果未指定比例,则默认比例为0。如果未指定精度,则默认精度为5。”

Precision is : "the total number of digits, both to the left and the right of the decimal point". 精度为:“小数点左右的位数总数”。

Scale is : "the number of digits of the fractional component". 比例为:“小数部分的位数”。

This means that because your default scale is 0, then all numbers are rounded to an integer. 这意味着因为您的默认小数位数为0,所以所有数字都将四舍五入为整数。

To fix this you need to specify a SCALE for your data. 要解决此问题,您需要为数据指定SCALE Most scenarios I've seen are sufficed by DECIMAL(18,5) and from your example queries this should be fine. 我所看到的大多数情况都足以满足DECIMAL(18,5) ,从您的示例查询中来看,这应该没问题。

So, just change your table definition to: 因此,只需将表定义更改为:

CREATE TABLE salespeople
  (
    snum  INTEGER NOT NULL PRIMARY KEY,
    sname CHAR(15) NOT NULL,
    city  CHAR(15) NOT NULL,
    comm  DECIMAL(18,5)
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM