postgresql中的货币类型

92vpleto  于 2023-03-17  发布在  PostgreSQL
关注(0)|答案(1)|浏览(221)

我从Mockaroo下载了数据,其中列价格确实存在货币符号。

create table car (
    id BIGSERIAL NOT NULL PRIMARY KEY,
    make VARCHAR(100) NOT NULL,
    model VARCHAR(100) NOT NULL,
    price NUMERIC(19, 2) NOT NULL
);
insert into car (id, make, model, price) values (1, 'Oldsmobile', 'Intrigue', '$754.65');
insert into car (id, make, model, price) values (2, 'Dodge', 'Intrepid', '$913.84');
insert into car (id, make, model, price) values (3, 'Chevrolet', 'S10', '$573.35');
insert into car (id, make, model, price) values (4, 'Ford', 'F-Series', '$528.57');
insert into car (id, make, model, price) values (5, 'Honda', 'Accord', '$495.70');
insert into car (id, make, model, price) values (6, 'Isuzu', 'Trooper', '$489.83');
insert into car (id, make, model, price) values (7, 'Mitsubishi', 'Eclipse', '$637.15');
insert into car (id, make, model, price) values (8, 'Subaru', 'Legacy', '$632.20');
insert into car (id, make, model, price) values (9, 'Mercury', 'Mountaineer', '$566.08');

但是有一个错误:数值类型语法无效。
有什么办法吗?

bq8i3lrv

bq8i3lrv1#

数值列(或任何“数字”)不能存储$符号,如果确实需要将货币与值一起存储,则需要将其存储在两列中:

create table car (
    id BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    make VARCHAR(100) NOT NULL,
    model VARCHAR(100) NOT NULL,
    price NUMERIC(19, 2) NOT NULL,
    currency varchar(3) NOT NULL
);

insert into car (make, model, price, currency) 
values 
('Oldsmobile', 'Intrigue', 754.65, 'USD'),
('Dodge', 'Intrepid', 913.84, 'USD'),
('Chevrolet', 'S10', 573.35, 'USD');

注意,不应该为生成的列提供值(既不应该为不鼓励的bigserial,也不应该为符合标准的标识列)

相关问题