1.3
1. 删除索引(清理无用索引,优化表性能)
sql
-- 语法
DROP INDEX 索引名 ON 表名;
-- 示例:删除user表的idx_user_name索引
DROP INDEX idx_user_name ON user;
2. 添加外键约束(建立表间关联,保证数据完整性)
sql
-- 语法
ALTER TABLE 从表名 ADD CONSTRAINT 外键名 FOREIGN KEY(从表关联字段) REFERENCES 主表名(主表主键);
-- 示例:给order表加外键,关联user表的主键id
ALTER TABLE order ADD CONSTRAINT fk_order_userid FOREIGN KEY(user_id) REFERENCES user(id);
3. 删除外键约束(解除表间关联)
sql
-- 语法
ALTER TABLE 表名 DROP FOREIGN KEY 外键名;
-- 示例:删除order表的fk_order_userid外键
ALTER TABLE order DROP FOREIGN KEY fk_order_userid;

浙公网安备 33010602011771号