KingMofo Posted May 21, 2010 Share Posted May 21, 2010 Hello all. I use SQL quite a lot. When i am updating a table, sometimes i want to insert a new column into an existing table but the only way i can seem to do that is if i drop the table and re-create the table with the added column which results in all the data in the table being lost. I was wondering if anyone knows how to add a column to the table without having to drop it and re-create it? An example: Here's my current table columns How would i add an extra column called "Account" without dropping and re-creating? I would really appreciate any help on this. Thanks in advanced. Link to comment
dzek (varez) Posted May 21, 2010 Share Posted May 21, 2010 this is for mysql, but should work in same way in sqlite (read sqlite manual maybe)? ALTER TABLE `wiki_user` ADD `test` INT Link to comment
50p Posted May 21, 2010 Share Posted May 21, 2010 It would work varez but unfortunately ALTER TABLE doesn't work in MTA. http://www.sqlite.org/lang_altertable.html It's a bug or "feature" or it's blocked for "security reason". In theory, you could copy the table where you want to add a column, create a new table with a new column and copy all the data from old table (the backup table) to the new one. http://www.sqlite.org/faq.html#q11 But it didn't work either when I tried it long time ago. Link to comment
dzek (varez) Posted May 21, 2010 Share Posted May 21, 2010 wow, weird.. security reasons? it could be better to block "drop table" But it didn't work either when I tried it long time ago. what? this workaroud HAVE TO work Link to comment
KingMofo Posted May 21, 2010 Author Share Posted May 21, 2010 It would work varez but unfortunately ALTER TABLE doesn't work in MTA. http://www.sqlite.org/lang_altertable.htmlIt's a bug or "feature" or it's blocked for "security reason". In theory, you could copy the table where you want to add a column, create a new table with a new column and copy all the data from old table (the backup table) to the new one. http://www.sqlite.org/faq.html#q11 But it didn't work either when I tried it long time ago. Oh wow, i just read that page about transferring it to another table and it sounds not a bad idea. I will try it out and let you know how it goes. Cheers you 2. Link to comment
eAi Posted May 21, 2010 Share Posted May 21, 2010 That'd be quite a slow thing to do with a large table. I'd generally avoid adding columns to tables like this - unless it's a kind of administrative task. Link to comment
AdiBoy Posted May 22, 2010 Share Posted May 22, 2010 oh, but alter table works, i've used it before, im still using it when updating my scripts get the admin panel up, resources tab and execute this command: executeSQLQuery( "ALTER TABLE ADD COLUMN (size) DEFAULT " ) the DEFAULT option is not mandatory, you can skip it but you might also find it useful Link to comment
KingMofo Posted May 22, 2010 Author Share Posted May 22, 2010 oh, but alter table works, i've used it before, im still using it when updating my scriptsget the admin panel up, resources tab and execute this command: executeSQLQuery( "ALTER TABLE ADD COLUMN (size) DEFAULT " ) the DEFAULT option is not mandatory, you can skip it but you might also find it useful Oh wow! I just tried that and it worked! Thank you all for your help and thank you AdiBoy. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now