I have a Postgresql 8.1 database. In one table, there are three columns: first_name, last_name, display_name.
Is it possible to set the default value of display_name to be first_name + " " + last_name?
|
I have a Postgresql 8.1 database. In one table, there are three columns: Is it possible to set the default value of |
|||
|
|
|
Use a trigger. Here's some code you can use as a base. If you also need to handle UPDATEs, only a small change is required.
|
|||
|
With the DEFAULT setting on a column table, nope. Your best bet here is a TRIGGER with the NEW value for each column you want computed. |
|||
|
|
|
You don't need to actually store the value; you can create a function that can be referenced much like a generated column. The one caveat is that references must always be qualified with the table or alias name.
The results: id | display_name ----+-------------- 1 | John Smith 2 | Jane Doe 3 | Prince (3 rows) You can even index on the generated value, including using KNN searches based on trigram similarity. For example:
This type of search returns rows from the index scan in order of "distance" from the search string. If you want to see how "close" they were, you could either use the distance operator ( |
|||||
|