I have 30 tables in a database, all InnoDB. They have their structure.
What I want to do is actually adding the bellow columns to EVERY table.
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`categoryId` int(11) NOT NULL,
`imageId` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`createdId` int(11) NOT NULL,
`updatedId` int(11) NOT NULL,
`allowedEmployeeIds` text COLLATE utf8_unicode_ci NOT NULL,
In a programming language (assume PHP) the normal approach is to create an abstract class, put all of the common variables there, and then inherit from it. What about MySQL? how should I do this?
For reasons coming from design, it’s not possible for me to create a commonData table and use foreign keys / join. Please note that I am writing the create statements from scratch, so there is no need for update.
I’d think through this for a while — it’s complicated and will add a lit to what you’re trying to do. It’ll add a lot of work later.
But if you really want to do this, the way to do it is not to put this in the table definition, but to create a script that can add these columns to a table.
Something like:
Then simply execute this script for each table you have.
If you want to write a script to do this flor all tables, use the command:
to find all the tables. Then loop through all the tables and execute the alter script on all the tables.