博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在MySQL中的特定列之后添加多个列
阅读量:2291 次
发布时间:2019-05-09

本文共 2955 字,大约阅读时间需要 9 分钟。

本文翻译自:

I need to add multiple columns to a table but position the columns after a column called lastname . 我需要向一个表中添加多个列,但将这些列lastname 名为lastname的列之后

I have tried this: 我已经试过了:

ALTER TABLE `users` ADD COLUMN(    `count` smallint(6) NOT NULL,    `log` varchar(12) NOT NULL,    `status` int(10) unsigned NOT NULL) AFTER `lastname`;

I get this error: 我收到此错误:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AFTER lastname ' at line 7 检查与您的MySQL服务器版本相对应的手册,以在第7行的')AFTER lastname '附近使用正确的语法

How can I use AFTER in a query like this? 如何在这样的查询中使用AFTER?


#1楼

参考:


#2楼

One possibility would be to not bother about reordering the columns in the table and simply modify it by add the columns. 一种可能性是不用担心重新排序表中的列,而只需通过添加列来对其进行修改。 Then, create a view which has the columns in the order you want -- assuming that the order is truly important. 然后,创建一个具有所需顺序的列的视图-假设顺序确实很重要。 The view can be easily changed to reflect any ordering that you want. 可以轻松更改视图以反映所需的任何顺序。 Since I can't imagine that the order would be important for programmatic applications, the view should suffice for those manual queries where it might be important. 由于我无法想象顺序对于编程应用程序将很重要,因此对于那些可能很重要的手动查询,该视图就足够了。


#3楼

Try this 尝试这个

ALTER TABLE usersADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;

check the 检查


#4楼

If you want to add a single column after a specific field, then the following MySQL query should work: 如果要在特定字段后添加一列,则以下MySQL查询应该起作用:

ALTER TABLE users    ADD COLUMN count SMALLINT(6) NOT NULL    AFTER lastname

If you want to add multiple columns, then you need to use 'ADD' command each time for a column. 如果要添加多个列,则每次需要为列使用“ ADD”命令。 Here is the MySQL query for this: 这是对此的MySQL查询:

ALTER TABLE users    ADD COLUMN count SMALLINT(6) NOT NULL,    ADD COLUMN log VARCHAR(12) NOT NULL,    ADD COLUMN status INT(10) UNSIGNED NOT NULL    AFTER lastname

Point to note 注意事项

In the second method, the last ADD COLUMN column should actually be the first column you want to append to the table. 在第二种方法中,最后一个ADD COLUMN 实际上应该是要添加到表中的第一列。

Eg: if you want to add count , log , status in the exact order after lastname , then the syntax would actually be: 例如:如果您lastname后的确切顺序添加countlogstatus ,则语法实际上是:

ALTER TABLE users    ADD COLUMN log VARCHAR(12) NOT NULL AFTER lastname,    ADD COLUMN status INT(10) UNSIGNED NOT NULL AFTER lastname,    ADD COLUMN count SMALLINT(6) NOT NULL AFTER lastname

#5楼

ALTER TABLE `users` ADD COLUMN`COLUMN NAME` DATATYPE(SIZE) AFTER `EXISTING COLUMN NAME`;

You can do it with this, working fine for me. 您可以这样做,对我来说很好。


#6楼

This one is correct: 这是正确的:

ALTER TABLE `users`    ADD COLUMN `count` SMALLINT(6) NOT NULL AFTER `lastname`,    ADD COLUMN `log` VARCHAR(12) NOT NULL AFTER `count`,    ADD COLUMN `status` INT(10) UNSIGNED NOT NULL AFTER `log`;

转载地址:http://tmcnb.baihongyu.com/

你可能感兴趣的文章
PHP平滑升级
查看>>
MySQL删除无用账户
查看>>
MySQL多实例配置方案
查看>>
MySQL设置及修改root密码
查看>>
MySQL用户权限
查看>>
MySQL数据备份
查看>>
MySQL使用explain检查索引执行计划
查看>>
MySQL字符集
查看>>
MySQL存储引擎
查看>>
MySQL主从同步
查看>>
MySQL半同步复制
查看>>
MySQL主库宕机从库提权
查看>>
MySQL主主模式
查看>>
MySQL错误代码
查看>>
MySQL binlog的三种模式
查看>>
MySQL利用binlog增量恢复数据库
查看>>
Tomcat多实例多应用
查看>>
Tomcat启动慢解决方法
查看>>
Tomca主配置文件详解
查看>>
Tomcat创建虚拟主机
查看>>