This question already has answers here:
Drop foreign keys generally in POSTGRES
(3 answers)
Closed 3 years ago.
I have created a constraint this way:
ALTER TABLE varaus ADD FOREIGN KEY ( varaus_id ) REFERENCES kayttaja ( id );
But when trying to drop it:
ALTER TABLE varaus DROP CONSTRAINT varaus_id;
I get the error:
ERROR: constraint "varaus_id" of relation "varaus" does not exist
I have also tried:
ALTER TABLE varaus DROP CONSTRAINT varaus_fkey;
ALTER TABLE varaus DROP CONSTRAINT id;
You have to find the name of constraint first using below query -
select constraint_name
from information_schema.table_constraints
where table_schema = 'your_schema_name'
and table_name='varaus'
and constraint_name like 'fk_%'
Then use those names in below query -
ALTER TABLE varaus DROP CONSTRAINT constraint_name
Good example of why you should always name your constraints instead letting the DBMS generate a name. So instead of the initial creation of the FK use (for example)
alter table varaus add
constraint var2kay_fk
foreign key ( varaus_id )
references kayttaja ( id );
Related
I have created 5 tables, with four of them having a Primary Key. I then try to assign Foreign Keys using the following alter statement:
ALTER TABLE SensorLocation
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensor_id)
REFERENCES Sensor (sensor_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensorlocation_id)
REFERENCES SensorLocation (sensorlocation_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (property_id)
REFERENCES ObservedProperty (property_id)
;
ALTER TABLE Observation
ADD CONSTRAINT constraint_name
FOREIGN KEY (observation_id)
REFERENCES DataStream (observation_id)
;
The thing is, when I only use one constraint per table, it works. But when I want to assign multiple to the table DataStream it doesn't work.
The error message I receive is as follows:
constraint "constraint_name" for relation "datastream" already exists
Does anyone have any idea on how to solve this? All help is welcome!
This is our data-structure:
I created a table in PostgreSQL with a foreign key constraint.
I dropped the table to which the foreign key belongs. Now how to alter the table or how to defer the foreign key present in the table?
To clarify:
I have a table named test. It has a column called subjectName, which is a foreign key of subject Table. Now I dropped subject table. How to remove the FK constaint on table test
Assuming the following tables:
create table subject
(
name varchar(10) primary key
);
create table test
(
some_column integer,
subject_name varchar(10) not null references subject
);
there are two scenarios what could have happened when you dropped the table subject:
1. you didn't actually drop it:
drop table subject;
ERROR: cannot drop table subject because other objects depend on it
Detail: constraint test_subject_name_fkey on table test depends on table subject
Hint: Use DROP ... CASCADE to drop the dependent objects too.
2. you did drop it, then the foreign key is gone as well.
drop table subject cascade;
NOTICE: drop cascades to constraint test_subject_name_fkey on table test
which tells you that the foreign key constraint was automatically dropped.
Perhaps your question in not exactly what you mean. Are you wanting to remove the which was a foreign key from the table. As amply indicated if you dropped the parent table then the FK is also dropped. However the column itself is not dropped from the child table. To remove that you need to alter the table.
alter table test drop column subject_name;
See demo here
I cannot drop a constraint in postgres even using adminer
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
Error in query : ERROR: constraint "constraint_name" of relation "table_name" does not exist
but if I create a new one like :
ALTER TABLE table_name
ADD CONSTRAINT my_new_constraint(column1, column2, ... column_n);
Then it works and I can drop it.
The one who made the constraint I try to drop did it this way two years ago:
create unique index constraint_name on table_name (column1,lower(column2),coalesce(deleted_at,\'19000101\')
If anyone has got any idea to drop this constraint?
CREATE UNIQUE INDEX creates an index that needs to be dropped with DROP INDEX, not a table constraint.
I created a table in PostgreSQL like this:
CREATE TABLE Table1 (
Id varchar(100) PRIMARY KEY CHECK (Id ~ '^[a-z0-9]{3,15}$'),
...
);
This will automatically create a constraint called table1_id_check.
Now I would like to change the check constraint to
(Id ~ '^[a-z0-9]{3,}$')
How can I do this in PostgreSQL as a single statement without dropping the constraint and recreating it again?
Using multiple statements within a transaction works on all SQL dbms that support using this DDL in a transaction.
begin transaction;
alter table table1
drop constraint table1_id_check;
alter table table1
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
commit;
PostgreSQL lets you use multiple clauses within an ALTER TABLE statement.
alter table table1
drop constraint table1_id_check,
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
I have a column in db which has 5 columns but no primary key.
One of the columns is named myTable_id and is integer.
I want to check if the table has a primary key column. If it doesn't, then make myTable_id a primary key column and make it identity column. Is there a way to do this?
I tried with this:
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)
and I get syntax error in Management studio.
This checks if primary key exists, if not it is created
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = 'Persons'
AND TABLE_SCHEMA ='dbo')
BEGIN
ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id)
END
ELSE
BEGIN
-- Key exists
END
fiddle: http://sqlfiddle.com/#!6/e165d/2
ALTER TABLE Persons
ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id)
An IDENTITY constraint can't be added to an existing column, so how you add this needs to be your initial thought. There are two options:
Create a new table including a primary key with identity and drop the existing table
Create a new primary key column with identity and drop the existing 'P_ID' column
There is a third way, which is a better approach for very large tables via the ALTER TABLE...SWITCH statement. See Adding an IDENTITY to an existing column for an example of each. In answer to this question, if the table isn't too large, I recommend running the following:
-- Check that the table/column exist and no primary key is already on the table.
IF COL_LENGTH('PERSONS','P_ID') IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = 'PERSONS')
-- Add table schema to the WHERE clause above e.g. AND TABLE_SCHEMA ='dbo'
BEGIN
ALTER TABLE PERSONS
ADD P_ID_new int IDENTITY(1, 1)
GO
ALTER TABLE PERSONS
DROP COLUMN P_ID
GO
EXEC sp_rename 'PERSONS.P_ID_new', 'P_ID', 'Column'
GO
ALTER TABLE PERSONS
ADD CONSTRAINT PK_P_ID PRIMARY KEY CLUSTERED (P_ID)
GO
END
Notes:
By explicitly using the CONSTRAINT keyword the primary key constraint is given a particular name rather than depending on SQL Server to auto-assign a name.
Only include CLUSTERED on the PRIMARY KEY if the balance of searches for a particular P_ID and the amount of writing outweighs the benefits of clustering the table by some other index. See Create SQL IDENTITY as PRIMARY KEY.
You can check if primary key exists or not using OBJECTPROPERTY Transact SQL, use 'TableHasPrimaryKey' for the second arguments.
DECLARE #ISHASPRIMARYKEY INT;
SELECT #ISHASPRIMARYKEY = OBJECTPROPERTY(OBJECT_ID('PERSONS'), 'TABLEHASPRIMARYKEY');
IF #ISHASPRIMARYKEY IS NULL
BEGIN
-- generate identity column
ALTER TABLE PERSONS
DROP COLUMN P_ID;
ALTER TABLE PERSONS
ADD P_ID INT IDENTITY(1,1);
-- add primary key
ALTER TABLE PERSONS
ADD CONSTRAINT PK_PERSONID PRIMARY KEY (P_ID);
END;
I don't think you can do that. For making a column into an identity column I think you have to drop the table entirely.