Normally, we add the primary key to a table when we define the table’s structure using CREATE TABLE statement. Define primary key when creating the table When you add a primary key to a table, PostgreSQL creates a unique B-tree index on the column or a group of columns used to define the primary key. It is a good practice to add a primary key to every table. Technically, a primary key constraint is the combination of a not-null constraint and a UNIQUE constraint.Ī table can have one and only one primary key. You define primary keys through primary key constraints. If we are working with a large or busy database, or we just want to have some extra assurances that we will not lose data, I prefer simply adding a new column rather than changing the type of an existing one.Summary: in this tutorial, we will show you what the primary key is and how to manage PostgreSQL primary key constraints through SQL statements.Ī primary key is a column or a group of columns used to identify a row uniquely in a table. An Alternative to Changing Column Type in PostgreSQL - Adding a New Column InsteadĬhanging a column type is fairly simple, but for a large database with millions of records and a lot of concurrent queries it is also very expensive because of the ACCESS EXCLUSIVE table lock. Honestly it’s hard to know for certain if a table lock will be required by just looking through documentation.įear not, there is an alternative.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |