How do I use ALTER TABLE
to add a new column and make it unique?
相关问题
- SQL join to get the cartesian product of 2 columns
- sql execution latency when assign to a variable
- Difference between Types.INTEGER and Types.NULL in
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
if table is empty
If you have data in table you need to this in three steps:
It is a two step process: add the new coloumn, then add the constraint. Because UNIQUE constraints permit nulls it doesn't matter whether the table is populated:
Depends on the DBMS, but I think the following is quite portable:
If you want to give a name to the
UNIQUE
constraint, you could replace the last command with this:You can do it with a single SQL statement (at least with MySQL):