Sobes.tech
Junior — Middle

Explain what data integrity constraints are in SQL and how they are used.

sobes.tech AI

Answer from AI

Data integrity constraints in SQL are rules that ensure the correctness and consistency of data in a database.

Main types of constraints:

  • PRIMARY KEY — a unique identifier for a row.
  • FOREIGN KEY — a reference to a key from another table, ensuring the relationship and integrity between tables.
  • UNIQUE — guarantees the uniqueness of a value in a column.
  • NOT NULL — prohibits storing empty values.
  • CHECK — verifies a condition for a value.

Example of using a constraint:

CREATE TABLE users (
    id INT PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    age INT CHECK (age >= 18)
);

Constraints help prevent errors, such as data duplication or mismatch of relationships between tables.

Explain what data integrity constraints are in SQL… - sobes.tech