SQL 将列更改为可为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3556890/
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
Altering a column to be nullable
提问by Ram
I want to alter a table column to be nullable. I have used:
我想将表列更改为可为空。我用过了:
ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations NULL
This gives an error at Modify
. What is the correct syntax?
这给出了一个错误Modify
。什么是正确的语法?
回答by Quassnoi
Assuming SQL Server
(based on your previous questions):
假设SQL Server
(基于您之前的问题):
ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations INT NULL
Replace INT
with your actual datatype.
替换INT
为您的实际数据类型。
回答by Igor S.
for Oracle Database 10g users:
对于 Oracle 数据库 10g 用户:
alter table mytable modify(mycolumn null);
You get "ORA-01735: invalid ALTER TABLE option" when you try otherwise
当您尝试其他方式时,您会收到“ORA-01735:无效的 ALTER TABLE 选项”
ALTER TABLE mytable ALTER COLUMN mycolumn DROP NOT NULL;
回答by djjeck
If this was MySQL syntax, the type would have been missing, as some other responses point out. Correct MySQL syntax would have been:
如果这是 MySQL 语法,则该类型将丢失,正如其他一些响应所指出的那样。正确的 MySQL 语法应该是:
ALTER TABLE Merchant_Pending_Functions MODIFY NumberOfLocations INT NULL
Posting here for clarity to MySQL users.
在这里发布是为了让 MySQL 用户清楚。
回答by Paul LeBeau
In PostgresQL it is:
在 PostgresQL 中,它是:
ALTER TABLE tableName ALTER COLUMN columnName DROP NOT NULL;
回答by Hammerite
Although I don't know what RDBMS you are using, you probably need to give the whole column specification, not just say that you now want it to be nullable. For example, if it's currently INT NOT NULL
, you should issue ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations INT
.
虽然我不知道您使用的是什么 RDBMS,但您可能需要提供整个列规范,而不仅仅是说您现在希望它可以为空。例如,如果当前是INT NOT NULL
,则应该发出ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations INT
.
回答by APC
As others have observed, the precise syntax for the command varies across different flavours of DBMS. The syntax you use works in Oracle:
正如其他人所观察到的,该命令的精确语法因 DBMS 的不同风格而异。您使用的语法适用于 Oracle:
SQL> desc MACAddresses
Name Null? Type
----------------------------------------- -------- ----------------------------
COMPUTER NUMBER
MACADDRESS VARCHAR2(12)
CORRECTED_MACADDRESS NOT NULL VARCHAR2(17)
SQL> alter table MACAddresses
2 modify corrected_MACAddress null
3 /
Table altered.
SQL> desc MACAddresses
Name Null? Type
----------------------------------------- -------- ----------------------------
COMPUTER NUMBER
MACADDRESS VARCHAR2(12)
CORRECTED_MACADDRESS VARCHAR2(17)
SQL>
回答by Libor B.
For HSQLDB:
对于 HSQLDB:
ALTER TABLE tableName ALTER COLUMN columnName SET NULL;
回答by chamzz.dot
ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT null;
This will work for you.
这对你有用。
If you want to change a not null column to allow null, no need to include not null clause. Because default columns get not null.
如果要将非空列更改为允许为空,则无需包含非空子句。因为默认列不为空。
ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT;
回答by Rodrigo Itursarry
Oracle
甲骨文
ALTER TABLE Merchant_Pending_Functions MODIFY([column] NOT NULL);
ALTER TABLE Merchant_Pending_Functions MODIFY([column] NOT NULL);
回答by ZORRO_BLANCO
This depends on what SQL Engine you are using, in Sybase your command works fine:
这取决于您使用的 SQL 引擎,在 Sybase 中您的命令可以正常工作:
ALTER TABLE Merchant_Pending_Functions
Modify NumberOfLocations NULL;