mysql修改表主鍵並設置自增


1、主建才可設置自增【如果沒有設置主鍵可以使用這個語句】
    1)設置主鍵,id為表字段
  alter table 表名 add primary key (id);
  如果該字段存在重復的記錄需要先刪除,才能設置主鍵

    2)設置自增:id為表字段
  alter table 表名 change id id int not null auto_increment ;

2、設置了主鍵,並且自增后,插入語句

    1)當id為主鍵時,插入帶id,則需要設置value為null
  insert into teacher values(null,'11');
    2)否則需要指定插入的字段:
  insert into teacher(name) values('11');
    3)也可如下(注意該id必須不重復,否則無法插入成功)
  insert into teacher(id,name) values(11,'11');




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM