开发者

PostgreSQL: check if attribute is in table

开发者 https://www.devze.com 2023-04-05 12:20 出处:网络
I\'m writing some triggers to check if an arrtibute of a newly create tuple is present in another table. What is the best way to check this?

I'm writing some triggers to check if an arrtibute of a newly create tuple is present in another table. What is the best way to check this?

IF (SELECT * FROM TABLEB where NEW.Attribute = Attribute) = NULL THEN
return NULL
END I开发者_如何转开发F

Is there a better way? And does a Select that returns nothing = NULL or empty set?


Use IF EXISTS (SELECT * FROM TABLEB where NEW.Attribute = Attribute).

An empty rowset isn't the same as NULL, which represents an empty cell of a row. Your syntax would only be valid if the query only selected one row, in which case PostgreSQL will allow use of comparison operators against the top cell returned, which counts as NULL if none are returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号