I have in my MYSQL table a unique key and i want to add to it.
UNIQUE KEY `user_id` (`user_id`,`account_id`)
and i want to add another
UNIQUE KEY `user_id` (`user_id`,`account_id`,`pet_id`)
Sometimes we want to add a unique key to the column of an existing table; then, this statement is used to add the unique key for that column. Following are the syntax of the ALTER TABLE statement to add a unique key: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(column_list);
To modify a unique constraint In the Object Explorer, right-click the table containing the unique constraint and select Design. On the Table Designer menu, click Indexes/Keys.... In the Indexes/Keys dialog box, under Selected Primary/Unique Key or Index, select the constraint you wish to edit.
We can define multiple Unique keys on a table where one or more columns combine to make a Unique key. According to ANSI, we can use multiple NULL values but in the SQL server, we can add only one NULL value.
The syntax for creating a unique constraint using an ALTER TABLE statement in SQL Server is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.
ALTER TABLE your_table DROP INDEX user_id, ADD UNIQUE KEY `user_id` (`user_id`,`account_id`,`pet_id`)
Note: You won't need the backticks around the column names if you're using mariadb on Linux - in fact it will throw an syntax error 1064/(42000)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With