Question

I had made a table :

id int,
name varchar(50),
addr varchar(50),
location varchar(20)

Now i want to make addr column to be PRIMARY KEY COLUMN. I tried a script:

alter table aaa
add constraint pk_addr primary key(addr)

But it shows an error and the data in that column is also NOT NULL and UNIQUE.My question is that can we make a varchar column to be primary key,If yes then the above alter column script is correct or not.

Était-ce utile?

La solution

you have to specify that column aaa is not nullable:

create table aaa (id int, name varchar(50), addr varchar(50), location varchar(20));

alter table aaa alter column addr varchar(50) not null;
alter table aaa add constraint pk_addr primary key(addr);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top