更改表在“代码”之前添加...?

发布于 2024-09-13 06:34:46 字数 352 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖阳 2024-09-20 06:34:46
ALTER TABLE `tada_prod`.`action_6_weekly`
ADD COLUMN `id` INT NULL AUTO_INCREMENT UNIQUE FIRST;
ALTER TABLE `tada_prod`.`action_6_weekly`
ADD COLUMN `id` INT NULL AUTO_INCREMENT UNIQUE FIRST;
合约呢 2024-09-20 06:34:46

您只能在特定字段之后添加列,或者首先不能在之前添加列。
在特定字段后添加列的 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

薄暮涼年 2024-09-20 06:34:46

实际上,

alter table table_name ADD column_name VARCHAR(12) NOT NULL BEFORE specific_column_name;

这个命令在 mySQL 语法中是不允许的。如果你使用它,我想你会得到

" 错误 1064:您的 SQL 语法有错误;请检查
与您的 MySQL 服务器版本相对应的手册
在第 1 行“消息的‘before Specific_column_name’附近使用的语法。

您可以尝试:

ALTER TABLE table_name ADD column_name VARCHAR(12) NOT NULL FIRST;

Actually,

alter table table_name ADD column_name VARCHAR(12) NOT NULL BEFORE specific_column_name;

This command is not allowed in mySQL syntax. If you use it I think you get

" ERROR 1064: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'before specific_column_name' at line 1 " message.

You can try:

ALTER TABLE table_name ADD column_name VARCHAR(12) NOT NULL FIRST;
望她远 2024-09-20 06:34:46

扩展 @php 答案,我认为不包括 BEFORE 背后的理由是因为BEFORE 可以使用 AFTERFIRST 轻松实现>

例如:

假设,最初您有一个类似的关系模式

+----------------------+
| name | age | address |
+----------------------+ 

,然后由于某种原因您被迫在之前添加一个新列 dob(出生日期) age,但由于不允许 BEFORE,因此您可以在 之后插入 dob >name 使用 AFTER 可以很好地达到相同的效果。

+----------------------------+
| name | dob | age | address |
+----------------------------+ 

但是如果您想在“name”之前插入新列“id”怎么办?

由于 name 之前没有列,因此我们无法使用 AFTER 来放置 id 列。为了解决这个问题,语言设计者引入了 FIRST ,它使所需的 id 列成为表的第一列。

+---------------------------------+
| id | name | dob | age | address |
+---------------------------------+ 

尽管我个人认为 AFTERBEFORE 会更直观。

Extending @php answer, I think the rationale behind not including BEFORE is because all the effects of BEFORE can be easily achieved using AFTER and FIRST

For Ex:

Let's say, initially you have a relational schema like

+----------------------+
| name | age | address |
+----------------------+ 

and then for some reason you're compelled to add a new column dob(date of birth) just before age, but since BEFORE is not allowed, what you can do instead is insert dob just after the name using AFTER and very well achieve the same effect.

+----------------------------+
| name | dob | age | address |
+----------------------------+ 

But what if you wanted to insert a new column `id` before `name`?

Since there is no column before name we cannot use AFTER to place the id column. In-order to resolve this, the language designers introduced FIRST which makes the desired id column as the first column of the table.

+---------------------------------+
| id | name | dob | age | address |
+---------------------------------+ 

Although I personally think AFTER and BEFORE would've made a more intuitive pair.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文