Junior — Middle
Kuidas saab kehtestada piiranguid juba olemasolevale tabelile andmebaasis?
sobes.tech AI
Vastus AI-lt
To impose constraints on an existing table in a database, the ALTER TABLE command is usually used. With it, you can add various constraints, for example:
- PRIMARY KEY — unique identifier of the record.
- FOREIGN KEY — reference to another table.
- UNIQUE — uniqueness of the value in the column.
- CHECK — condition that the values must satisfy.
- NOT NULL — prohibition of storing NULL.
Example of adding a uniqueness constraint:
ALTER TABLE employees
ADD CONSTRAINT unique_email UNIQUE (email);
You can also use migration tools (e.g., Flyway or Liquibase) to manage changes to the database schema in the project.