如果列存在于 PostgreSQL 9+ 中,如何删除它? [英] How to drop column if it exists in PostgreSQL 9+?

查看:59
本文介绍了如果列存在于 PostgreSQL 9+ 中,如何删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从表中删除一列.如何检查该列是否存在?

I am trying to drop a column from a table. How can I check if the column exists or not?

我浏览了 https://www.postgresql 上的文档.org/docs/9.2/static/sql-altertable.html,但没有找到任何示例.

I went through the documentation at https://www.postgresql.org/docs/9.2/static/sql-altertable.html, but didn't find any example how to do it.

甚至发现 如何检查列是否存在于 SQL Server 表中?,但它似乎不相关.

Even found How to check if a column exists in a SQL Server table?, but it does not seem relevant.

推荐答案

您只需要将 IF EXIST 添加到您的 DROP COLUMN 语句:

You just need to add IF EXIST to your DROP COLUMN statement:

ALTER TABLE tableName
DROP COLUMN IF EXISTS columnName;

这篇关于如果列存在于 PostgreSQL 9+ 中,如何删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆