打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
6、约束管理、索引管理、键管理
非空约束
#设置非空约束> alter table testtb modify name varchar(100) not null;#删除非空约束> alter table testtb modify name varchar(100) null;
自动增长
#添加> alter table testtb modify id int auto_increment;> alter table testtb change id id int auto_increment;#自动增长> alter table testtb change id id int;> alter table testtb modify id int;
主键约束
# 添加主键约束> alter table testtb4 add primary key(id);> alter table testtb4 add constraint primary key(id);# 删除主键约束> alter table testtb drop primary key;
唯一键约束
#添加> alter table testtb add unique key(uid);> alter table testtb add unique key uni_test(test);#删除> alter table testtb drop index uni_test;
查看约束
#查看主键/唯一键/外键> select * from information_schema.key_column_usage where table_name='test1';#只查看主键> desc test1#查看test1表中引用了那些表中的主键作为自己的外键> select TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME from information_schema.KEY_COLUMN_USAGE where TABLE_NAME = 'test1' and REFERENCED_TABLE_NAME is not null;#查看test2表的主键被哪些表引用成为外键> select REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME,CONSTRAINT_NAME,TABLE_NAME,COLUMN_NAME from information_schema.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME = 'test2';
外键约束
# 添加> alter table testtb add column tid int default 0 not null;> alter table testtb add constraint testtb_tid_fk foreign key(tid) references testtb2(id);# 删除> alter table test4 drop foreign key test_tid_fk;
索引管理
#添加> alter table testtb add index ind_name(name);create index ind_name on testtb (name(20) desc);create index ind_name on testtb (name(20));create index ind_name on testtb (name);# 联合索引> create index ind_id_name on testtb1 (id,name);> create index ind_id_name on testtb1 (id,name(20));#重建索引> repair table table_name quick;#删除索引> alter table test2 drop index uu_ttt;#查看索引> show index from testtb;> show index from testtb where key_name like 'ind%';
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Oracle与MySQL删除字段时对索引与约束的处理对照
Oracle 常见的五种约束
MySQL 学习笔记 一
主键和唯一索引的区别(转)
db2唯一索引与唯一约束,重复删除唯一约束存储过程
Oracle 添加主键和索引
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服