MySQL 更改表添加主键语法错误的列

编程入门 行业动态 更新时间:2024-10-25 14:27:20
本文介绍了MySQL 更改表添加主键语法错误的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试向我的数据库表之一添加一列,但出现语法错误我似乎找不到问题...

I'm trying to add a column to one of of my database tables, but there is a syntax error and I can't seem to find the problem...

我当前的数据库表如下所示:

My current database table looks like this:

component + tag_id + item_id ------------|----------|----------- com_content | 23 | 2642 com_content | 26 | 3481 com_content | 35 | 1868 com_content | 85 | 5827 com_content | 89 | 7882

我希望它看起来像这样,其中id"是自动递增的,所有列都是主键的一部分

I want it to look like this, where 'id' is auto increment and all columns part of the primary key

id + component + tag_id + item_id -----|--------------|----------|----------- 1 | com_content | 23 | 2642 2 | com_content | 26 | 3481 3 | com_content | 35 | 1868 4 | com_content | 85 | 5827 5 | com_content | 89 | 7882

这是我的查询:

DROP PRIMARY KEY ALTER TABLE gitags_items ADD COLUMN id INT NOT NULL AUTO_INCREMENT FIRST PRIMARY KEY (id,component,tag_id,item_id)

但是我收到此错误消息:

However I'm getting this error message:

#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 'PRIMARY KEY ALTER TABLE gitags_items ADD COLUMN id INT NOT NULL AUTO_INC' at line 1

任何帮助/指针将不胜感激

Any help/pointers would be much appreciated

推荐答案

'ALTER TABLE' 位必须先出现,然后每个部分必须用逗号分隔:

The 'ALTER TABLE' bit must come first, and then each part must be seperated by a comma:

ALTER TABLE gitags_items DROP PRIMARY KEY, ADD COLUMN id INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (id,component,tag_id,item_id);

但我不确定您是否可以在同一个状态中删除和创建主键.

but I'm not sure if you can drop and create a primary key in the same staatement.

更多推荐

MySQL 更改表添加主键语法错误的列

本文发布于:2023-10-20 03:28:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509710.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   语法错误   MySQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!