[英]How can I modify the size of Primary Key column of Varchar in a MariaDB table?
I have 我有
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.0.36-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
My table desc: 我的桌子描述:
MariaDB [GTX_CC]> desc SubscribedUser;
+---------------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------------------+--------------+------+-----+---------+-------+
| UserID | varchar(25) | NO | PRI | NULL | |
| UserName | varchar(100) | YES | | NULL | |
| UserStatusCode | varchar(10) | NO | | NULL | |
| Password | varchar(512) | NO | | NULL | |
Now I want to Modify the Column length of UserID to varchar(999) 现在我想将UserID的列长度修改为varchar(999)
Running 运行
MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID varchar(999) NOT NULL AUTO_INCREMENT;
gives the following error: 给出以下错误:
ERROR 1063 (42000): Incorrect column specifier for column 'UserID'
Added> 新增>
I also tried: 我也尝试过:
MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID varchar(999) NOT NULL ;
It also gives error as: 它还给出如下错误:
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
Also Tried as follows: 还尝试如下:
MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID varchar(767) NOT NULL ;
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID varchar(766) NOT NULL ;
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
MariaDB [GTX_CC]>
What wrong in my statement? 我的陈述有什么问题? Any help , please?
有什么帮助吗?
What is wrong, appears to be related to this question: MariaDB won't create table with large VARCHAR as PRIMARY KEY . 出了什么问题,似乎与这个问题有关: MariaDB不会创建带有大VARCHAR作为PRIMARY KEY的表 。
In summary, and without wanting to repeat what's said in the above link, there is a maximum length for varchar primary keys, and the limit depends on the charset. 总而言之,并且不想重复上面链接中的内容,varchar主键有最大长度,并且限制取决于字符集。 To verify that, drop the primary key (
alter table SubscribedUser drop primary key
), alter the length of userid to your 999 (that succeeds), try to add userid as primary key ( alter table SubscribedUser add primary key(userid)
) and it'd fail with the same error. 要验证这一点,请删除主键(
alter table SubscribedUser drop primary key
),将userid的长度更改为999(成功),尝试将userid添加为主键( alter table SubscribedUser add primary key(userid)
),然后将失败,并出现相同的错误。
The script will be as follows: 该脚本将如下所示:
LOCK TABLES
UserSession WRITE,
SubscribedUser WRITE;
ALTER TABLE UserSession
DROP FOREIGN KEY fk_Session_SubscribedUser1,
MODIFY SubscribedUser_UserID varchar(255) NOT NULL;
ALTER TABLE SubscribedUser MODIFY UserID varchar(255) NOT NULL ;
ALTER TABLE UserSession
ADD CONSTRAINT fk_Session_SubscribedUser1 FOREIGN KEY (SubscribedUser_UserID)
REFERENCES SubscribedUser (UserID);
UNLOCK TABLES;
The maximum allowed char length under varchar is 255 only. varchar下允许的最大字符长度仅为255。
Please provide SHOW CREATE TABLE SubscribedUser
. 请提供
SHOW CREATE TABLE SubscribedUser
。 I question whether that 'describe' is correct. 我怀疑那个“描述”是否正确。 It should be impossible to have a
VARCHAR
as an AUTO_INCREMENT
, regardless of the length . 无论长度如何,都不可能将
VARCHAR
用作AUTO_INCREMENT
。
Decide whether you want the PRIMARY KEY
to be AUTO_INCREMENT
or VARCHAR
. 确定您是要
PRIMARY KEY
是AUTO_INCREMENT
还是VARCHAR
。
If you choose to have a VARCHAR
, then think about what size is reasonable. 如果选择使用
VARCHAR
,请考虑合理的大小。
If you need to index a large VARCHAR
, then see http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes for the options you have. 如果您需要为大型
VARCHAR
编制索引,请参见http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes了解所提供的选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.