8.7.3. Type Safety

8.7.3. Type Safety
8.7.3 Types of security
Each enumerated data type is separate and cannot be compared with other enumerated types. See this example:
Every enumeration type are independent and can not be compared with other enumerated types. E.g:
 
CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
CREATE TABLE holidays (
num_weeks integer,
happiness happiness
);
INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
ERROR: invalid input value for enum happiness: "sad"
SELECT person.name, holidays.num_weeks FROM person, holidays
WHERE person.current_mood = holidays.happiness;
ERROR: operator does not exist: mood = happiness
 
If you really need to do something like that, you can either write a custom operator or add explicit casts to your query:
If you really want to do this, you can write a custom operator, or an explicit type conversion in the query:
 
SELECT person.name, holidays.num_weeks FROM person, holidays
WHERE person.current_mood::text = holidays.happiness::text;
name | num_weeks
------+-----------
Moe | 4
(1 row)

 

Published 341 original articles · won praise 54 · views 880 000 +

Guess you like

Origin blog.csdn.net/ghostliming/article/details/104663324