[英]how to insert values to table in postgresql
When i try to insert values to my table, it doesn't work and i get this error:当我尝试向我的表中插入值时,它不起作用并且我收到此错误:
ERROR: invalid input syntax for integer: "regular"
LINE 2: ('100', 'Astronomy Today', '1st publisher', 6, 'regular', 4/...
^
This is my code:这是我的代码:
CREATE TABLE Subscribes
(cid numeric(5,0),
title varchar (30),
publisher varchar(30),
period integer,
offer varchar (10),
sfrom date,
primary key(cid, title, publisher, offer, period),
foreign key(cid) references Customer(cid),
foreign key(title, publisher,offer, period) references Pricing(title,
publisher,offer, period));
INSERT INTO Subscribes VALUES
('100', 'Astronomy Today', '1st publisher', 6, 'regular', 4/4/2015),
('100', 'Bridal Guide', '3rd publisher', 12, 'regular', 1/5/2015),
('100', 'Click Magazine', '3rd publisher', 6, 'regular', 20/12/2014),
('107', 'Bridal Guide', '3rd publisher', 12, 'regular', 29/4/2015);
PS: I've checked every variable and couldn't find the problem PS:我已经检查了每个变量并找不到问题
Place single quotes around date like below在日期周围放置单引号,如下所示
INSERT INTO Subscribes VALUES
('100', 'Astronomy Today', '1st publisher', 6, 'regular', '2015/4/4')
someone was faster than me :)有人比我快:)
CREATE TABLE Subscribes (cid numeric(5,0), title varchar (30), publisher,varchar(30), period integer, offer varchar (10), sfrom date ....
INSERT INTO Subscribes VALUES ('100' , 'Astronomy Today' , '1st publisher' , 6 , 'regular' , 4/4/2015) .....
you have problem inserting date field, its not quoted ... and its format is not well formed.你在插入日期字段时遇到问题,它没有被引用......并且它的格式格式不正确。 you should use standard system format, '2015-04-04' or use to_date(text, text) conversion function from text to date inside insert.
您应该使用标准系统格式,'2015-04-04' 或使用 to_date(text, text) 转换函数从插入中的文本到日期。
When using INSERT
, you should take care with the code you write:使用
INSERT
,您应该注意您编写的代码:
So, it should look more like this:所以,它应该看起来更像这样:
INSERT INTO Subscribes(cid, title, publisher, period, offer, sfrom)
VALUES (100, 'Astronomy Today', '1st publisher', 6, 'regular', '2015-04-04'), . . .
To insert a date you should use TO_DATE要插入日期,您应该使用 TO_DATE
The following insert should work for your case:以下插入应该适用于您的情况:
INSERT INTO Subscribes(cid, title, publisher, period, offer, sfrom)
VALUES (100, 'Astronomy Today', '1st publisher', 6, 'regular', TO_DATE('04/04/2015', 'DD/MM/YYYY'))
29/4/2015
is not a valid date literal. 29/4/2015
不是有效的日期文字。 Either use a proper ANSI literal: date '2015-04-29'
or use the to_date()
function (I prefer ANSI literals, because it's less typing)要么使用适当的 ANSI 文字:
date '2015-04-29'
或使用to_date()
函数(我更喜欢 ANSI 文字,因为它的输入较少)
And '100'
is not a number it's a character literal. '100'
不是数字,而是字符文字。 Number don't need single quotes.数字不需要单引号。 Use
100
instead.改用
100
。
You should also always qualify the column names to make the statement more robust:您还应该始终限定列名以使语句更健壮:
INSERT INTO Subscribes (cid, title, publisher, period, offer, sfrom)
Putting all together this results in something like this:把所有这些放在一起,结果是这样的:
INSERT INTO Subscribes
(cid, title, publisher, period, offer, sfrom)
VALUES
(100, 'Astronomy Today', '1st publisher', 6, 'regular', date '2015-04-04'),
(100, 'Bridal Guide', '3rd publisher', 12, 'regular', date '2015-05-01'),
(100, 'Click Magazine', '3rd publisher', 6, 'regular', date '2014-12-20'),
(107, 'Bridal Guide', '3rd publisher', 12, 'regular', date '2015-04-29');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.