SQL DROP COLUMN problem
#1

I'm using SAMP SQL for users system and i want to delete some column when the table is already exist with a command only.
I'm using this:
ALTER TABLE x DROP COLUMN y

but it doesn't work?
It's work for you?, or you have any solution for me?
Reply
#2

Do you mean SQLite? Not all ALTER TABLE commands are supported (http://www.sqlite.org/omitted.html). Just rename the table, create it without the columns and insert the data back in, 3 simple queries.

EDIT: 4 queries, drop the renamed table afterwards.
Reply
#3

You mean I need to do that?:
1)Rename Y to Z(Table name)
2)Read the table with SELECT
3)Make Table with the name Y
4)add all COLUMNS i need.
5)Insert, but HOW?
Reply
#4

Lets say you had the following table
[code=SQLite]
CREATE TABLE IF NOT EXISTS `users` ( `name` TEXT NOT NULL PRIMARY KEY COLLATE NOCASE , `money` INTEGER NOT NULL DEFAULT 0 , `unusedcolumn` );
INSERT INTO `users` ( `name` ) VALUES ( 'player1' );
INSERT INTO `users` ( `name` ) VALUES ( 'player2' );
INSERT INTO `users` ( `name` ) VALUES ( 'player3' );
[/code]

To remove the `unusedcolumn` column, you would use these queries
[code=SQLite]
ALTER TABLE `users` RENAME TO `old_users`;
CREATE TABLE `users` ( `name` TEXT NOT NULL PRIMARY KEY COLLATE NOCASE , `money` INTEGER NOT NULL DEFAULT 0 );
INSERT INTO `users` SELECT `name` , `money` FROM `old_users`;
DROP TABLE `old_users`;
[/code]
Reply
#5

Quote:
Originally Posted by Dabombber
Lets say you had the following table
[code=SQLite]
CREATE TABLE IF NOT EXISTS `users` ( `name` TEXT NOT NULL PRIMARY KEY COLLATE NOCASE , `money` INTEGER NOT NULL DEFAULT 0 , `unusedcolumn` );
INSERT INTO `users` ( `name` ) VALUES ( 'player1' );
INSERT INTO `users` ( `name` ) VALUES ( 'player2' );
INSERT INTO `users` ( `name` ) VALUES ( 'player3' );
[/code]

To remove the `unusedcolumn` column, you would use these queries
[code=SQLite]
ALTER TABLE `users` RENAME TO `old_users`;
CREATE TABLE `users` ( `name` TEXT NOT NULL PRIMARY KEY COLLATE NOCASE , `money` INTEGER NOT NULL DEFAULT 0 );
INSERT INTO `users` SELECT `name` , `money` FROM `old_users`;
DROP TABLE `old_users`;
[/code]
Okay,but the problem that i have over 25 Columns...
Reply
#6

Just select all the columns you want.
[code=SQLite]
INSERT INTO `table` SELECT `col1` , `col2`, `col3` , `col4` , `col5` , `col6` FROM `old_table`
[/code]
etc...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)