如何使用索引更改 table 的列?

How to alter column of a table with indexes?

我想用从 varchar(200) 到 varchar(8000) 的一些索引更改 table 中 a 列的列大小。我该如何进行?

既然是 VARCHAR 并且您正在增加大小,那么只需 ALTER TABLE ... ALTER COLUMN ... 就足够了。

The data type of columns included in an index cannot be changed unless the column is a varchar, nvarchar, or varbinary data type, and the new size is equal to or larger than the old size.

否则您将删除索引,更改列,然后添加回索引。

请注意,尽管 SQL 服务器 maximum index key size 是 900(或新版本的 1700),因此即使 ALTER 会成功,未来的 INSERT 超过 900 长度限制的数据将失败并出现错误:

Msg 1946, Level 16, State 3, Line 13
Operation failed. The index entry of length ... bytes for the index '...' exceeds the maximum length of 900 bytes.

Oracle中的示例:

CREATE TABLE EMP (NAME VARCHAR(200));

ALTER TABLE  EMP MODIFY  NAME VARCHAR(800);

对 MSSQL 试试这个:

drop index person.idx_pers_fullname
ALTER table person alter COLUMN  pers_firstname nvarchar(8000)
create index idx_pers_fullname on person(pers_firstname)