简体   繁体   English

如何修改MariaDB表中Varchar的“主键”列的大小?

[英]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 KEYAUTO_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.

相关问题 如何修改varchar的大小? - How can I modify the size of a varchar? 如何创建一个包含五个外键的表,其中一个作为主键(也是一个VARCHAR) - How can I create a table with five foreign keys and one of them as primary key (which is also a VARCHAR) Oracle 12c:如何将现有主键列修改为标识列? - Oracle 12c: How can I modify an existing primary key column to an identity column? 我可以使用 VARCHAR 作为 PRIMARY KEY 吗? - Can I use VARCHAR as the PRIMARY KEY? 在具有varchar类型主键的表中具有Identity列 - Having an Identity column in a table with primary key with varchar type 使varchar列成为主键 - making a varchar column to be a PRIMARY KEY 将表主键列大小更改为 255 - Alter table primary key column size to 255 我应该设计一个主键为varchar或int的表吗? - Should I design a table with a primary key of varchar or int? 如何在一张表的(order_no)同一列中添加主键和检查约束 - How can I add primary key and check constraint in (order_no) same column in one table 如何引用或将具有varchar主键的表与具有INT主键的表结合在一起(SQL Server) - How to reference or join a table that has a varchar primary key with a table that has an INT primary Key (SQL server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM