Linux 通过命令行更改表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5897080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:56:21  来源:igfitidea点击:

Alter table via command line

mysqllinuxalter-table

提问by zozo

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

我需要更改 MySQL 数据库的表(以更改列的长度和类型)。

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

问题是我需要从命令行(在 Linux 中)执行此操作,但我真的不知道如何操作。

Can anyone help me?

谁能帮我?

I have root ssh access.

我有 root ssh 访问权限。

采纳答案by azzy81

Something like this should do the trick:

像这样的事情应该可以解决问题:

Login to mysql:

登录mysql:

mysql -u username -p;

enter mysql password

输入mysql密码

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

varchar(200) 位是您输入列类型和值长度的地方,如 int(11) 等

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.

如果表包含数据,请记住要小心更改字段类型,因为它可能会清空字段或将其减少到指定的新长度。

回答by Dave Maple

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);