sql
Add unique key constraint on existing column Sql query
October 4, 2022
When adding a unique key constraint to an existing column in SQL, the column must first be converted to a data type that can hold only unique values. This can be done using the ALTER TABLE command. Once the column has been converted to the appropriate data type, the UNIQUE KEY constraint can be added using the ADD CONSTRAINT command.
ALTER TABLE Employee
ADD UNIQUE (ID);
If you have a table named 'Employee' for example in your database and you want to add UNIQUE Constraint on that column. You can achieve that using the above query
Example: Add unique constraint on product_id columns
Below is a table called "products" that has one of the columns named product_id and we want to add a unique constraint to this column. We can do that using the below SQL query:
ALTER TABLE products
ADD UNIQUE (product_id);
Was this helpful?
Similar Posts
- MySQL Foreign key constraint
- SQL query to copy one column values to another column in same table
- Add Unique Constraints on Column in MySQL
- SQL Server query to get table structure and column information
- SQL Query to delete rows that have different Ids or column values
- SQL Server query to add auto increment field
- Drop table SQL query