I wrote the following query.
-- TB_PK_INDEX
CREATE TABLE TB_PRIMARY_INDEX
(
COL VARCHAR COLLATE pg_catalog."POSIX" NOT NULL
)
WITH (
OIDS=false
);
-- TB_PK_INDEX Primary Key
CREATE UNIQUE INDEX PK_TB_PRIMARY_INDEX
ON TB_PRIMARY_INDEX
( -- TB_PK_INDEX
COL COLLATE pg_catalog."POSIX" varchar_pattern_ops ASC
);
-- TB_PK_INDEX
ALTER TABLE TB_PRIMARY_INDEX
ADD CONSTRAINT PK_TB_PRIMARY_INDEX
PRIMARY KEY
USING INDEX PK_TB_PRIMARY_INDEX
NOT DEFERRABLE;
And the following error occurs.
SQL Error [42809]: Error: "pk_tb_primary_index" no primary sort method for index 1 column
Detail: Cannot create a primary key or unique constraint using such an index.
Test was conducted on PostgreSQL 14.2.. The concept of operator class is lacking. Please advise.
Related
Table definition is as follows:
CREATE TABLE public.the_table
(
id integer NOT NULL DEFAULT nextval('the_table_id_seq'::regclass),
report_timestamp timestamp without time zone NOT NULL,
value_id integer NOT NULL,
text_value character varying(255),
numeric_value double precision,
bool_value boolean,
dt_value timestamp with time zone,
exported boolean NOT NULL DEFAULT false,
CONSTRAINT the_table_fkey_valdef FOREIGN KEY (value_id)
REFERENCES public.value_defs (value_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE RESTRICT
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.the_table
OWNER TO postgres;
Indices:
CREATE INDEX the_table_idx_id ON public.the_table USING brin (id);
CREATE INDEX the_table_idx_timestamp ON public.the_table USING btree (report_timestamp);
CREATE INDEX the_table_idx_tsvid ON public.the_table USING brin (report_timestamp, value_id);
CREATE INDEX the_table_idx_valueid ON public.the_table USING btree (value_id);
The query is:
SELECT * FROM the_table r WHERE r.value_id = 1064 ORDER BY r.report_timestamp desc LIMIT 1;
While running the query PostgreSQL does not use the_table_idx_valueid index.
Why?
If anything, this index will help:
CREATE INDEX ON the_table (value_id, report_timestamp);
Depending on the selectivity of the condition and the number of rows in the table, PostgreSQL may correctly deduce that a sequential scan and a sort is faster than an index scan.
I try to create code for postgresql
CREATE TABLE kaart (
kaartid integer NOT NULL,
naam character varying,
saldo real,
kaarthouderid integer
);
CREATE TABLE kaart_product (
kaartkaartid integer,
productid2 integer
);
CREATE TABLE kaarthouder (
id integer NOT NULL,
naam character varying(255),
naw character varying(255),
geslacht "char"
);
CREATE TABLE product (
naam character varying,
id integer NOT NULL
);
ALTER TABLE ONLY kaart
ADD CONSTRAINT kaart_pkey PRIMARY KEY (kaartid);
ALTER TABLE ONLY kaarthouder
ADD CONSTRAINT kaarthouder_pkey PRIMARY KEY (id);
ALTER TABLE ONLY product
ADD CONSTRAINT product_pkey PRIMARY KEY (id);
ALTER TABLE ONLY kaart
ADD CONSTRAINT kaartco FOREIGN KEY (kaartid) REFERENCES kaarthouder(id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY kaart_product
ADD CONSTRAINT kaartkaartidco FOREIGN KEY (kaartkaartid) REFERENCES kaart(kaartid) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE ONLY kaart_product
ADD CONSTRAINT productidco FOREIGN KEY (kaartkaartid) REFERENCES product(id) ON UPDATE CASCADE ON DELETE CASCADE;
INSERT INTO kaart VALUES (1, 'Sander',50.00 ,1);
INSERT INTO kaart_product VALUES (1,1);
INSERT INTO kaarthouder VALUES (1, 'Sander','test,testing','man');
INSERT INTO product VALUES ('studentenproduct',1);
But whenever i try to run it it gives me this error:
23503: insert or update on table "kaart" violates foreign key constraint "kaartco"
But i really dont know why this happens since it is the same to the other foreign keys that are below it
So can someone help me fix this?
You try to link to a product and a kaart that doesn't exist yet.
Move:
INSERT INTO kaart_product VALUES (1,1);
Two lines down under:
INSERT INTO product VALUES ('studentenproduct',1);
That should do the job for you.
Try to search google for forgein key contstraints and how they work.
Table A is a kind of unique sequence for all my tables.
-- Table: public."IdCentral"
-- DROP TABLE public."IdCentral";
CREATE TABLE public."IdCentral"
(
"Id" bigint NOT NULL DEFAULT nextval('"IdCentral_Id_seq"'::regclass),
"Tag" character varying(127) COLLATE pg_catalog."default",
CONSTRAINT "IdCentral_pkey" PRIMARY KEY ("Id")
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
Table B is any table of my database
-- Table: public."Users"
-- DROP TABLE public."Users";
CREATE TABLE public."Users"
(
"Id" bigint NOT NULL,
"Login" character varying(30) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT "Users_pkey" PRIMARY KEY ("Id"),
CONSTRAINT "PK" FOREIGN KEY ("Id")
REFERENCES public."IdCentral" ("Id") MATCH FULL
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public."Users"
OWNER to dba;
When I want to insert into B, I need to create a new record in A passing the B Table name as Tag.
What you want is CURRVAL:
SELECT CURRVAL('IdCentral_Id_seq');
... which will give you the current value for the ID sequence after insert. For safety, it's best to use it inside a transaction, especially if you're combining it with load-balancing:
BEGIN;
INSERT INTO "a" ...
INSERT INTO "b" VALUES ( CURRVAL('IdCentral_Id_seq', ... )
COMMIT;
That being said, it appears that you're implementing a "universal ID" system for your database. This is something every new DBA tries (I did), and it's inevitably a bad idea which you end up spending a lot of time refactoring out later. If there's some reason you genuinely need some kind of universal ID, consider using a UUID instead.
Below example table Stream_types gives an ERROR: there is no unique constraint matching given keys for referenced table, and having stared at it for while now I can't figure out why this error arises in this situation.
create table Stream_types (
id integer NOT NULL,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);
create table Streams (
id integer, -- PG: serial
code char(6) not null, -- e.g. COMPA1, SENGA1
name LongName not null,
offeredBy integer references OrgUnits(id),
stype ShortString references Stream_types(id),
description TextString,
firstOffer integer references Semesters(id), -- should be not null
lastOffer integer references Semesters(id), -- null means current
primary key (id)
);
The error is:
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
********** Error **********
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
SQL state: 42830
I'm using postgresql 9.3.16, can anyone help me fix this?
change create table Stream_types statement like here:
create table Stream_types (
id integer NOT NULL PRIMARY KEY,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);
Today I started to port a MySql database to PostgreSQL and I have a problem with the foreign keys and their 'sharing'
I don't know how to explain that so here is some pseudo code of the create script:
create table id_generator (
id serial PRIMARY KEY,
description varchar(50) );
create table parent (
id REFERENCES id_generator(id),
--content );
create table child (
id REFERENCES id_generator(id),
id_parent REFERENCES parent(id),
--content );
So I use one table as 'id_generator' to create the id's and to give the other tables a reference to a value table. In MySql I had no problems with that so I want that to work in PostgreSQL as well.
When running that script I get the error, that I need a primary key / unique on the table parent to create a reference on it. so I changed it into:
create table id_generator (
id serial PRIMARY KEY,
description varchar(50) );
create table parent (
id REFERENCES id_generator(id) PRIMARY KEY,
--content );
create table child (
id REFERENCES id_generator(id),
id_parent REFERENCES parent(id),
--content );
so the server accepts the create script and all is set up for some inserts.
I create the first id with:
insert into id_generator(description) values("parentID");
then I want to add a parent:
insert into parent(id, /*content*/) values(1, /*content*/);
that also works as expected, so I need to insert a child for parent:
insert into id_generator(description) values("childID");
insert into child(id,id_parent)values(2,1);
and here I get the error message "ERROR: duplicate key value violates unique constraint DETAIL: key (id_parent)=(1) allready exists"
edit:
\d child;
Tabelle ╗public.child½
Spalte | Typ | Attribute
-------------------+---------+-----------
id | integer | not null
id_parent | integer |
Indexe:
"child_pkey" PRIMARY KEY, btree (id)Fremdschlⁿssel-Constraints:
"child_id_parent_fkey" FOREIGN KEY (id_parent) REFERENCES parent(id)
"child_id_generator_fkey" FOREIGN KEY (id) REFERENCES id_generator(id)
how can I solve that?
Ok, I am sorry, problem solved, had a double call of the insert statement :facepalm: