#设置非空约束> 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%';
联系客服