Thread: "quoting" column names

"quoting" column names

From
Patrik Kudo
Date:
Hi

I've been using Postgres for... well, as long as I can remember ;)
And aparently I've been using a column name wich is "almost reserved"
(i.e. cannot be used as identifer). Since upgrading to 6.5 I nolonger
can access these fields the way I've always done them. This what I've
been doing:

create table login (userid text, when datetime);
select when from login where userid = 'xxxx';

In 6.5 I have to do:

select "when" from login where userid = 'xxxx';

My question now is, should I always quote column names, just in case,
or should I only quote column names that are "almost reserved"?

/Kudo
-- 
ech`echo xiun|tr nu oc|sed 'sx\([sx]\)\([xoi]\)xo un\2\1 is xg'`ol
Känns det oklart? Fråga på!



Re: [SQL] "quoting" column names

From
Tom Lane
Date:
Patrik Kudo <kudo@partitur.se> writes:
> create table login (userid text, when datetime);
> select when from login where userid = 'xxxx';
> In 6.5 I have to do:
> select "when" from login where userid = 'xxxx';

Right ... WHEN is a keyword in CASE expressions, which didn't use to
be implemented but are now.

> My question now is, should I always quote column names, just in case,
> or should I only quote column names that are "almost reserved"?

In theory you should only have to worry about keywords that are
reserved in SQL92 but that Postgres hasn't gotten around to implementing
yet.  That's a fairly short list, I think, but I do not have the
details handy.  (Anyone?)

Quoting every column name in sight would make your SQL pretty unreadable
(pg_dump now does it by default ... try reading pg_dump commands :-().
Also, if you have a reserved word as a column name, you won't be able
to access the table at all using tools that don't quote column names.
Better to rename your columns if you get burnt by this.
        regards, tom lane


Re: [SQL] "quoting" column names

From
Patrik Kudo
Date:
Tom Lane wrote:
> 
> Patrik Kudo <kudo@partitur.se> writes:
> > create table login (userid text, when datetime);
> > select when from login where userid = 'xxxx';
> > In 6.5 I have to do:
> > select "when" from login where userid = 'xxxx';
> 
> Right ... WHEN is a keyword in CASE expressions, which didn't use to
> be implemented but are now.

Ah! that explains that part...

> > My question now is, should I always quote column names, just in case,
> > or should I only quote column names that are "almost reserved"?
> 
> In theory you should only have to worry about keywords that are
> reserved in SQL92 but that Postgres hasn't gotten around to implementing
> yet.  That's a fairly short list, I think, but I do not have the
> details handy.  (Anyone?)

The list of reserved keywords can be found at:http://www.postgresql.org/ -> Info Central -> Documentation -> User's
Guide
-> SQL Syntax -> Key Words.

That list doesn't say which need to be quoted though.

> Quoting every column name in sight would make your SQL pretty unreadable
> (pg_dump now does it by default ... try reading pg_dump commands :-().
> Also, if you have a reserved word as a column name, you won't be able
> to access the table at all using tools that don't quote column names.
> Better to rename your columns if you get burnt by this.

Will do, will do...

/Kudo


Re: [SQL] "quoting" column names

From
Herouth Maoz
Date:
At 17:08 +0300 on 13/07/1999, Tom Lane wrote:


> In theory you should only have to worry about keywords that are
> reserved in SQL92 but that Postgres hasn't gotten around to implementing
> yet.  That's a fairly short list, I think, but I do not have the
> details handy.  (Anyone?)

The list of reserved words from the SQL92 spec:

ABSOLUTE, ACTION, ADD, ALL, ALLOCATE, ALTER, AND, ANY, ARE, AS, ASC,
ASSERTION, AT, AUTHORIZATION, AVG

BEGIN, BETWEEN, BIT, BIT_LENGTH, BOTH, BY

CASCADE, CASCADED, CASE, CAST, CATALOG, CHAR, CHARACTER, CHAR_LENGTH,
CHARACTER_LENGTH, CHECK, CLOSE, COALESCE, COLLATE, COLLATION, COLUMN,
COMMIT, CONNECT, CONNECTION, CONSTRAINT, CONSTRAINTS, CONTINUE, CONVERT,
CORRESPONDING, COUNT, CREATE, CROSS, CURRENT, CURRENT_DATE, CURRENT_TIME,
CURRENT_TIMESTAMP, CURRENT_USER, CURSOR

DATE, DAY, DEALLOCATE, DEC, DECIMAL, DECLARE, DEFAULT, DEFERRABLE, DEFERRED,
DELETE, DESC, DESCRIBE, DESCRIPTOR, DIAGNOSTICS, DISCONNECT, DISTINCT,
DOMAIN, DOUBLE, DROP

ELSE, END, END-EXEC, ESCAPE, EXCEPT, EXCEPTION, EXEC, EXECUTE, EXISTS,
EXTERNAL, EXTRACT

FALSE, FETCH, FIRST, FLOAT, FOR, FOREIGN, FOUND, FROM, FULL

GET, GLOBAL, GO, GOTO, GRANT, GROUP

HAVING, HOUR

IDENTITY, IMMEDIATE, IN, INDICATOR, INITIALLY, INNER, INPUT, INSENSITIVE,
INSERT, INT, INTEGER, INTERSECT, INTERVAL, INTO, IS, ISOLATION

JOIN

KEY

LANGUAGE, LAST, LEADING, LEFT, LEVEL, LIKE, LOCAL, LOWER

MATCH, MAX, MIN, MINUTE, MODULE, MONTH

NAMES, NATIONAL, NATURAL, NCHAR, NEXT, NO, NOT, NULL, NULLIF, NUMERIC

OCTET_LENGTH, OF, ON, ONLY, OPEN, OPTION, OR, ORDER, OUTER, OUTPUT, OVERLAPS

PAD, PARTIAL, POSITION, PRECISION, PREPARE, PRESERVE, PRIMARY, PRIOR,
PRIVILEGES, PROCEDURE, PUBLIC

READ, REAL, REFERENCES, RELATIVE, RESTRICT, REVOKE, RIGHT, ROLLBACK, ROWS

SCHEMA, SCROLL, SECOND, SECTION, SELECT, SESSION, SESSION_USER, SET, SIZE,
SMALLINT, SOME, SPACE, SQL, SQLCODE, SQLERROR, SQLSTATE, SUBSTRING, SUM,
SYSTEM_USER

TABLE, TEMPORARY, THEN, TIME, TIMESTAMP, TIMEZONE_HOUR, TIMEZONE_MINUTE,
TO, TRAILING, TRANSACTION, TRANSLATE, TRANSLATION, TRIM, TRUE

UNION, UNIQUE, UNKNOWN, UPDATE, UPPER, USAGE, USER, USING

VALUE, VALUES, VARCHAR, VARYING, VIEW

WHEN, WHENEVER, WHERE, WITH, WORK, WRITE

YEAR

ZONE




Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma