Junior — Middle
What methods can be used to impose constraints on an existing table in a database?
sobes.tech AI
Answer from AI
To impose constraints on an existing table in a database, the ALTER TABLE command is usually used. It allows adding various constraints, such as:
- PRIMARY KEY — a unique identifier for a record.
- FOREIGN KEY — a reference to another table.
- UNIQUE — uniqueness of values in a column.
- CHECK — a condition that values must satisfy.
- NOT NULL — prohibition of storing NULL.
An example of adding a uniqueness constraint:
ALTER TABLE employees
ADD CONSTRAINT unique_email UNIQUE (email);
You can also use migration tools (such as Flyway or Liquibase) to manage database schema changes in your project.