+ 1
SQL insert duplicate
Hello so I have INSERT INTO CITY_T( City,Population) Values (Miami, 100000); My primary key is City and I don’t want to be able to add duplicates.
3 Antworten
+ 4
You should declare city as primary key when creating the table
create table `a`(city varchar(30) primary key)
But because the table already created you'll need to alter the column to act as PK
alter table `city_t` add primary key(`city`)
If error happen maybe the column nees to be set to not null
Alter table `city_t` modify `city` varchar(30) not null
+ 3
Taste... Nice, solid answer.
I'd also recommend applying a unique index on City and adding an autoincrementing integer column as the PK. It's generally not a good practice to use columns with meaningful data as primary keys for various reasons.
+ 1
thankss!