ALTER主要用于修改表结构:

    1. 1.修改表名
    2. alter table `旧表名` rename `新表名`;
    3. 2.修改表中的字段名(数据类型必须填)
    4. ALTER TABLE `表名` change `旧字段名` `新字段名` 数据类型;
    5. 3.修改表中的字段的数据类型
    6. ALTER TABLE `表名` MODIFY `字段名` 新数据类型;
    7. 4.修改表中的字段名+数据类型:
    8. ALTER TABLE `表名` change `旧字段名` `新字段名` 新数据类型;
    9. 5.在表中新增一个字段(如果不加first,排列顺序默认为表的最后一列字段)
    10. ALTER TABLE `表名` ADD `字段名` 数据类型
    11. [not null unique primary key default '默认值' COMMENT '注释'] -- 非必填项
    12. [first] -- 将新增字段的位置设置成表的第一列
    13. [after `字段A`] -- 将新增字段的位置设置为字段A的后面;
    14. 6.修改表中字段的排列顺序
    15. ALTER TABLE `表名` MODIFY `字段名` 数据类型
    16. [FIRST] -- 将字段的位置修改成表的第一列
    17. [AFTER `字段A`] -- 将字段的位置修改为字段A的后面;
    18. 7.删除表中的一个字段
    19. ALTER TABLE `表名` DROP `字段名`;

    -7ab41fd6c244a070.jpg

    1. -- 家庭户籍表: `family`
    2. ALTER TABLE `family` add `FK_FPID` int(15) null COMMENT '外键: 标识该家庭属于哪种贫困类型';
    3. ALTER TABLE `family` add constraint FOREIGN KEY ( `FK_FPID` ) references `family_pool` ( `FP_ID` );
    4. ALTER TABLE `family` add `FK_VGID` int(15) null COMMENT '外键: 标识该家庭属于哪个村组';
    5. ALTER TABLE `family` add CONSTRAINT FOREIGN KEY ( `FK_VGID` ) REFERENCES `village_group` ( `VG_ID` );
    6. -- 村民表: `villager`
    7. ALTER TABLE `villager` add `FK_RID` int(15) null COMMENT '外键: 标识村民的角色';
    8. ALTER TABLE `villager` add CONSTRAINT FOREIGN KEY ( `FK_RID` ) REFERENCES `villager_role` ( `R_ID` );
    9. ALTER TABLE `villager` add `FK_DLID` int(15) null COMMENT '外键: 如果村民残疾,标识其残疾级别';
    10. ALTER TABLE `villager` add CONSTRAINT FOREIGN KEY ( `FK_DLID` ) REFERENCES `disability_level` ( `DL_ID` );
    11. ALTER TABLE `villager` add `FK_VID` int(15) null COMMENT '外键: 户主(村民)ID ,标识村民所属哪个家庭';
    12. ALTER TABLE `villager` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `family` ( `V_ID` );
    13. ALTER TABLE `villager` add `FK_FRID` int(15) null COMMENT '外键: 标识村民与户主的血缘关系';
    14. ALTER TABLE `villager` add CONSTRAINT FOREIGN KEY ( `FK_FRID` ) REFERENCES `family_relation` ( `FR_ID` );
    15. -- 村组信息表`village_group`
    16. ALTER TABLE `village_group` add `FK_VID` int(15) null COMMENT '外键: 标识该村组的村组干部是哪个村民';
    17. ALTER TABLE `village_group` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `villager` ( `V_ID` );
    18. -- 村民工作信息表`job_info`
    19. ALTER TABLE `job_info` add `FK_VID` int(15) null COMMENT '外键: 标识这行工作信息元组属于哪个村民';
    20. ALTER TABLE `job_info` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `villager` ( `V_ID` );
    21. -- 村民患病信息表`illness_info`
    22. ALTER TABLE `illness_info` add `FK_VID` int(15) null COMMENT '外键: 标识这行患病信息元组属于哪个村民';
    23. ALTER TABLE `illness_info` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `villager` ( `V_ID` );
    24. -- 残疾补贴表`disability_payments`
    25. ALTER TABLE `disability_payments` add `FK_VID` int(15) null COMMENT '外键: 标识这行残疾补贴领取记录属于哪个村民';
    26. ALTER TABLE `disability_payments` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `villager` ( `V_ID` );
    27. -- 养老补贴表`pension_subsidy`
    28. ALTER TABLE `pension_subsidy` add `FK_VID` int(15) null COMMENT '外键: 标识这行养老补贴领取记录属于哪个村民';
    29. ALTER TABLE `pension_subsidy` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `villager` ( `V_ID` );
    30. -- 家庭年收支表`income_expenditure`
    31. ALTER TABLE `income_expenditure` add `FK_VID` int(15) null COMMENT '外键: 标识这行家庭年收支调查记录属于哪一户家庭';
    32. ALTER TABLE `income_expenditure` add CONSTRAINT FOREIGN KEY ( `FK_VID` ) REFERENCES `family` ( `V_ID` );
    33. -- 村民保障表`villager_ensure`
    34. -- 11: 主键即外键
    35. ALTER TABLE `villager_ensure` add CONSTRAINT FOREIGN KEY ( `V_ID` ) REFERENCES `villager` ( `V_ID` );
    36. -- 家庭保障表 `family_ensure`
    37. -- 11: 主键即外键
    38. ALTER TABLE `family_ensure` add CONSTRAINT FOREIGN KEY ( `V_ID` ) REFERENCES `family` ( `V_ID` );
    39. -- 土地所属详情表`family_land`
    40. -- 多对多: 主键即外键
    41. ALTER TABLE `family_land` add CONSTRAINT PRIMARY KEY ( `V_ID`,`L_ID` );
    42. ALTER TABLE `family_land` add CONSTRAINT FOREIGN KEY ( `V_ID` ) REFERENCES `family` ( `V_ID` );
    43. ALTER TABLE `family_land` add CONSTRAINT FOREIGN KEY ( `L_ID` ) REFERENCES `land` ( `L_ID` );
    44. -- 种植类型详情表`family_plant`
    45. -- 多对多: 主键即外键
    46. ALTER TABLE `family_plant` add CONSTRAINT PRIMARY KEY ( `V_ID`,`P_ID` );
    47. ALTER TABLE `family_plant` add CONSTRAINT FOREIGN KEY ( `V_ID` ) REFERENCES `family` ( `V_ID` );
    48. ALTER TABLE `family_plant` add CONSTRAINT FOREIGN KEY ( `P_ID` ) REFERENCES `plant` ( `P_ID` );
    49. -- 养殖类型详情表`family_breed`
    50. -- 多对多: 主键即外键
    51. ALTER TABLE `family_breed` add CONSTRAINT PRIMARY KEY ( `V_ID`,`B_ID` );
    52. ALTER TABLE `family_breed` add CONSTRAINT FOREIGN KEY ( `V_ID` ) REFERENCES `family` ( `V_ID` );
    53. ALTER TABLE `family_breed` add CONSTRAINT FOREIGN KEY ( `B_ID` ) REFERENCES `breed` ( `B_ID` );