SQL domain ERROR: column does not exist, setting default

懵懂的女人 提交于 2019-12-11 09:16:56

问题


I created a DOMAIN:

CREATE DOMAIN public."POSTAL_CODE"
  AS character(5)
  NOT NULL;

I tried to set the default value:

ALTER DOMAIN public."POSTAL_CODE"
SET DEFAULT "00000";

but got the error:

ERROR: column "00000" does not exist

Then I managed to set the default value using DEFAULT 00000, but I think it was cast to INTEGER as it shows as 0 instead of 00000. I tried character(5)[] and {'0','0','0','0','0'} without success as well.

How to get a default value as text and not get an error?

I used PostgreSQL.


回答1:


In SQL double quotes " are used to refer to a column or table named "select"

A string constant in SQL is an arbitrary sequence of characters bounded by single quotes ('), for example 'This is a string'. So this is not the same as a double-quote character (")

As a result you have to use single quote like below

select * from test where old_code = '220088242'

so in your case it should be like below

ALTER DOMAIN public."POSTAL_CODE"
    SET DEFAULT '00000';



回答2:


Try this: value should be in single quote like '00000'

ALTER DOMAIN public."POSTAL_CODE" set default '00000';



回答3:


Try this

Alter table <table name> ALTER <column name> TYPE character varying, ALTER <column name> SET DEFAULT '00000'


来源:https://stackoverflow.com/questions/51837721/sql-domain-error-column-does-not-exist-setting-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!