![]() In this section, we will learn how to add or drop a FOREIGN KEY CONSTRAINT in Postgres Using ALTER TABLE command. How to Drop a FOREIGN KEY CONSTRAINT in Postgres?Ī FOREIGN KEY is a column that points to the PRIMARY KEY of some other Postgres table. The output clarifies that the primary key constraint has been removed successfully. Let’s verify the constraint deletion via the below command: SELECT * FROM staff_bio In this coding example, we dropped a primary key constraint named st_id_pk from the staff_bio table: The DROP CONSTRAINT clause can be used in conjunction with ALTER TABLE to drop a primary key constraint from a Postgres table. This way, you can add a primary key to any table’s column. Let’s validate the table’s structure via the following command: SELECT * FROM staff_bio The “ALTER TABLE” message in the output window proves that the “staff_bio” table has been modified successfully. The “st_id” represents a primary key column. The “st_id_pk” represents the name of the primary key. ![]() The “staff_bio” is a table to be altered. This section will teach you how to add or drop a PRIMARY KEY CONSTRAINT in Postgres Using ALTER TABLE command.įirstly, let’s add a primary key constraint on the “st_id” column using ALTER TABLE command and ADD CONSTRAINT clause: ALTER TABLE staff_bio In Postgres, Primary keys are used to identify a table’s record uniquely. How to Drop a PRIMARY KEY CONSTRAINT in Postgres? The table name staff_bio has been created with four columns: st_id, st_name, st_department, and st_age. Afterward, we will add and drop a specific constraint according to the requirement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |