zozo
zozo

Reputation: 8582

Alter table via command line

I need to alter a table (to change the length and type of a column) of a MySQL database.

The problem is that I need to do this from the command line (in Linux) and I don't really know how.

Can anyone help me?

I have root ssh access.

Upvotes: 2

Views: 24141

Answers (2)

azzy81
azzy81

Reputation: 2269

Something like this should do the trick:

Login to mysql:

mysql -u username -p;

enter mysql password

use databasename;

ALTER TABLE `tablename`
MODIFY COLUMN `columnname`  varchar(200) NULL DEFAULT NULL AFTER `previouscolumnname`;

The varchar(200) bit is where you would enter the column type and value length like int(11) etc

Remember to be careful changing field types if the table contains data as it may empty the field or cut it down to the new length specified.

Upvotes: 13

Dave Maple
Dave Maple

Reputation: 8412

Obviously you need to update per your username, database, tablename and type/length:

mysql -u username -p
use database;
alter table `tablename` modify column `columnname` varchar(512);

Upvotes: 0

Related Questions