ADD CONSTRAINT is a SQL command that is used together with ALTER TABLE to add constraints (such as a primary key or foreign key) to an existing table in a SQL database.
The basic syntax of ADD CONSTRAINT is:
ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2);
The above command would add a primary key constraint to the table table_name. We can do more than just add primary keys with ADD CONSTRAINT, but first let’s do a quick review of what constraints are and why they’re useful. (If you’re already a SQL database pro, feel free to skip right to the examples.