Mysql determines whether a table field or index exists, and mysql determines a field Index

Source: Internet
Author: User

Mysql determines whether a table field or index exists, and mysql determines a field Index

Determine whether a field exists:

Drop procedure if exists schema_change; DELIMITER // create procedure schema_change () begin declare CurrentDatabase VARCHAR (); select database () INTO CurrentDatabase; if not exists (SELECT * FROM information_schema.columns WHERE table_schema = CurrentDatabase AND table_name = 'rtc _ order' AND column_name = 'ifupsend ') then alter table rtc_orderADD COLUMN 'ifupsend' bit not null default comment' whether to upload or NOT '; end if; END // DELIMITER; CALL schema_change ();

Determine whether an index exists:

DROP PROCEDURE IF EXISTS schema_change; DELIMITER //CREATE PROCEDURE schema_change() BEGIN DECLARE CurrentDatabase VARCHAR();SELECT DATABASE() INTO CurrentDatabase;IF NOT EXISTS (SELECT * FROM information_schema.statistics WHERE table_schema=CurrentDatabase AND table_name = 'rtc_phototype' AND index_name = 'index_name') THEN ALTER TABLE `rtc_Phototype` ADD INDEX index_name ( `imgtype` );END IF; END// DELIMITER ; CALL schema_change(); 

We can see a lot of things from these two paragraphs. You can test them by yourself.

I would like to introduce you to Mysql's content about how to determine whether a table field or index exists. I hope it will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.