简体   繁体   English

PostgreSQL:如何更改表定义

[英]Postgresql: how to change the table definition

The problem: I'm searching for an alternative solution to change column definition in postgresql . 问题:我正在寻找一种替代方法来更改postgresql列定义。

I would like to do something that is similar to the mysql solution: 我想做一些类似于mysql解决方案的事情:

ALTER TABLE table_name 
CHANGE [COLUMN] old_col_name new_col_name column_definition 

Is there a way to use CHANGE COLUMN similar to what I've described previously? 有没有办法使用CHANGE COLUMN类似于我之前描述的方式? Is there any way to easily change the column definition in postgres? 有什么方法可以轻松更改postgres中的列定义吗?

I'd like to use a similar method to my example because I'm building the alter query from code. 我想使用与示例相似的方法,因为我正在根据代码构建alter query。

There is no direct equivalent to MySQL's CHANGE COLUMN clause in PostgreSQL. 在PostgreSQL中没有直接等效于MySQL的CHANGE COLUMN子句。 You'll need to specify the column definitions parts one by one. 您需要一一指定列定义的各个部分。 See documentation for ALTER TABLE . 请参阅ALTER TABLE的文档。 Fortunately you don't need to detect differences between old definition and new in typical cases, if the change is redundant PostgreSQL will just ignore it. 幸运的是,在典型情况下,您无需检测新旧定义之间的差异,如果更改是多余的,PostgreSQL只会忽略它。 eg ALTER COLUMN x DROP NOT NULL will still work when the column is already nullable. 例如,当该列已经可以为空时, ALTER COLUMN x DROP NOT NULL仍然可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM