简体   繁体   English

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

[英]How to alter column of a table with indexes?

I would like to change the column size of the a column in a table with some indexes from varchar(200) to varchar(8000). 我想用一些索引从varchar(200)更改为varchar(8000)的表中列的列大小。 How should I proceed? 我该怎么办?

Since is VARCHAR and you're increasing the size, then simply ALTER TABLE ... ALTER COLUMN ... should be enough. 由于是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. 除非列是varchar,nvarchar或varbinary数据类型,并且新大小等于或大于旧大小, 否则不能更改索引中包含的列的数据类型。

Otherwise you would drop the index(es), alter the column, then add back the index(es). 否则,您将删除索引,更改列,然后添加回索引。

Be aware though that SQL Server maximum index key size is 900 (or 1700 for newer editions), so even though the ALTER will succeed, a future INSERT of data over the 900 length limit will fail with error: 请注意,尽管SQL Server 最大索引密钥大小为900(对于较新版本为1700),因此即使ALTER成功,未来的900长度限制数据INSERT也将失败并显示错误:

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.

Try this for MSSQL: 试试这个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)

Example in Oracle : Oracle示例:

CREATE TABLE EMP (NAME VARCHAR(200));

ALTER TABLE  EMP MODIFY  NAME VARCHAR(800);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM