MySQL扩展知识3——约束维护(添加、修改、删除)

xiaoxiao2021-02-28  7

添加、修改

基本语法

ALTER TABLE tbl_name ADD 字段类型(字段名) // 添加 MODIFY 字段名 字段类型 // 修改(重新定义字段)

例子

1)客户号设为主键 alter table customer2 add primary key(customer_id); 2)客户姓名不能为空 alter table customer2 modify name varchar(20) not null default ‘’; 3)邮件不可重复 alter table customer2 add unique (email);

删除约束

基本语法

1. 删除主键约束: alter table 表名 drop primary key 注意: 如果主键有外键指向它,需要先把从表的外键删除。 2. 删除外键约束: alter table 从表名 drop foreign key 外键名字 3. 外键名字可以使用show create table从表名查看 在CONSTRAINT字段 4. 删除唯一(unique)约束: alter table 表名 drop index(字段名)
转载请注明原文地址: https://www.6miu.com/read-1750132.html

最新回复(0)