It’s going to be enforced by a unique index in the same manner and can be references by a foreign key constraint (which will be covered later in this article.) However, the primary key constraint is typically the columns used in a foreign key relationship, and the columns of a primary key may not allow NULL values. Primary Key ConstraintsĪ primary key constraint is effectively the same as a unique constraint, with a few minor, but important differences. Of course, this is wasted space for the duplicated column and the extra index, plus the need to find an impossible value for uniqueval (and probably add a constraint to make sure that it is truly impossible.) However, almost always, implementation cost and complexity are going to be less important than data quality. ![]() You can also do the same thing using a slightly different syntax: However, it will be marked and listed as a constraint as well as an index: The advantage to the queries that are executed is going to be the same either way. This helps to signal to people tuning your database’s queries that the index could be removed without harm, but the constraint is part of the core business rules for the database. My rule of thumb is that a UNIQUE constraint is there to enforce that the values in a table’s column are different from one another. However, the documentation that using the unique constraint implies to the database might be important. The question then becomes, why create a unique constraint at all? Why not just create a unique index? Well, you can truly do it either way. ![]() ![]() This is familiar to me as a SQL Server user. The way that PostgreSQL satisfies the unique criteria for the column is by creating a unique b-tree index. Running this query results in the following:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |