Home > Back-end >  how to change primary key from null to not null in mysql?
how to change primary key from null to not null in mysql?

Time:12-17

I have the following table

create table ooor (
 id int(10) PRIMARY KEY,
 name varcha(20),
 city varchar(20) UNIQUE KEY
                  );

and its primary key value shown is null and i want its value to change to not null without deleting table.
so , how can i do it ??

from

 ------- ------------- ------ ----- --------- ------- 
| Field | Type        | Null | Key | Default | Extra |
 ------- ------------- ------ ----- --------- ------- 
| id    | int         | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| city  | varchar(20) | YES  | UNI | NULL    |       |
 ------- ------------- ------ ----- --------- ------- 

to

 ------- ------------- ------ ----- --------- ------- 
| Field | Type        | Null | Key | Default | Extra |
 ------- ------------- ------ ----- --------- ------- 
| id    | int         | NO   | PRI | NOT NULL|       |
| name  | varchar(20) | YES  |     | NULL    |       |
| city  | varchar(20) | YES  | UNI | NULL    |       |
 ------- ------------- ------ ----- --------- ------- 

note: i know its the column which has primary key value is not null by default , i want to change its wriiten value (NULL to NOT NULL).

CodePudding user response:

Using Heidi Sql to actually do what you just said, this was the command executed to accomplish what you're asking. This doesn't require that you remake the table or anything. It simply changed the table where it is.

ALTER TABLE `ooor`
CHANGE COLUMN `id` `id` INT(50) NOT NULL
  • Related