Junior — Middle
Kas oleksite võimeline selgitama, mida SQL-i piirangud tähendavad ja kuidas neid rakendatakse?
sobes.tech AI
Vastus AI-lt
Constraints in SQL are rules applied to table columns to ensure data integrity and correctness of information. They help prevent errors and inconsistencies.
Main types of constraints:
- PRIMARY KEY — a unique identifier for the row, cannot be NULL.
- FOREIGN KEY — ensures referential integrity between tables.
- UNIQUE — guarantees the uniqueness of values in the column.
- NOT NULL — prohibits storing empty values.
- CHECK — sets a condition that data must satisfy.
Example of creating a table with constraints:
CREATE TABLE Users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
age INT CHECK (age >= 18)
);
Here, we guarantee that each user will have a unique id and email, and that the age will not be less than 18.