maL-AU Posted September 28, 2011 Share Posted September 28, 2011 Hey all, I've been trying to set up a small script that would create a different column every time a file is updated, kind of for a form of logging. I need it to replace the number at the end of the column name with the next if said column already exists, for example: say i try to make a column named 'column1' ALTER TABLE `table1` ADD `column1` VARCHAR(4) NOT NULL except column 1 already exists, so mysql_error returns "Duplicate column name 'column1'" so it would then try ALTER TABLE `table1` ADD `column2` VARCHAR(4) NOT NULL and so on, until it successfully creates a column, or by doing it some otherway, but the idea would remain the same. I know this is insanely inefficient but is this possible? Any help would be greatly appreciated, Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/248015-mysql-creating-sequential-column-names/ Share on other sites More sharing options...
Buddski Posted September 28, 2011 Share Posted September 28, 2011 Perhaps looking up the COLUMNS already in the table, by looking through this information you will know what the next column name is. SHOW COLUMNS FROM `table1`; Quote Link to comment https://forums.phpfreaks.com/topic/248015-mysql-creating-sequential-column-names/#findComment-1273468 Share on other sites More sharing options...
cunoodle2 Posted September 28, 2011 Share Posted September 28, 2011 Why can't you just add another row? Adding another column seems extremely inefficient. Quote Link to comment https://forums.phpfreaks.com/topic/248015-mysql-creating-sequential-column-names/#findComment-1273561 Share on other sites More sharing options...
Pikachu2000 Posted September 28, 2011 Share Posted September 28, 2011 Altering a table every time data changes is poor design. You're attempting to force a relational database to behave as a spreadsheet, which it is not. Quote Link to comment https://forums.phpfreaks.com/topic/248015-mysql-creating-sequential-column-names/#findComment-1273563 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.