pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re:  Creation of FK without enforcing constraint for existing data
2+ messages / 2 participants
[nested] [flat]

* Re:  Creation of FK without enforcing constraint for existing data
@ 2022-08-02 07:14  sivapostgres@yahoo.com <sivapostgres@yahoo.com>
  0 siblings, 1 reply; 2+ messages in thread

From: sivapostgres@yahoo.com @ 2022-08-02 07:14 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Hello,
I need to create a Foreign Key for a table without enforcing the constraint for existing data.   Few orphan exists in existing data, which we plan to resolve it later.  
We use the following query to create the FK [ Which of course checks for the presence of record in referencing table]
ALTER TABLE public.table1    ADD CONSTRAINT "constraint1_FK" FOREIGN KEY (field1)    REFERENCES public.tabnle2(field2) MATCH SIMPLE    ON UPDATE RESTRICT    ON DELETE RESTRICT;
The facility NOT to check for the presence of the parent record is available in SQL Server.   Searching an equivalent option in PostGreSQL.  Is it available?   Else any other way to achieve this.
Happiness Always
BKR Sivaprakash

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Creation of FK without enforcing constraint for existing data
@ 2022-08-02 07:43  Gavan Schneider <list.pg.gavan@pendari.org>
  parent: sivapostgres@yahoo.com <sivapostgres@yahoo.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Gavan Schneider @ 2022-08-02 07:43 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; +Cc: sivapostgres@yahoo.com

On 2 Aug 2022, at 17:14, sivapostgres@yahoo.com wrote:

> Hello,
> I need to create a Foreign Key for a table without enforcing the 
> constraint for existing data.   Few orphan exists in existing data, 
> which we plan to resolve it later.  
> We use the following query to create the FK [ Which of course checks 
> for the presence of record in referencing table]

Try something like 
[sql-altertable.html](https://www.postgresql.org/docs/current/sql-altertable.html):

	ALTER TABLE public.table1 ADD CONSTRAINT "constraint1_FK" FOREIGN KEY 
(field1) REFERENCES
	public.tabnle2(field2) MATCH SIMPLE  ON UPDATE RESTRICT ON DELETE 
RESTRICT
	DEFERRABLE  INITIALLY DEFERRED
  	;

> The facility NOT to check for the presence of the parent record is 
> available in SQL Server.   Searching an equivalent option in 
> PostGreSQL.  Is it available?   Else any other way to achieve this.
>
No knowledge of SQL Server and how this is specified there, but the 
…DEFFER… syntax is according to the SQL standard

Regards

Gavan Schneider
——
Gavan Schneider, Sodwalls, NSW, Australia
Explanations exist; they have existed for all time; there is always a 
well-known solution to every human problem — neat, plausible, and 
wrong.
— H. L. Mencken, 1920

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2022-08-02 07:43 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-02 07:14 Re:  Creation of FK without enforcing constraint for existing data sivapostgres@yahoo.com <sivapostgres@yahoo.com>
2022-08-02 07:43 ` Gavan Schneider <list.pg.gavan@pendari.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox