Add a primary key to the MySQL field and set it to grow automatically

Let me talk about how to set the primary key. Here I set the primary key for the num field in the stuinfo table.

ALTER TABLE stuinfo 
add primary key auto_increment(num);

The prerequisite for setting auto-increment for a field is that you have to have a word key. If you don't set it for any field, you can't set it. Then set up auto-increment. Also note that you must plan all the previous formats. Like this~

alter table stuinfo 
modify num int(5) not null auto_increment;

If you want to set the primary key and self-growth at the same time, you can refer to the following writing

 alter table stuinfo 
 modify num int(5) not null auto_increment,
 add primary key(num);

If you want to set the primary key and self-growth when creating the table, just write it after the type.

Guess you like

Origin blog.csdn.net/qq_44386537/article/details/105220895