如何使用 sql 脚本更改列的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4848784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 09:06:53  来源:igfitidea点击:

How to alter a column's attribute using sql script

sqlsql-serverdatabasesql-server-2008

提问by Jerameel Resco

How can I alter a column's attribute using a sql script?

如何使用 sql 脚本更改列的属性?

Here's what I've tried but I got errors:

这是我尝试过但出现错误的方法:

ALTER TABLE [dbo].[tblBiometricPattern] COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY(1,1)

Thank you in advance.

先感谢您。

Here's the error message that appears when executed:

这是执行时出现的错误消息:

Incorrect syntax near the keyword 'COLUMN'.

采纳答案by Damien_The_Unbeliever

If you're trying to alter the column so that it's an IDENTITY column... you can't do that. You can add a new column with the identity property, but you can't alter an existing column.

如果您试图更改该列,使其成为 IDENTITY 列……您不能这样做。您可以添加具有标识属性的新列,但不能更改现有列。

If that's not what you're trying to do, perhaps you could include the actual error messagesyou're getting.

如果这不是您想要做的,也许您可​​以包含您收到的实际错误消息



The general form for altering an existing column is:

更改现有列的一般形式是:

ALTER TABLE [dbo].[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL IDENTITY(1,1)

(that is, you were missing the word "ALTER" before COLUMN). But as I say, this will now return an error telling you that you can't change the IDENTITY property of the column.

(也就是说,您在 COLUMN 之前遗漏了“ALTER”一词)。但正如我所说,这现在将返回一个错误,告诉您无法更改列的 IDENTITY 属性。



If the column is alreadyan identity column, and you're just altering the datatype, then leave off the IDENTITY() property. It will still be an identity column:

如果该列已经是一个标识列,而您只是在更改数据类型,则不要使用 IDENTITY() 属性。它仍然是一个标识列:

ALTER TABLE [dbo].[tblBiometricPattern] ALTER COLUMN BiometricPatternID TINYINT NOT NULL

回答by bsanneh

ALTER TABLE table_name ALTER COLUMN column_name datatype

ALTER TABLE table_name ALTER COLUMN column_name 数据类型

回答by Saleh Enam Shohag

If you want to alter/modify column of a table.
For MySQL / Oracle (prior version 10G):

如果要更改/修改表的列。
对于 MySQL/Oracle(10G 之前的版本):

ALTER TABLE table_name MODIFY COLUMN column_name datatype;

For Oracle 10G and later:

对于 Oracle 10G 及更高版本:

ALTER TABLE table_name MODIFY column_name datatype;