更改表在“代码”之前添加...?
ALTER TABLE tada_prod
.action_6_weekly
添加列 id
INT NULL AUTO_INCRMENT UNIQUE AFTER member_id
;
有效,
所以我想,将该列添加为我可以做的第一列,
ALTER TABLE `tada_prod`.`action_6_weekly` ADD COLUMN `id` INT NULL AUTO_INCREMENT UNIQUE BEFORE `code`;
但出现语法错误, 正确的语法是什么?
ALTER TABLE tada_prod
.action_6_weekly
ADD COLUMN id
INT NULL AUTO_INCREMENT UNIQUE AFTER member_id
;
works,
so i thought, to add the column as the first column i could do
ALTER TABLE `tada_prod`.`action_6_weekly` ADD COLUMN `id` INT NULL AUTO_INCREMENT UNIQUE BEFORE `code`;
but i get a syntax error,
what is the correct syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只能在特定字段之后添加列,或者首先不能在之前添加列。
在特定字段后添加列的 mysql 查询为:
ALTER TABLE table_name ADD COLUMN column_name VARCHAR(30) AFTER column_name
You can add column only after particular field or at first not before.
The mysql query for add column after particular filed is:
ALTER TABLE table_name ADD COLUMN column_name VARCHAR(30) AFTER column_name
实际上,
这个命令在 mySQL 语法中是不允许的。如果你使用它,我想你会得到
您可以尝试:
Actually,
This command is not allowed in mySQL syntax. If you use it I think you get
You can try:
扩展 @php 答案,我认为不包括 BEFORE 背后的理由是因为
BEFORE
可以使用AFTER
和FIRST
轻松实现>例如:
假设,最初您有一个类似的关系模式
,然后由于某种原因您被迫在之前添加一个新列
dob
(出生日期)age
,但由于不允许BEFORE
,因此您可以在之后插入
使用dob
>nameAFTER
可以很好地达到相同的效果。由于
name
之前没有列,因此我们无法使用AFTER
来放置id
列。为了解决这个问题,语言设计者引入了FIRST
,它使所需的id
列成为表的第一列。尽管我个人认为
AFTER
和BEFORE
会更直观。Extending @php answer, I think the rationale behind not including BEFORE is because all the effects of
BEFORE
can be easily achieved usingAFTER
andFIRST
For Ex:
Let's say, initially you have a relational schema like
and then for some reason you're compelled to add a new column
dob
(date of birth) just beforeage
, but sinceBEFORE
is not allowed, what you can do instead is insertdob
just after thename
usingAFTER
and very well achieve the same effect.Since there is no column before
name
we cannot useAFTER
to place theid
column. In-order to resolve this, the language designers introducedFIRST
which makes the desiredid
column as the first column of the table.Although I personally think
AFTER
andBEFORE
would've made a more intuitive pair.