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

时间:2018-10-01 07:35:28

标签: sql mariadb

我有

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

我的桌子描述:

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    |       |

现在我想将UserID的列长度修改为varchar(999)

运行

MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID  varchar(999) NOT NULL  AUTO_INCREMENT;

出现以下错误:

ERROR 1063 (42000): Incorrect column specifier for column 'UserID'

已添加>

我也尝试过:

MariaDB [GTX_CC]> ALTER TABLE SubscribedUser MODIFY UserID  varchar(999) 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(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]> 

我的陈述有什么问题?有什么帮助吗?

3 个答案:

答案 0 :(得分:0)

出什么问题了,似乎与以下问题有关:MariaDB won't create table with large VARCHAR as PRIMARY KEY

总而言之,并且不想重复上面链接中的内容,varchar主键有一个最大长度,并且限制取决于字符集。要验证这一点,请删除主键(UDP_IP = "" UDP_PORT = 5000 BUFFER_SIZE = 1024 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((UDP_IP, UDP_PORT)) while True: data, addr = sock.recvfrom(BUFFER_SIZE) ),将userid的长度更改为您的999(成功),尝试将userid添加为主键(alter table SubscribedUser drop primary key),否则会失败同样的错误。

答案 1 :(得分:0)

脚本如下:

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;

varchar下允许的最大字符长度仅为255。

答案 2 :(得分:0)

请提供SHOW CREATE TABLE SubscribedUser。我怀疑那个“描述”是否正确。不管长度如何,都不可能将VARCHAR作为AUTO_INCREMENT

确定要让PRIMARY KEYAUTO_INCREMENT还是VARCHAR

如果您选择使用VARCHAR,请考虑合理的尺寸。

如果您需要为大的VARCHAR编制索引,请参见http://mysql.rjweb.org/doc.php/limits#767_limit_in_innodb_indexes以获得可用的选项。