Mysql字段操作—增加字段、刪除字段、修改字段名、修改字段類型(約束條件)


1.增加字段:
    alter table   tablename    add   new_field_id   type   not null default '0'; 
    例: 
    alter table mmanapp_mmanmedia add appid_id integer not null default 372;

    增加主鍵:

    alter table  tabelname   add   new_field_id   type    default 0 not nullauto_increment ,add   primary key (new_field_id); 
    增加外鍵:

    在已經存在的字段上增加外鍵約束

    ALTER TABLE yourtablename    ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)    REFERENCES tbl_name (index_col_name, ...)    [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]    [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}] 

 

2.刪除字段:

    alter table    tablename    drop column      colname; 
     例: 
     alter table   mmanapp_mmanmedia    drop column     appid_id;

 

3.修改字段名:

     alter table     tablename    change   old_field_name    new_field_name  old_type;

 

4.修改字段類型:

    alter table     tablename     change    filed_name   filed_name   new_type;  


免責聲明!

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



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