+ 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.

28th Nov 2018, 4:36 AM
Isaac
Isaac - avatar
3 Respostas
+ 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
28th Nov 2018, 6:37 AM
Taste
Taste - avatar
+ 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.
28th Nov 2018, 7:13 AM
David Carroll
David Carroll - avatar
+ 1
thankss!
28th Nov 2018, 4:35 PM
Isaac
Isaac - avatar