MODIFY COLUMN in oracle - How to check if a column is nullable before setting to nullable?(oracle 中的修改列 - 如何在设置为可为空之前检查列是否可为空?)
问题描述
我试图替一位同事做一些 Oracle 工作,但遇到了一个障碍.在尝试编写脚本以将列修改为可为空时,我遇到了可爱的 ORA-01451 错误:
I'm trying to fill in for a colleague in doing some Oracle work, and ran into a snag. In attempting to write a script to modify a column to nullable, I ran into the lovely ORA-01451 error:
ORA-01451: column to be modified to NULL cannot be modified to NULL
发生这种情况是因为该列已经为 NULL.我们有几个需要更新的数据库,所以在我错误的假设中,我认为将它设置为 NULL 应该可以全面确保每个人都是最新的,无论他们是否手动将此列设置为可空.但是,这显然会导致某些已经将该列设为可为空的人出错.
This is happening because the column is already NULL. We have several databases that need to be udpated, so in my faulty assumption I figured setting it to NULL should work across the board to make sure everybody was up to date, regardless of whether they had manually set this column to nullable or not. However, this apparently causes an error for some folks who already have the column as nullable.
如何检查一列是否已经可以为空以避免错误?可以实现这个想法的东西:
How does one check if a column is already nullable so as to avoid the error? Something that would accomplish this idea:
IF( MyTable.MyColumn IS NOT NULLABLE)
ALTER TABLE MyTable MODIFY(MyColumn NULL);
推荐答案
您可以在 PL/SQL 中执行此操作:
You could do this in PL/SQL:
declare
l_nullable user_tab_columns.nullable%type;
begin
select nullable into l_nullable
from user_tab_columns
where table_name = 'MYTABLE'
and column_name = 'MYCOLUMN';
if l_nullable = 'N' then
execute immediate 'alter table mytable modify (mycolumn null)';
end if;
end;
这篇关于oracle 中的修改列 - 如何在设置为可为空之前检查列是否可为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:oracle 中的修改列 - 如何在设置为可为空之前检查列是否可为空?


基础教程推荐
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01