I have a table that looks like the following:
CREATE TABLE word(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
word TEXT NOT NULL,
count INT NOT NULL,
ratio NUMERIC(10, 3) NOT NULL,
percent_of_total NUMERIC(10, 3) NOT NULL,
daily_count_id UUID REFERENCES daily_count(id)
);
I then tries to insert with this statement:
INSERT INTO word (word, count, ratio, percent_of_total, daily_count_id)
VALUES ('test', 5, 5/214, 5*100/214,
(SELECT id from daily_count WHERE day_of_count = CURRENT_DATE+1));
It works. It inserts the value when selecting it from the table then the numeric values has been rounded like the following:
67035a35-e5df-495b-95d5-cb3b4041c7b4 test 5 0.000 2.000 91858e7a-3440-4959-9074-9d197d6c97fc
The values 2.000 and 0.000 are rounded but I need them to be the precise value.
I'm using the DataGrip IDE but I do not think it has anything to do with it.
5/214 will be executed as integer division. I.e. the result will be integer.
If you want floating point division, you can simply do 5.0/214.
(Or use cast, e.g cast(5 as NUMERIC(10, 3)) / 214.)
Related
I want to create a table in my PostgreSQL:
CREATE TABLE my_table(
id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
description TEXT,
score FLOAT NOT NULL
);
How do I limit the number of decimals stored in the "score" column to a maximum of 3 decimals?
You would use numeric. However, you need a precision as well, which limits the maximum value:
CREATE TABLE my_table(
id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
description TEXT,
score NUMERIC(10, 3) NOT NULL
);
This will store numbers up to 9,999,999.999.
I created a table on a mariaDB with the following definition. Note the longitude and latitude fields.
Create Table geo_data (
geo_data_id int NOT NULL AUTO_INCREMENT,
place_id int NOT NULL,
longitude DOUBLE(18,18) SIGNED,
latitude DOUBLE(18,18) SIGNED,
Primary Key (geo_data_id),
Foreign Key (place_id) References place (place_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
When I try to insert data into the geo_data table using
Insert into geo_data (place_id, longitude, latitude) Values (1, 1.2, 3.4);
I receive the following error message:
Error: ER_WARN_DATA_OUT_OF_RANGE: Out of range value for column 'longitude' at row 1
I guess I am missing something here, since I don't believe 1.2 could in any way be out of range of a Double(18,18). So what on earth is going on here?
Your column is defined as DOUBLE(18,18). The first number is the scale (the total number of digits in the whole number, including decimals); the second is the precision (the number of decimal positions).
Giving the same value to both the scale and precision means that your value cannot be greater than 1 (all 18 digits are decimals).
You want to decrease the precision to something smaller in order to leave room for non-decimal digits, like: DOUBLE(18, 6), which gives you 12 non-decimal positions.
I have a C# app where the user fills a form to store a product in a SQL Server database.
The problem is that every time a product is stored (through the user filling a form), the price (a decimal) is automatically converted to int and has 1 added to it.
I initially thought it was an issue with the app, however, the registration process is pretty simple and I didn't find any error there, so I inserted a row directly from SQL Server and the issue presented itself, so this tells me the issue is in SQL Server, not in the app.
Executing
insert into product (code, description, unit_price, stock, category_code)
values (7, 'Window Cleaner', 20.50, 20, 3)
Results into price being 21.
This is the table definition
CODE INT NOT NULL,
DESCRIPTION VARCHAR(30) NOT NULL,
UNIT_PRICE DECIMAL NOT NULL,
STOCK INT NOT NULL,
CATEGORY_CODE INT NOT NULL
CONSTRAINT PK_PRODUCT PRIMARY KEY(CODE),
CONSTRAINT FK_CATEGORY_CODE FOREIGN KEY (CATEGORY_CODE) REFERENCES Category(CODE),
CONSTRAINT PRODUCT_POSITIVE_VALUES CHECK(UNIT_PRICE > 0 AND CODE >= 0 AND STOCK >= 0)
You have used the following to define your column:
UNIT_PRICE DECIMAL NOT NULL
This has no precision nor scale and will therefore use the default precision (18) and scale (0). The default scale of 0 is effectively an int. So when you insert/update a value the value will get rounded to an int. To solve your problem define your column with the correct precision and scale e.g.
UNIT_PRICE DECIMAL(9,2) NOT NULL
Reference
Currently working on a school project.
I'm trying to create the following table:
CREATE TABLE Purchase (
ID INT NOT NULL,
Type INT DEFAULT 3 NOT NULL,
Price DECIMAL(5,5) NOT NULL,
CONSTRAINT check_3 CHECK (TYPE = 3),
CONSTRAINT price_check CHECK (Cost>0),
CONSTRAINT pk_1 PRIMARY KEY (ID),
CONSTRAINT fk_1 FOREIGN KEY (ID,Type) REFERENCES Part(ID,Type));
My problem is when I'm trying to insert values into this column.
When I try to do this:
INSERT INTO Purchase VALUES (12, 3, 200);
I get the following error:
SQL> INSERT INTO Purchase VALUES (12, 3, 200);
INSERT INTO Purchase VALUES (12, 3, 200)
*
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column
I don't get what I'm doing wrong. Can't I add integers to a decimal column? Is that the problem? Doesn't make much sense to me.
Thank you for taking the time to read this!
First, always list the columns for an insert:
INSERT INTO Purchase (id, type, price)
VALUES (12, 3, 200);
Second, your price is declared as DECIMAL(5, 5). That means that the prices range from 0.00000 to 0.99999.
Presumably, you want a broader range. I don't know what that is, but DECIMAL(10, 5) would solve your problem. More generally, just NUMBER solves your problem in Oracle.
DECIMAL(5,5) means you will recieve space for 5 number after the comma (5 of 5). Please try to create your table like that: DECIMAL(10,5)
I am trying to create a table in Access.
I have the following code:
CREATE TABLE Class Enrollement (
OfferNo INTEGER PRIMARY KEY,
StdNo Text(9) NULL,
EnrGrade Decimal(2) Percision(8) scale(4) NULL
);
EnrGrade needs to be a decimal, Precision of 8, Scale of 4, and 2 decimal places.
The last line of code is not correct. How would I do this?
I believe you're looking for:
Where the first value is the precision (number of decimal digits, followed by scale, or numbers after the decimal)
CREATE TABLE ClassEnrollment (
OfferNo INTEGER PRIMARY KEY,
StdNo Text(9) NULL,
EnrGrade Decimal(8, 2) NULL
);
You must enable ANSI-92 Query Mode. After that in your query you can write:
CREATE TABLE Offering (
OfferNo INTEGER PRIMARY KEY,
StdNo Text(9) NULL,
EnrGrade Decimal(8,4) NULL
);
Precision of 8, Scale of 4, and 2 decimal places
These requirements appear contradictory. A decimal column with a precision of 8 and a scale of 4 can store up to 4 decimal places.
Perhaps the intention of the spec is to display at least two decimal places? e.g.
SQL DDL:
CREATE TABLE ClassEnrollement (
OfferNo INTEGER PRIMARY KEY,
StdNo NVARCHAR(9),
EnrGrade DECIMAL(8, 4)
);
SQL DML:
SELECT OfferNo, StdNo,
FORMAT$(ClassEnrollement, '0.00##')
AS ClassEnrollement__formatted
FROM ClassEnrollement;
Or perhaps the extra numeric scale is to enable custom rounding? The DECIMAL type exhibits rounding by trucation, a feature often missed because all other numeric types exhibit bankers' rounding. A rule of thumb is to store an extra place of numeric scale so that the inherent rounding, whatever it is, does not affect the raw value being stored, enabling custom rounding to be applied later. Two extra may simply be over-engineering ;)