What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Database constraints are rules that protect data from becoming inconsistent. A primary key uniquely identifies each row, a unique constraint prevents duplicate values in another identifier, and a foreign key requires a reference to match an eligible key in another table. The exact behavior depends on the database engine; “Field” is not identified here as a specific database product.

What does a primary key do?

A primary key identifies a row in its table. Its value must be unique and cannot be null. A table has one primary key constraint, though that key can consist of one column or several columns together. PostgreSQL documents primary keys as unique and not null: PostgreSQL 18: Constraints.

How is a unique constraint different?

A unique constraint prevents duplicate values in a column or combination of columns. It can protect a second candidate identifier without making that identifier the table’s primary key. For example, a customer table may use customer_id as its primary key and require each email value to be unique. How a particular engine treats nulls in a unique column can vary, so check its documentation rather than assuming one universal rule. PostgreSQL and Microsoft Access document their respective behaviors: PostgreSQL 18 constraints and Microsoft Access: Create and use indexes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What does a foreign key require?

A foreign key links a value in one table to an eligible key in another table. The value in the referencing, or child, table must match a value in the referenced table. This protects referential integrity: a row cannot point to a parent record that does not exist when the database enforces the constraint. The referenced key is commonly a primary key; some systems also permit an appropriate unique key. Consult the chosen engine’s rules for which targets qualify. See PostgreSQL 18 and SQL Server foreign-key relationships.

A foreign key does not have to be unique. Many child rows can point to the same parent. For instance, many orders can refer to one customer.

How do the three constraints work together?

Consider two tables: Customers(customer_id, email) and Orders(order_id, customer_id). The following rules express their roles:

  • Customers.customer_id is the primary key: each customer has a distinct, non-null identifier.
  • Customers.email has a unique constraint if each email must be distinct.
  • Orders.customer_id is a foreign key to Customers.customer_id, so each order’s customer must exist.

Several orders can use the same customer ID. This is an explanatory relational example, not a description of a particular “Field” product.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What is a composite key?

A composite key uses more than one column. Uniqueness applies to the combination, not necessarily to each column on its own. In a linking table, for example, (product_id, vendor_id) can identify a product-vendor pairing: either value may recur individually, while the same pair cannot recur under the constraint. SQL Server documents this product/vendor pattern in its guidance on creating primary keys.

Rank #3

What happens when a referenced row changes or is deleted?

Foreign-key rules can specify what happens to related child rows when a referenced value is updated or its row is deleted. Depending on the engine and the constraint definition, actions may include cascading the change or setting the reference to null. A delete does not automatically cascade in every database or schema; the action must be supported and selected. PostgreSQL, SQL Server, and Access describe configurable referential actions in their documentation: PostgreSQL, SQL Server, and Microsoft Access.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why does the database engine matter?

Do not assume that declaring a constraint means every product checks it in the same way. PostgreSQL documents constraint enforcement; BigQuery explicitly says its declared primary and foreign key constraints are not enforced, leaving data owners responsible for keeping data conformant. BigQuery notes that such keys are typically used for data integrity and query optimization, but a declaration alone does not reject inconsistent writes. See Google Cloud BigQuery: Use primary and foreign keys.

Other implementation details differ too. SQL Server says creating a foreign key does not automatically create an index on the referencing columns. An index may help joins and constraint checks, but whether to add one depends on the workload. Review the selected engine’s documentation for enforcement, permitted referenced keys, null treatment, referential actions, and indexing behavior before relying on a constraint.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.