AUTOMATION OF SQL QUERY IN MYSQL

Hi All,
 I would like to automate this process, should run automatically and the result set(s) Can I do this? If yes, then how?
Thanks!

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA LIKE '<dbs_name>' AND COLUMN_NAME = '<student_Id>' AND DATA_TYPE= 'varchar'

 CREATE TABLE student_id_new LIKE student_id;
 
 ALTER TABLE student_id RENAME student_id_old;

 INSERT INTO student_id_new SELECT * FROM student_id_old;

 ALTER TABLE student_id_new RENAME student_id;


  DROP TABLE student_id_old;
RotebergAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
You could do this using cron and a shell script. But you inevitably will run into issues if there are constraints/indexes/foreign keys in other tables relying on this one.
What is the point for doing this?
Mark BullockQA EngineerCommented:
To run mysql scripts from the command line:
mysql db_name < script.sql > output.tab

Open in new window


Complete info is here: http://dev.mysql.com/doc/refman/5.6/en/mysql.html
RotebergAuthor Commented:
Thank you Arnold for your answer the purpose of this is automate those script instead of running it mannually. @ Mark thank you as well I will check that out
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

arnoldCommented:
I understand that is to automate, the only reason I think to run this is because your ID column/s is set too low such that every so often after entries are deleted, you need to reset.
You are copying data from the old table into the new without changes.
Why not run the alter table and change the column that causes you problems updating other columns in tables that reference thus one.
RotebergAuthor Commented:
Thanks Arnold, I want to convert all student_id that are in var to int  on all affected tables, so you mean I can just ALTER the table and change the column on a production server without going through those process?
arnoldCommented:
that is more risky, but why do you need to run this on a regular basis?
Why not do it once, define the new table based on the the old as you have, and then alter the table setting the particular column to int (you do have to be certain that that columns contents are valid.
then during the insert into newtable select column, column,column, change the type of the column student_id. ONCE AND FOR ALL. make sure you make the adjustment in the data entry side to avoid errors.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.