How to create unique and foreign key constraints in MySQL?
To create a unique constraint in MySQL, you can use the following syntax:
ALTER TABLE table_name
ADD CONSTRAINT constraint_name UNIQUE (column_name);
To create a foreign key constraint in MySQL, you can use the following syntax:
ALTER TABLE child_table
ADD CONSTRAINT fk_name
FOREIGN KEY (child_column)
REFERENCES parent_table(parent_column);
Among them, table_name is the name of the table where the unique constraint is to be added, constraint_name is the name of the constraint, column_name is the name of the column where the unique constraint is to be added. child_table is the child table containing a foreign key, fk_name is the name of the foreign key, child_column is the name of the foreign key column in the child table, parent_table is the name of the main table, and parent_column is the name of the column in the main table.