1、创建数据库表,createtabletest_users(user_idbigint,user_namevarchar(100))
2、查看系统视图tables,在系统视图中可以查到刚建的数据表,select*frominformation_schema.tablestwheretable_name='test_users',
3、查看系统视图columns,在系统视图中可以查到该表所有的字段,select*frominformation_schema.columnstwheretable_name='test_users',
4、查询表中不存在的字段,执行无返回结果,
select*frominformation_schema.columnst
wheretable_name='test_users'
andcolumn_name='user_id2'
很多公司都要求再生产上打得sql脚本允许反复执行(防止某一个sql报错以后要拎出来执行)。
所以就产生了需要先判断索引是否存在,再做添加索引或者删除索引的 *** 作(若索引不存在,添加或删除索引会报错)。实例如下:
drop PROCEDURE if EXISTS add_index
DELIMITER //
create PROCEDURE add_index()
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='Prod.Oms.OmsToSgGateway' AND table_name = 'Oms.OmsToSgGateway.IntermeDiate' AND index_name = 'index_GW_Query') then
ALTER TABLE `Prod.Oms.OmsToSgGateway`.`Oms.OmsToSgGateway.IntermeDiate` ADD INDEX `index_GW_Query`(`ResourceName`, `Category`, `ResourceType`) USING BTREE COMMENT '增加国网数据检索效率'
END IF
IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema='Prod.Oms.OmsToSgGateway' AND table_name = 'Oms.OmsToSgGateway.IntermeDiate' AND index_name = 'index_ResourceId') then
ALTER TABLE `Prod.Oms.OmsToSgGateway`.`Oms.OmsToSgGateway.IntermeDiate` ADD INDEX `index_ResourceId`(`ResourceId`) USING BTREE COMMENT '源始id'
END IF
END
//
DELIMITER
call add_index()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)