How to insert without giving column names but not giving identity column - sql

Table definition:
CREATE TABLE IF NOT EXISTS public.test
(
"Id" integer NOT NULL GENERATED ALWAYS AS IDENTITY (INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1),
"SomeColumn" character(100) COLLATE pg_catalog."default",
CONSTRAINT test_pkey PRIMARY KEY ("Id")
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.test
OWNER to postgres;
I am trying this query:
INSERT INTO public.test VALUES ('testData');
But PostgreSQL throws this error:
ERROR: invalid input syntax for type integer: "testData"
LINE 1: INSERT INTO public.test VALUES ('testData');
I know this is valid in SQL Server. Is there a way the achieve this behaviour in PostgreSQL?
I do not want to specify the column names. Columns are defined in the order, but the identity column does not exist in the query.

I want to not give the column names
That's a bad idea. You should always specify the target columns for an INSERT statement. Especially if you want to skip some, but not others.
However, if you insist on bad coding style, you can use the DEFAULT keyword
INSERT INTO public.test VALUES (DEFAULT, 'testData');

Related

postgres doesn't autogenerate PK if the PK is inserted manually

I have a simple table like this:
CREATE TABLE IF NOT EXISTS myval
(
id integer NOT NULL DEFAULT nextval('myval_myval_id_seq'::regclass),
name character varying(255),
CONSTRAINT "PK_aa671c3359a0359082a84ecb801" PRIMARY KEY (id)
)
the sequence definition is:
CREATE SEQUENCE IF NOT EXISTS myval_myval_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 2147483647
CACHE 1
OWNED BY myval.myval_id;
when I insert data along with the primary key:
INSERT INTO myval(id, name) VALUES (1, 'sdf');
INSERT INTO myval(id, name) VALUES (2, 'sdf');
INSERT INTO myval(id, name) VALUES (3, 'sdf');
INSERT INTO myval(id, name) VALUES (4, 'sdf');
then, I insert it without the PK:
INSERT INTO myval(name) VALUES ('new sdf');
it gives an error saying:
duplicate key value violates unique constraint "PK_aa671c3359a0359082a84ecb801",
DETAIL: Key (myval_id)=(1) already exists.
I expected it to start with PK value of 5 but, instead it gives an error. Can we configure postgres to skip conflicting values and generate from the closest available value to use instead of throwing an error?
The best way to avoid such conflicts is to use identity columns - in this case a GENERATED ALWAYS AS IDENTITY seems the right option.
CREATE TABLE IF NOT EXISTS myval
(
id integer GENERATED ALWAYS AS IDENTITY,
name character varying(255),
CONSTRAINT "PK_aa671c3359a0359082a84ecb801" PRIMARY KEY (id)
);
This will work like a sequence (serial), however it will fail if the user tries to manually insert a value in this column
INSERT INTO myval (id,name)
VALUES (1,'foor');
ERROR: cannot insert a non-DEFAULT value into column "id"
DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS.
TIP: Use OVERRIDING SYSTEM VALUE to override.
If for whatever reason you must override this behavior in a certain INSERT statement you can do so using OVERRIDING SYSTEM VALUE, as the error message above suggests
INSERT INTO myval (id,name) OVERRIDING SYSTEM VALUE
VALUES (1,'foo');
You might be able to achieve a sequential value using serial even if the user screws things up with inserts, e.g. using trigger functions. But such an architecture is hard to maintain and imho is definitely not worth the trouble.
Demo: db<>fiddle

ODP.Net, Entity Framework: ORA-00947 (not enough values) when trying to add a record with Store Generated Identity

I have simple table where I want to insert new records.
The table has an ID column which is set as identity and generated always.
StoreGeneratedPattern=Identity is set for the ID column.
When I try to add a new record to the table in VB.Net
rec = New TEST_TABLE
ctx.TEST_TABLE.Add(rec)
ctx.SaveChanges()
It results in get ORA-00947: not enough values.
It seems as if the Entity Framework is creating an invalid SQL request. Something like INSERT INTO TEST_TABLE (ID, NAME) VALUES('Tom');.
How can I solve this situation?
How can I check, which SQL request is sent to Oracle?
Adding lines with direct sql queries works perfectly:
INSERT INTO TEST_TABLE (NAME) VALUES('Tom');
1 line affected.
The table's schemata is as follows:
CREATE TABLE "TEST_TABLE"
("NAME" VARCHAR2(20) NOT NULL ENABLE,
"ID" NUMBER GENERATED ALWAYS AS IDENTITY
MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOORDER NOCYCLE NOT NULL ENABLE,
CONSTRAINT "TEST_TABLE_PK" PRIMARY KEY ("ID") ENABLE
);
CREATE UNIQUE INDEX "TEST_TABLE_PK" ON "TEST_TABLE" ("ID");
Edit: I solved the problem: StoreGeneratedValue=Identity was set for NAME, not for ID. After correcting this, everything just works fine.
"Not enough values" comes when there are too few values passed on insert statement. Like this
insert into table_name(col1, col2, col3) values(1, 2)
I've passed 2 values whereas there are 3 columns listed in the beginning of insert. Thus, I almost sure, underlying insert statement lacks of a value.
You need to check content of inserting routine on VB side if posiible.
As a workaround I'd try to replace "GENERATED ALWAYS" with "GENERATED BY DEFAULT" and run the VB code with StoreGeneratedPattern=None

Postgres breaking null constraint on a serial column

I have a table that I create independently, the primary key is set with the serial type and a sequence applied to the table, but when I try to insert a value a NULL CONSTRAINT error is thrown and the return looks like null was passed, am I missing something in the INSERT statement?
SQL for table generation:
DROP TABLE IF EXISTS public."Team" CASCADE;
CREATE TABLE public."Team" (
"IdTeam" serial PRIMARY KEY,
name text NOT null,
CONSTRAINT "pKeyTeamUnique" UNIQUE ("IdTeam")
);
ALTER TABLE public."Team" OWNER TO postgres;
DROP SEQUENCE IF EXISTS public."Team_IdTeam_seq" CASCADE;
CREATE SEQUENCE public."Team_IdTeam_seq"
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public."Team_IdTeam_seq" OWNER TO postgres;
ALTER SEQUENCE public."Team_IdTeam_seq" OWNED BY public."Team"."IdTeam";
SQL for insert :
INSERT INTO public."Team" (name) values ('Manchester Untited');
The returning error:
ERROR: null value in column "IdTeam" violates not-null constraint
DETAIL: Failing row contains (null, Manchester Untited).
SQL state: 23502
I am baffled. Why are you trying to define your own sequence when the column is already defined as serial?
Second, a primary key constraint is already unique. There is no need for a separate unique constraint.
Third, quoting identifiers just makes the code harder to write and to read.
You can just do:
DROP TABLE IF EXISTS public.Team CASCADE;
CREATE TABLE public.Team (
IdTeam serial PRIMARY KEY,
name text NOT null
);
INSERT INTO public.Team (name)
VALUES ('Manchester Untited');
Dropping the sequence causes the default definition for the IdTeam column to be dropped. After recreating the sequence you will have to recreate the default definition.

Postgres: Problems using insert with select

I have a table defined like this:
CREATE TABLE wp_master (
gid integer NOT NULL DEFAULT nextval('wp_master_gid_seq'::regclass),
name character varying(80),
....
type integer DEFAULT 4,
CONSTRAINT p_key PRIMARY KEY (gid),
);
I want to insert data into the table from another table so I
insert into wp_master ( name, .... type) select "NAME", ...., 1 from ."Tiri2011";
but I get the error:
ERROR: duplicate key value violates unique constraint "p_key"
DETAIL: Key (gid)=(2) already exists.
Why is postgres trying to put anything into the gid field when I have explicitly not included it in the list of columns? I assumed that gid pick up its value from the sequence.
Russell
Is is trying to insert the next value of the wp_master_gid_seq sequence. Declaring an id column as serial (auto-increment) will create a sequence which has a stored value of the last inserted id which was auto-incremented. If at anytime you inserted a gid value manually, it bypassed the sequence and the autoincrement function may become broken, because the sequence value did not get updated accordingly.
The easiest way to fix it is to change the value of the sequence to the (max gid value of your table) + 1. Just execute this once and you should be ok
select setval('wp_master_gid_seq', coalesce((select max(id)+1 from wp_master), 1), false)

sql not null property error

I have a sql table and here is my column which gives error. When I try to add a new record which has null active_status to this table, It gives "not-null property references a null or transient value" error. Is there any idea?
active_status character varying(30) NOT NULL DEFAULT 'NEW'::character varying,
EDIT: I have created a new simple table;
CREATE TABLE mytable
(
"MyData" character varying(30) NOT NULL DEFAULT 'NEW'::character varying,
CONSTRAINT mytable_pkey PRIMARY KEY ("MyData" )
)
WITH (
OIDS=FALSE
);
ALTER TABLE mytable
OWNER TO postgres;
When I try to insert a string, it runs fine;
insert into mytable values('ssss');
But when I try to insert a null value it gives error;
insert into mytable values(null);
ERROR: null value in column "MyData" violates not-null constraint
SQL state: 23502
With this statement:
insert into mytable values(null);
you explicitely requested to insert a NULL value into the column MyData and therefor you get the error message.
If you want to use the default value, you need to tell the DBMS to do so:
insert into mytable values (default);
Btw: it is much better coding style to always specify the columns in the insert statement:
insert into mytable ("MyData") values (null);
And another thing: you should avoid using quoted identifiers ("MyData" vs. MyData) , they simply are more trouble than it's worth it.
You need to first create the column with NULL constraint. Update all rows for that column with the default values. Alter the column to have Not Null constraint