I have a table definition in Postgres. I would like to add a constraint to a column that is of Character datatype to have only 3 allowed values:
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL
);
So I want the my_text column to contain only 'A', 'B' or 'C' as values.
Where can I find some documentation on this?
Use a check constraint:
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL,
constraint check_allowed check (my_text in ('A', 'B', 'C'))
);
More details in the manual: http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-CHECK-CONSTRAINTS
If you want to be able to add caracters without modyfying the condition:
CREATE TABLE my_ref
(
my_value varying(1) PRIMARY KEY
)
INSERT INTO my_ref VALUES( 'A' );
INSERT INTO my_ref VALUES( 'B' );
INSERT INTO my_ref VALUES( 'C' );
CREATE TABLE my_table
(
id character varying(255) NOT NULL,
uid character varying(255) NOT NULL,
my_text text NOT NULL,
is_enabled boolean NOT NULL,
constraint check_allowed FOREIGN KEY( my_text ) REFERENCES my_ref( my_value )
);
You won't be able to add values in my_text that aren't in my_ref table.
Related
I have users table like below
CREATE TABLE public.users
(
id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
uid uuid DEFAULT (md5(((random())::text || (clock_timestamp())::text)))::uuid,
createdon timestamp without time zone DEFAULT now(),
createdby integer,
modifiedon timestamp without time zone,
modifiedby integer,
comments boolean DEFAULT false,
verified boolean DEFAULT false,
active boolean DEFAULT true,
deleted boolean DEFAULT false,
tags text[] COLLATE pg_catalog."default",
user_type user_types NOT NULL,
fullname character varying(100) COLLATE pg_catalog."default" NOT NULL,
email character varying(84) COLLATE pg_catalog."default" NOT NULL,
pword character varying(32) COLLATE pg_catalog."default",
salt character varying(32) COLLATE pg_catalog."default",
hash text COLLATE pg_catalog."default",
source character varying(100) COLLATE pg_catalog."default",
reference character varying(100) COLLATE pg_catalog."default",
CONSTRAINT users_pkey PRIMARY KEY (id),
CONSTRAINT email_unique UNIQUE (email)
,
CONSTRAINT users_createdby_fkey FOREIGN KEY (createdby)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT users_modifiedby_fkey FOREIGN KEY (modifiedby)
REFERENCES public.users (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
email field is set to unique
when I try to insert record twice on pgadmin, I got the error.
however, if the same query run over my nodejs app via pg library, records are inserted
what is the reason of this misoperation?
the query object that used in app:
{ text: 'INSERT INTO public.players ( createdby, user_type, fullname, email, pword, reference, source, salt, hash ) \n VALUES ( $1, $2, $3, $4, $5, $6, $7, $8, $9 ) RETURNING id',
values:
[ null,
'player',
'James De Souza',
'james#desouza.com',
'4297f44b13955235245b2497399d7a93',
'organic',
'on-site',
'07ecab28a4bab8f1bf63208ac8961053',
'25571c0618c701087495069cb4e45bf4fb07197e5ff301d963b670a9e033d2044557eb46537cee27a51da8b1fd0c8987b68ad7e8e47f48a06bcb1f44e6d3678d2c875d3dd4311a506a75eabf6fff23b65deba6a202606cc6d6b42abe4b25d136faffed8bd0046620f4e10ef0b974b108b27511a42c150983e268e1f7522ad678f0699848747a9e2f4a0cafc66704915a38966fbc76647678d907ca960533a5dc4de983167fafb7807e583dd5affcc2e14900295c6f396e768a32f106a4c636be78a6df96268216bc9410373fcc2528eb7984e2cb91ae62c3c65660dc477db3c3bfeadfacb214a055a48a1e9ed0c169ee54fcc6e7b24435cb53c3596e19bedbfef2c289ffb784f6fce18b9623253260e17aca5b3d810248ece6c51d810f3b44b1eb95225d5170cde0f3c9fda8ceefd9a287016c785576264f95ee961254bc371fed8671a7497456ce439d7318f21e539ce5940bd2fd73a350fc5d139cbe06bda568663a35488ceb7c62dadf3ee6d5810b5248abe447472b9c294a13c30144271a06e10b6a7f070df5bd7e804b13b1ab541c65de65dc5b85cf3199d7b13431095aff83de6939afc2d72d187597bf8214bf45f356591f7e513e7026322a20beed430966fbd3cbe4ec2c95b54d081c032f5e2ba930019857bb63e7c631668e3f607559b4ffffc1de6c957f687930f2900fb27123aaaf5f55a06844586cee94d10757' ] }
NOTE: public.players is inherited from public.users
CREATE TABLE public.players (
"username" character varying(100) UNIQUE DEFAULT concat('player', (random() * 100000000)::int::text),
"location" int REFERENCES public.list_locations ON DELETE RESTRICT,
"address" text,
"bio" text
) INHERITS (public.users);
just realized that unique constraint not working over inherited table
is there any solution or workaround for this problem(or whatever)?
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.
I have the following tables :
CREATE TABLE public.participant_audit
(
participant_audit_id bigint NOT NULL DEFAULT nextval('participant_audit_participant_audit_id_seq'::regclass),
participant_id bigint,
shared_asset_id bigint NOT NULL,
asset_role_type character varying(200) NOT NULL,
user_external_ref_uuid uuid NOT NULL,
user_first_name character varying(200) NOT NULL,
user_last_name character varying(200) NOT NULL,
user_email_address character varying(200) NOT NULL,
deleted_timestamp timestamp(0) with time zone,
row_updated_timestamp timestamp(6) with time zone NOT NULL,
row_created_timestamp timestamp(6) with time zone NOT NULL,
row_created_by_db_user oid NOT NULL,
row_updated_by_db_user oid NOT NULL,
created_by_client uuid,
updated_by_client uuid,
CONSTRAINT participant_audit_pkey PRIMARY KEY (participant_audit_id)
)
WITH (
OIDS=FALSE
);
CREATE TABLE public.participant
(
participant_id bigint NOT NULL DEFAULT nextval('participant_participant_id_seq'::regclass),
shared_asset_id bigint NOT NULL,
asset_role_type_id bigint NOT NULL,
user_external_ref_uuid uuid NOT NULL,
user_first_name character varying(200) NOT NULL,
user_last_name character varying(200) NOT NULL,
user_email_address character varying(200) NOT NULL,
deleted_timestamp timestamp(0) with time zone,
row_updated_timestamp timestamp(6) with time zone NOT NULL,
row_created_timestamp timestamp(6) with time zone NOT NULL,
row_created_by_db_user oid NOT NULL,
row_updated_by_db_user oid NOT NULL,
created_by_client uuid,
updated_by_client uuid,
CONSTRAINT participant_pkey PRIMARY KEY (participant_id),
CONSTRAINT participant_asset_role_type_id_fkey FOREIGN KEY (asset_role_type_id)
REFERENCES public.asset_role_type (asset_role_type_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT participant_shared_asset_id_fkey FOREIGN KEY (shared_asset_id)
REFERENCES public.shared_asset (shared_asset_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
And the following TRIGGER FUNCTION:
-- DROP FUNCTION public.participant_audit();
CREATE OR REPLACE FUNCTION public.participant_audit()
RETURNS trigger AS
$BODY$
BEGIN
insert into participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
updated_by_client
)
select NEW.* ;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
When I execute the following INSERT
INSERT INTO participant (shared_asset_id,asset_role_type_id,
user_external_ref_uuid,user_first_name,user_last_name,
user_email_address,row_created_by_db_user,
row_updated_by_db_user,created_by_client,updated_by_client)
VALUES (1, 1, 'c9d140ad-b0da-4a9d-a898-8719000c7b7b'::uuid , 'john', 'simpson', 'js#gmail.com', 1::oid,1::oid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid);
I get the following error:
ERROR: INSERT has more target columns than expressions LINE 2:
...user , row_updated_by_db_user , created_by_client,updated_by...
^ QUERY: insert into public.participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type ,
user_external_ref_uuid,user_first_name , user_last_name ,
user_email_address , deleted_timestamp,
row_updated_timestamp,row_created_timestamp , row_created_by_db_user ,
row_updated_by_db_user , created_by_client,updated_by_client)
select NEW.* CONTEXT: PL/pgSQL function participant_audit() line 3 at SQL statement
********** Error **********
ERROR: INSERT has more target columns than expressions SQL state:
42601 Context: PL/pgSQL function participant_audit() line 3 at SQL
statement
How can I fix this issue ??
The problem is in your trigger. Count the columns that you are trying to insert into the audit table here.
insert into participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
updated_by_client
)
select NEW.* ;
That's quite a few more than what's contained in NEW because your insert statement has only 10 columns in it. I believe some of your columns maybe taking NULL values. Pass nulls explicitly in the SELECT part of your statement inside the trigger.
I am new to Postgresql, I am trying to create this table actually just following a similar mysql table. But I keep getting this error for ENUM()
Below is the query for creating the table structure:
CREATE TABLE IF NOT EXISTS gkb_users (
id bigint NOT NULL,
userid character varying(50) NOT NULL DEFAULT '',
password character varying(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email character varying(100) NOT NULL DEFAULT '',
gender enum('Male','Female') NOT NULL,
dob date NOT NULL,
mobile character varying(10) NOT NULL DEFAULT '',
telephone character varying(15) NOT NULL DEFAULT '',
city character varying(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
PIN character varying(255) NOT NULL,
shipping_PIN character varying(255) NOT NULL,
area character varying(255) NOT NULL,
shipping_area character varying(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted enum('0','1') NOT NULL
);
Any help will be greatly appreciated. Thanks
ENUM is a user-defined datatype. You can use CREATE TYPE syntax to create your enum and then use it in the schema to create table.
CREATE TYPE your_enum2 AS ENUM('0','1');
CREATE TYPE your_enum1 AS ENUM('male','female');
Followed by the CREATE TABLE statement,
CREATE TABLE IF NOT EXISTS gkb_users (
id bigint NOT NULL,
userid character varying(50) NOT NULL DEFAULT '',
password character varying(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email character varying(100) NOT NULL DEFAULT '',
gender your_enum1 NOT NULL,
dob date NOT NULL,
mobile character varying(10) NOT NULL DEFAULT '',
telephone character varying(15) NOT NULL DEFAULT '',
city character varying(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
PIN character varying(255) NOT NULL,
shipping_PIN character varying(255) NOT NULL,
area character varying(255) NOT NULL,
shipping_area character varying(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted your_enum2 NOT NULL
);
Refer postgresql docs https://www.postgresql.org/docs/current/static/datatype-enum.html for more information on enum creation and usage.
You don't need an enum for this (and I personally think one never needs an enum - but that's off topic).
You should either implement this as a check constraint:
CREATE TABLE IF NOT EXISTS gkb_users
(
id bigint NOT NULL,
userid varchar(50) NOT NULL DEFAULT '',
password varchar(50) NOT NULL DEFAULT '',
firstname text NOT NULL,
middlename text NOT NULL,
lastname text NOT NULL,
email varchar(100) NOT NULL DEFAULT '',
gender text NOT NULL,
dob date NOT NULL,
mobile varchar(10) NOT NULL DEFAULT '',
telephone varchar(15) NOT NULL DEFAULT '',
city varchar(50) NOT NULL DEFAULT '',
address text NOT NULL,
shippingaddress text NOT NULL,
pin varchar(255) NOT NULL,
shipping_pin varchar(255) NOT NULL,
area varchar(255) NOT NULL,
shipping_area varchar(255) NOT NULL,
previouscart text NOT NULL,
updatedon timestamp(0) NOT NULL,
is_deleted integer NOT NULL,
constraint check_gender check (gender in ('Male', 'Female')),
constraint check_deleted flag check (is_deleted in (0,1))
)
However, for is_delete should better be a proper boolean column - then you also don't need a check constraint for that column.
Postgres - like many other DBMS - is case sensitive when comparing strings. So with the above constraint you won't be able to store 'male' into the gender column.
Unrelated but: if you were assuming that varchar(255) has some magic performance benefits compared to e.g. varchar(300) then you are wrong. The maximum length of a varchar column does not influence the performance or the space requirements when storing the values.
I am not able to delete record from parent table of PostGres DB.. Any one of you can get me an idea on this.
-- Table: tbl_patient
-- DROP TABLE tbl_patient;
CREATE TABLE tbl_patient
(
patient_id bigserial NOT NULL,
date_of_birth date NOT NULL,
fathers_name character varying(255) NOT NULL,
first_name character varying(255) NOT NULL,
last_name character varying(255),
marital_status character varying(255),
mobile_number character varying(255) NOT NULL,
occupation character varying(255),
phone_number character varying(255),
pregnancy_status character varying(255),
sex character varying(255) NOT NULL,
CONSTRAINT tbl_patient_pkey PRIMARY KEY (patient_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE tbl_patient
OWNER TO postgres;
-- Table: tbl_address
CREATE TABLE tbl_address
(
address_id bigserial NOT NULL,
address_line_1 character varying(255) NOT NULL,
address_line_2 character varying(255),
city character varying(255),
country character varying(255),
district character varying(255) NOT NULL,
pincode character varying(255) NOT NULL,
state character varying(255),
street character varying(255),
patient_id bigint,
CONSTRAINT tbl_address_pkey PRIMARY KEY (address_id),
CONSTRAINT fk_slc6pgeimmox5buka8bydy6c4 FOREIGN KEY (patient_id)
REFERENCES tbl_patient (patient_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE tbl_address
OWNER TO postgres;
//-------------------------------------------------------------------
When I put this command
DELETE FROM tbl_patient
WHERE patient_id = 1;
I got this error below
ERROR: update or delete on table "tbl_patient" violates foreign key
constraint "fk_slc6pgeimmox5buka8bydy6c4" on table "tbl_address" SQL
state: 23503 Detail: Key (patient_id)=(1) is still referenced from
table "tbl_address".
You write DELETE NO ACTION and you wanting actions:) Just need to change to
REFERENCES tbl_patient (patient_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
NO ACTION means that server wont do anything with referenced rows if they exists. Since they exists and you specified also MATCH SIMPLE to one-column-foreign key then PostgreSQL cannot perform deletion because of that referenced rows.