Add primary key to PostgreSQL table only if it does not exist

You could do something like the following, however it is better to include it in the create table as a_horse_with_no_name suggests. if NOT exists (select constraint_name from information_schema.table_constraints where table_name=”table_name” and constraint_type=”PRIMARY KEY”) then ALTER TABLE table_name ADD PRIMARY KEY (id); end if;

SQLite Modify Column

That’s one of the better-known drawbacks of SQLite (no MODIFY COLUMN support on ALTER TABLE), but it’s on the list of SQL features that SQLite does not implement. edit: Removed bit that mentioned it may being supported in a future release as the page was updated to indicate that is no longer the case

ALTER TABLE on dependent column

I believe that you will have to drop the foreign key constraints first. Then update all of the appropriate tables and remap them as they were. ALTER TABLE [dbo.Details_tbl] DROP CONSTRAINT [FK_Details_tbl_User_tbl]; — Perform more appropriate alters ALTER TABLE [dbo.Details_tbl] ADD FOREIGN KEY (FK_Details_tbl_User_tbl) REFERENCES User_tbl(appId); — Perform all appropriate alters to bring the key …

Read more