I am using Ubuntu and PostgreSql 8.4.9.
Now, for any table in my database, if I do select table_name.name from table_name
, it shows a result of concatenated columns for each row, although I don't have any name
column in the table. For the tables which have name column, no issue. Any idea why?
My results are like this:
select taggings.name from taggings limit 3;
---------------------------------------------------------------
(1,4,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(2,5,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(3,6,84,,,PlantCategory,soil_pref_tags,"2010-03-18 00:37:55")
(3 rows)
select name from taggings limit 3;
ERROR: column "name" does not exist
LINE 1: select name from taggings limit 3;
Name is a 63 byte (varchar) type used for storing system identifiers. Using psql you can get type information using \dT or \dT+
Special character symbols are characters with a pre-defined syntactic meaning in PostgreSQL. They are typically disallowed from being used in identifier names for this reason, though as mentioned in the section on quoted identifiers, this restriction can usually be worked around with quotes if need be.
The most natural column name is typically type , but I try to avoid using SQL keywords or reserved words in my naming. I'm aware that type is a non-reserved keyword in both MySQL and Postgres, so I can use it, but should I? What is current best practice around using type as a column name?
where ID is a column I added via a GUI-tool. I then found out that ID is a keyword in my current version of PSQL and should be quoted.
This is a known confusing "feature" with a bit of history. Specifically, you could refer to tuples from the table as a whole with the table name, and then appending .name
would invoke the name
function on them (i.e. it would be interpreted as select name(t) from t
).
At some point in the PostgreSQL 9 development, Istr this was cleaned up a bit. You can still do select t from t
explicitly to get the rows-as-tuples effect, but you can't apply a function in the same way. So on PostgreSQL 8.4.9, this:
create table t(id serial primary key, value text not null);
insert into t(value) values('foo');
select t.name from t;
produces the bizarre:
name
---------
(1,foo)
(1 row)
but on 9.1.1 produces:
ERROR: column t.name does not exist
LINE 1: select t.name from t;
^
as you would expect.
So, to specifically answer your question: name
is a standard type in PostgreSQL (used in the catalogue for table names etc) and also some standard functions to convert things to the name
type. It's not actually reserved, just the objects that exist called that, plus some historical strange syntax, made things confusing; and this has been fixed by the developers in recent versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With