Uno Mein Ame
Uno Mein Ame

Reputation: 1090

add mysql columns on the fly

Is there a way to add mysql columns on the fly when I try to insert a row?

For example, table contains columns A, B, C and I want to insert into the table a row that contains A, C, D, and F?

Is there a way to actually add the D and F columns instead of resulting in an error?

Upvotes: 1

Views: 1216

Answers (3)

Uno Mein Ame
Uno Mein Ame

Reputation: 1090

Thank you GlaciesofPacis and Adev

You made me think differently about the question that I asked, and it looks like your solution is a lot more elegant than what I was initially thinking about.

Instead of having a table with very few rows and over 100 columns, I will use the ID, Key, Value model.

Thank you!

Upvotes: 2

Ahmed Kato
Ahmed Kato

Reputation: 1707

you can use ALTER TABLE and give it any query you need for example

ALTER TABLE TABLE_NAME
ADD COLUMN_NAME (ATTRIBUTE)

hope this helps you

Upvotes: 0

Ofir Baruch
Ofir Baruch

Reputation: 10356

You can use Alter and ADD in order to add new columns to your table.

For instance;

ALTER TABLE contacts ADD email VARCHAR(60);

Will add a new column named email which its type if Varchar , to the contacts table.

Of course that before you insert the row , you must check which columns to add, add them and just then insert the new row.

Upvotes: 0

Related Questions