Mysql – Execute Mysql queries using the same file more than once

MySQL

I have a file with .sql extension in which I write a query like insertions, deletions or updates, and execute it in toadformysql . I repeat the same action many times because I have lot of queries, so it turned out that I have a lot .sql files.
for the first query It works . but When I tried to add a second query in the same file and execute it , there are errors because the first query has already been executed. If the first query is delete for example, it displays an error "no such column" , which is logic beacuse I already delete the column.
Is there a way that I can have a single file in which I add all my queries and while executing it , I won't have errors from old queries like duplicates or others, something like errors handling. It is because I have to keep an history of all queries.
Only the query that I didn't already execute will throw an error if there is.

for example if my first query is

ALTER TABLE adbproject DROP COLUMN imageFormat

and I execute it. for the second time I want to add another query which is:

ALTER TABLE PERSON ADD MATRICULE VARCHAR(50) AFTER CODE;

So the file to be executed line's will be :

ALTER TABLE adbproject DROP COLUMN imageFormat;
ALTER TABLE PERSON ADD MATRICULE VARCHAR(50) AFTER CODE;

but I have logically this error : Can't DROP 'imageFormat'; check that column/key exists. I am searching a way to avoid this error.
Thanks in advance

Best Answer

The problem is, you should not be trying to create an ever-growing set of queries in this manner.

Of course the first step in your example fails on the second run, since the imageFormat column has already been dropped. There is nothing to restore imageFormatand there is no error handling to cope with the missing column.

I cannot fathom why you would want to build up your queries in such a manner.

If your goal is to record every query that you run, then insert the code into a table that records the time and the code that you run.

But do not expect to run all the commands without errors unless you have scripted the code to also deal with each exception that may arise.