postgres column "X" does not exist - sql

I have this postgrse code:
CREATE TABLE IF NOT EXISTS config_change_log
(
id serial primary key,
last_config_version varchar(255) NOT NULL,
is_done Boolean NOT NULL DEFAULT '0',
change_description varchar(255),
timestamp timestamp default current_timestamp
);
INSERT INTO config_change_log(last_config_version, is_done, change_description )
VALUES("5837-2016-08-24_09-12-22", false, "{ 'key':'value'}");
and I get this error:
psql:createConfigChangeLog.sql:11: ERROR: column "5837-2016-08-24_09-12-22" does not exist
LINE 2: VALUES("5837-2016-08-24_09-12-22", false, "{ 'key':'value'}"...
how can it be? it's a value not a column.postgr

Use single quotes for string constants
INSERT INTO config_change_log(last_config_version, is_done, change_description )
VALUES('5837-2016-08-24_09-12-22', false, '{ ''key'':''value''}');
Also you can escape single quotes in data by doubling them
SQL FIDDLE DEMO

Related

Invalid JSON text but it seems that it is okay [duplicate]

This question already has answers here:
Do the JSON keys have to be surrounded by quotes?
(7 answers)
Closed 1 year ago.
I am using Sequeal Ace. It says this error
Invalid JSON text: "The document root must not be followed by other values." at position 6 in value for column 'orders.drink'.
CREATE TABLE `orders` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`drink` json NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
A json that I want to put in.
{"abc": {
"milk": 100,
"tea": 100
},
"def":{
"milk": 100,
"cola": 100,
}
}
Using your table I managed to do a simple insert with the following query. You need to just add quotation marks to the main keys in the object.
INSERT INTO orders (id, user_id, drink, meal) VALUES(2, 2, '{"yoji": {
"milk": 100,
"barley_tea": 100,
"soy_milk": 100
},
"nyuji":{
"milk": 100,
"barley_tea": 100,
"soy_milk": 100}
}', 'asd');

How can we add NULL as a value for a field on snowflake that accepts null value if its type is datetime

I am trying to load some data from a stage into a table having the following DDL:
CREATE TABLE IF NOT EXISTS SAT_COUNTRY_PROGRAMME (
COUNTRY_PROGRAMME_SAT_HASH_KEY VARCHAR(32) NOT NULL,
LOAD_DT DATETIME NOT NULL,
LOAD_END_DT DATETIME NULL DEFAULT NULL,
RECORD_SRC VARCHAR(64) NOT NULL,
COUNTRY_NAME VARCHAR(64) NOT NULL,
COUNTRY_PROGRAMME_HASH_KEY VARCHAR(32) NOT NULL,
PRIMARY KEY (COUNTRY_PROGRAMME_SAT_HASH_KEY),
CONSTRAINT fk_SAT_COUNTRY_PROGRAMME_HUB_COUNTRY_PROGRAMME1
FOREIGN KEY (COUNTRY_PROGRAMME_HASH_KEY)
REFERENCES HUB_COUNTRY_PROGRAMME (COUNTRY_PROGRAMME_HASH_KEY)
ON DELETE CASCADE
ON UPDATE CASCADE)
;
As you can see, the LOAD_END_DT could be NULL.
Here is the insert command:
INSERT INTO SAT_COUNTRY_PROGRAMME
(SELECT md5(md5(t.$3)), current_timestamp(), NULL, 'PORTFOLIO', (replace(replace(t.$4, t.$3), ' - ')), md5(t.$3)
FROM #INGEST_STAGE_TEMP/country_programmes.csv (file_format=>'GENERIC_CSV_FORMAT') t);
The error is:
NULL result in a non-nullable column
Here is the file format:
ALTER FILE FORMAT "DEV_DB_IYCF"."DATA_VAULT_INGEST".GENERIC_CSV_FORMAT
SET COMPRESSION = 'AUTO' FIELD_DELIMITER = ',' RECORD_DELIMITER = '\n'
SKIP_HEADER = 1 FIELD_OPTIONALLY_ENCLOSED_BY = 'NONE' TRIM_SPACE = TRUE
ERROR_ON_COLUMN_COUNT_MISMATCH = TRUE ESCAPE = 'NONE'
ESCAPE_UNENCLOSED_FIELD = '\134' DATE_FORMAT = 'AUTO' TIMESTAMP_FORMAT = 'AUTO' NULL_IF = ('NULL');
I tried to use 'NULL' and '' but got the same error.
As the error message says, you are trying to insert a NULL value to a non-nullable column. Therefore you need to check if any of first 4 columns contain NULL values:
SELECT $1, $2, $3, $4 from #INGEST_STAGE_TEMP/country_programmes.csv
(file_format=>'GENERIC_CSV_FORMAT')
where $1 is null or $2 is null or $3 is null or $4 is null ;
NULLIF is used to convert specific strings such as 'NULL' to NULL values. It's not something that may help you to avoid the "NULL result in a non-nullable column".

DB2 SQL Insert NULL into NOT NULL WITH DEFAULT

I faced a problem when inserting a NULL value into a column defined as NOT NULL WITH DEFAULT values.
In this example, I removed most of the columns for illustration purposes.
CREATE TABLE
FKTIM04
(
OBJECTID CHARACTER(32) NOT NULL,
UP_CHANGE_CL CHARACTER(1) DEFAULT '1' NOT NULL,
UP_CTRL_CL CHARACTER(1) DEFAULT '0' NOT NULL,
CONSTRAINT PK_FKTIM04 PRIMARY KEY (OBJECTID)
);
When I execute this SQL statement, there is an error:
INSERT INTO KTI.FKTIM04 (
UP_Change_CL
,UP_ctrl_CL
,ObjectID
)
VALUES (
NULL
,NULL
,'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
Error:
[Code: -407, SQL State: 23502] Assignment of a NULL value to a NOT
NULL column "TBSPACEID=2, TABLEID=1298, COLNO=46" is not allowed..
SQLCODE=-407, SQLSTATE=23502, DRIVER=4.22.29
I know that the column is defined as NOT NULL. If it tries to insert a NULL into the column, shouldn't it take the DEFAULT value instead?
Please teach me how to get the DEFAULT values to be inserted instead.
What should I look out for?
Thank you.
The default value will be used for a column if a value is not supplied in the INSERT statement for this column.
So don't include the columns that you want to get their default values in the list like this:
INSERT INTO KTI.FKTIM04 (
ObjectID
)
VALUES (
'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
this way the row will be inserted and the 2 columns, since they were not specified in the list, will get their default values.
See the demo.
Another way to achieve the same is by using DEFAULT keyword:
INSERT INTO FKTIM04 (
UP_Change_CL
,UP_ctrl_CL
,ObjectID
)
VALUES (
DEFAULT
,DEFAULT
,'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
See the demo.

How do I declare a table with a string column that has a non null default value in Postgres?

What do I need to add to make sure this table is created with the 'name' field as non null but with a value of ""?
CREATE TABLE stuff (id serial primary key, name varchar(64) <-- what goes here??
Standard SQL applies:
CREATE TABLE stuff (
id serial primary key,
name varchar(64) not null default ''
);
It's possible you attempted to use double quotes to specify the text literal like this "", which will explode. Postgres uses single quotes to delimit text literals, like this ''.
See SQLFiddle.

sql insert fails on postgresql database

I am trying to run the following insert statement using pgadmin3:
INSERT INTO device
VALUES
(12345,
'asdf',
'OY8YuDFLYdv',
'2',
'myname',
'2013-04-24 11:30:08',
Null,Null)
But I keep getting the following error message:
ERROR: invalid input syntax for integer: "asdf"
LINE 4: 'asdf',
^
********** Error **********
ERROR: invalid input syntax for integer: "asdf"
SQL state: 22P02
Character: 42
Here's the table definition:
CREATE TABLE device
(
device_id integer NOT NULL DEFAULT nextval('device_device_id_seq'::regclass),
userid integer NOT NULL,
description character varying(255),
password character varying(255) NOT NULL,
user_id integer NOT NULL,
createdname character varying(255),
createddatetime timestamp without time zone,
updatedname character varying(255),
updateddatetime timestamp without time zone,
CONSTRAINT device_pkey PRIMARY KEY (device_id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE device
OWNER TO appadmin;
Can you tell me where I'm going wrong? I've tried changing the single quotes to double quotes but that didn't help.
I don't want to have to list all the column names in the INSERT if I dont have to.
Thanks.
Apparently you're expecting the INSERT to skip device_id since it is the primary key and has a default that comes from a sequence. That's not going to happen so PostgreSQL thinks you mean this:
insert into device (device_id, userid, ...)
values (12345, 'asdf', ...);
If you insist on not listing your columns explicitly (and making the people that get to maintain your code suffer needlessly) then you can specify DEFAULT in the VALUES to tell PostgreSQL to use the PK's default value; from the fine manual:
INSERT INTO table_name [ ( column_name [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
[...]
DEFAULT
The corresponding column will be filled with its default value.
For example:
INSERT INTO device
VALUES
(DEFAULT,
12345,
'asdf',
...
But really, you should just specify the columns to make the SQL easier to understand and more robust when the schema changes.