Mysql Determines whether a table field or index exists _mysql

Source: Internet
Author: User

To 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 ' D column_name = ' ifupsend ') THEN 
ALTER TABLE rtc_order
ADD column ' ifupsend ' BIT not NULL DEFAULT COMMENT ' Whether or not to upload Upload ';
End IF; 
end// 
DELIMITER; 

To 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_photot Ype ' and index_name = ' index_name ') THEN 
ALTER TABLE ' rtc_phototype ' ADD index index_name (' Imgtype ');
End IF; 
end// 
DELIMITER; 

From these two paragraphs can see a lot of things, you can test yourself

About the small series to introduce the MySQL Judge table field or whether the index exists content to introduce to everyone here, I hope to 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.