将表主键列大小更改为255(Alter table primary key column size to 255)

编程入门 行业动态 更新时间:2024-10-25 18:32:55
将表主键列大小更改为255(Alter table primary key column size to 255)

我有一个主键列大小设置为50的表。由于一些新的要求,我需要增加到255的大小。在网上搜索后,我明白它是不可能改变列,如果它是一部分主键约束。 所以,我采取了放弃约束,改变列和添加约束的方法。 但是,我仍然遇到一个小问题,我的原始列是Non Null类型,并且具有默认值集,但现在当我尝试以下SQL时,出现错误“DEFAULT附近的语法不正确”

ALTER TABLE [tblLocation] DROP CONSTRAINT [PK_tblLocation] ALTER TABLE [tblLocation] ALTER COLUMN Location VARCHAR(255) DEFAULT('New Location') NOT NULL ALTER TABLE [tblLocation] ADD CONSTRAINT [PK_tblLocation] PRIMARY KEY CLUSTERED ( [Location] ASC )

谢谢你的帮助。 Javid

I have a table with a primary key column size is set to 50. Due to some new requirements, I need to increase the size to 255. After searching online, I understood it is not possible to alter a column if it is a part of a primary key constraint. So, I took the approach of dropping the constraint, altering the column and adding the constraint back. But, I am still experiencing a small issue, my original column is of type Non Null and has a default value set, but now when i try the following sql, I get an error "Incorrect syntax near DEFAULT"

ALTER TABLE [tblLocation] DROP CONSTRAINT [PK_tblLocation] ALTER TABLE [tblLocation] ALTER COLUMN Location VARCHAR(255) DEFAULT('New Location') NOT NULL ALTER TABLE [tblLocation] ADD CONSTRAINT [PK_tblLocation] PRIMARY KEY CLUSTERED ( [Location] ASC )

Thanks for any help. Javid

最满意答案

您不能使用`ALTER TABLE ... ALTER COLUMN'同时修改列约束(这里是默认值)。 您首先需要更改列,然后更改默认约束。 (您可能需要先删除然后重新创建默认设置。)

You cannot use `ALTER TABLE... ALTER COLUMN' to modify both a column and a constraint (here, the default) at the same time. You will first need to alter the column, and then alter the default constraint. (You might need to first drop and then recreate the default.)

更多推荐

本文发布于:2023-08-01 11:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   大小   Alter   table   key

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!