With Check Option Postgresql - postgresql

I have an ALTER TABLE statement, written in T-SQL (SQL Server):
ALTER TABLE myTable WITH CHECK ADD CONSTRAINT [FK_myTable_myColumn] FOREIGN KEY(myColumn) REFERENCES otherTable (Column)
If I want to translate this statement in Postgresql, how can I make this? Paying attention to WITH CHECK ADD CONSTRAINT

You need to
remove WITH CHECK - I don't know what this is supposed to do, but you can't have a "check constraint" together with a foreign key constraint in Postgres
use standard compliant identifiers (without the square brackets)
ALTER TABLE my_table
ADD CONSTRAINT fk_mytable_mycolumn
FOREIGN KEY(my_column) REFERENCES other_table (column)

Related

Make a previously existing foreign key column have a unique constraint in postgres

I need to create a migration for an already existing table to make it's foreign key have a UNIQUE constraint. How do I do this?
From the examples I found in the documentation, it is mostly done when the table is created. The issue is I need to add this onto a column that already exists and is already set as a foreign key. This is what the table looks like at it's creation:
CREATE TABLE IF NOT EXISTS "myTable" (
"_id" SERIAL NOT NULL,
"myForeignKeyId" INTEGER NOT NULL,
"name" VARCHAR(255) NOT NULL,
CONSTRAINT "pk_myTable" PRIMARY KEY ("_id"),
CONSTRAINT "fk_myTable_myForeignKeyId" FOREIGN KEY ("myForeignKeyId") REFERENCES "myOtherTable" ("_id")
);
What I want to do is on a migration make myForeignKeyId unique. How do I do that?
I have tried to following:
CREATE UNIQUE INDEX CONCURRENTLY "myTable_myForeignKeyId"
ON province ("myForeignKeyId");
ALTER TABLE IF EXISTS "myTable"
ADD CONSTRAINT "myForeignKeyId"
UNIQUE USING INDEX "myTable_myForeignKeyId";
First off, when I try this in a migration I get the error:
CREATE INDEX CONCURRENTLY cannot run inside a transaction block
So that part cannot be done, but even just doing it through SQL, the second part doesn't work either as it claims myForeignKeyId already exists. Even if I add an ALTER COLUMN myForeignKeyId it just says there is an error on that line.
This seems like it should be a simple enough operation, how can I do this?
After digging some more found quite a simple way to do this, was clearly originally off target.
To add a unique constraint to a column:
ALTER TABLE "myTable"
ADD CONSTRAINT "myUniqueKeyNameOfChoice" UNIQUE ("myColumn");
To remove it:
ALTER TABLE "myTable"
DROP CONSTRAINT "myUniqueKeyNameOfChoice";

Postgres drop exclusion constraint

I ran this DDL to add an exclusion constraint to a table:
ALTER TABLE recurring_charges
ADD EXCLUDE USING GIST (period WITH &&);
Now I want to remove the constraint - how do I do that? I tried some variations of ALTER TABLE ... DROP EXCLUDE and DROP CONSTRAINT but nothing seems to work.
Figured it out - looks like it generated a name for the constraint that I have to use.
ALTER TABLE recurring_charges
DROP CONSTRAINT recurring_charges_period_excl;
And now I've updated my original DDL to use the full ADD CONSTRAINT syntax so that I can name my constraint rather than relying on automatic naming behavior:
ALTER TABLE recurring_charges
ADD CONSTRAINT recurring_charges_period_excl EXCLUDE USING GIST (period WITH &&);
To add to the above answer, to find out the constraint name, you can find it in psql using:
\d+ tableName

Error en eliminacion de llave foranea - wrong in drop foreign key postgresql

I'm using the following query:
alter table hlt_citas drop constraint Ref_hlt_citas_to_hlt_atencion;
When I query the structure of the table, the part of the foreign key is the following:
"Ref_hlt_citas_to_hlt_atencion" FOREIGN KEY (cod_atencion)
REFERENCES hlt_atencion(cod_atencion) ON UPDATE CASCADE ON DELETE RESTRICT
But when I run the query it tells me that it does not exist.
When identifiers like column or constraint names are given in double quotes, the identifiers become case-sensitive. You should then also use double-quotes in queries to maintain the case-sensitivity:
alter table hlt_citas drop constraint "Ref_hlt_citas_to_hlt_atencion";

T-SQL create table with primary keys

Hello I wan to create a new table based on another one and create primary keys as well.
Currently this is how I'm doing it. Table B has no primary keys defined. But I would like to create them in table A. Is there a way using this select top 0 statement to do that? Or do I need to do an ALTER TABLE after I created tableA?
Thanks
select TOP 0 *
INTO [tableA]
FROM [tableB]
SELECT INTO does not support copying any of the indexes, constraints, triggers or even computed columns and other table properties, aside from the IDENTITY property (as long as you don't apply an expression to the IDENTITY column.
So, you will have to add the constraints after the table has been created and populated.
The short answer is NO. SELECT INTO will always create a HEAP table and, according to Books Online:
Indexes, constraints, and triggers defined in the source table are not
transferred to the new table, nor can they be specified in the
SELECT...INTO statement. If these objects are required, you must
create them after executing the SELECT...INTO statement.
So, after executing SELECT INTO you need to execute an ALTER TABLE or CREATE UNIQUE INDEX in order to add a primary key.
Also, if dbo.TableB does not already have an IDENTITY column (or if it does and you want to leave it out for some reason), and you need to create an artificial primary key column (rather than use an existing column in dbo.TableB to serve as the new primary key), you could use the IDENTITY function to create a candidate key column. But you still have to add the constraint to TableA after the fact to make it a primary key, since just the IDENTITY function/property alone does not make it so.
-- This statement will create a HEAP table
SELECT Col1, Col2, IDENTITY(INT,1,1) Col3
INTO dbo.MyTable
FROM dbo.AnotherTable;
-- This statement will create a clustered PK
ALTER TABLE dbo.MyTable
ADD CONSTRAINT PK_MyTable_Col3 PRIMARY KEY (Col3);

T-SQL foreign key check constraint

When you create a foreign key constraint in a table and you create the script in MS SQL Management Studio, it looks like this.
ALTER TABLE T1 WITH CHECK ADD CONSTRAINT FK_T1 FOREIGN KEY(project_id)
REFERENCES T2 (project_id)
GO
ALTER TABLE T1 CHECK CONSTRAINT FK_T1
GO
What I don't understand is what purpose has the second alter with check constraint.
Isn't creating the FK constraint enough? Do you have to add the check constraint to assure reference integrity ?
Another question: how would it look like then when you'd write it directly in the column definition?
CREATE TABLE T1 (
my_column INT NOT NULL CONSTRAINT FK_T1 REFERENCES T2(my_column)
)
Isn't this enough?
First it creates the constraint and here you can specify whether data allready in the table should be checked or not against your new constraint. WITH { CHECK | NOCHECK }
The second part specifies that the constraint is enabled. ALTER TABLE TableName { CHECK | NOCHECK } CONSTRAINT ConstraintName
The second statement is compelled by the "WITH CHECK" in the first statement. There is a setting you can toggle to not do this.