tags:

views:

41

answers:

3

In MySQL, is it possible to append default columns after creation or create them automatically? A brief overview is this:

All tables must have 5 fields that are standardized across our databases (created_on, created_by, row_status etc). Its sometimes hard for developers to remember to do this and/or not done uniformly. Going forward we'd like to automate the task some how. Does anyone know if its possible to create some sort of internal mysql script that will automatically append a set of columns to a table?

After reading through some responses, I think i'd rephrase the question bit, rather than making it an autoamtic task (i.e EVERY Table), make it function that can be user-triggered to go through and check for said columns and if not add them. I'm pretty confident this is out of SQL's scope and would require a scripting language to do, not a huge issue but it had been preferable to keep things encapsulated within SQL.

+1  A: 

i would say first - don't do that. make an audit table seperately - and link with triggers.

otherwise, you will need to feed your table construction through a procedure or other application that will create what you want.

Randy
The columns the OP listed are typical for handling concurrency; an audit table is for showing all the history...
OMG Ponies
OMG Ponies is right, though in a certain scenario one could use a master table to hold the information i'm looking for and hold columns to reference both the table name and primary id...still not the most ideal situation. I'm not sure how such a table would be efficiency wise especially as we're anticipating large scaling up.
CogitoErgoSum
A: 

I'd first defer to Randy's answer - this info is probably better extracted elsewhere.

That said, if you're set on adding the columns, ALTER TABLE is probably what you're looking for. You might consider also including some extra logic to determine which columns are missing for each table.

Jimmy
At that route, one migth as well create a script PHP based perhaps that is run after tables are created and ensure the required columns are there..if not, add them. I did debate this route but I was hoping to know within SQL if there was a more direct route.
CogitoErgoSum
+1  A: 

I'm not very aware of MySQL specific data modeling tools, but there's no infrastructure to add columns to every table ever created in a database. Making this an automatic behavior would get messy too, when you think about situations where someone added the columns but there were typos. Or what if you have tables that are allowed to go against business practice (the columns you listed would typically be worthless on code tables)...

Development environments are difficult to control, but the best means of controlling this is by delegating the responsibility & permissions to as few people as possible. IE: There may be 5 developers, but only one of them can apply scripts to TEST/PROD/etc so it's their responsibility to review the table scripts for correctness.

OMG Ponies
You do have a good point here. I suppose I'd rephrase to ask if there was a way to make an internal function to add the said columns so that it wasn't automatically triggered, but an option to more rapidly add the said columns
CogitoErgoSum
@CogitoErgoSum: Yes, you can create a stored procedure in MySQL, but because you would have to pass the table name as a parameter - you'd have to use MySQL Prepared Statements (dynamic SQL). But it's not a silver bullet...
OMG Ponies