Re: Query in SQL statement

Поиск
Список
Период
Сортировка
Искать
От
Jim C. Nasby
Тема
Re: Query in SQL statement
Дата
Msg-id
20050930234853.GT40138@pervasive.com
Ответ на
Re: Query in SQL statement (Christopher Kings-Lynne)
Список
Дерево обсуждения
Query in SQL statement "R, Rajesh (STSD)" <rajesh.r2@hp.com>
Re: Query in SQL statement Christopher Kings-Lynne <chriskl@familyhealth.com.au>
Re: Query in SQL statement "Jim C. Nasby" <jnasby@pervasive.com>
Re: [PERFORM] Query in SQL statement Richard Huxton <dev@archonet.com>
On Thu, Sep 29, 2005 at 09:28:38PM +0800, Christopher Kings-Lynne wrote:
> 
> >CREATE SEQUENCE ai_id;
> >CREATE TABLE badusers (
> >  id int DEFAULT nextval('ai_id') NOT NULL,
> >  UserName varchar(30),
> >  Date  datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
> >  Reason varchar(200),
> >  Admin varchar(30) DEFAULT '-',
> >  PRIMARY KEY (id),
> >  KEY UserName (UserName),
> >  KEY Date (Date)
> >);
> >
> >
> >Am always getting foll. Errors,
> >
> >ERROR:  relation "ai_id" already exists
> >ERROR:  syntax error at or near "(" at character 240
> 
> You have just copied the Mysql code to Postgresql.  It will in no way 
> work.  Your default for 'Date' is illegal in postgresql and hence it 
> must allow NULLs.  There is no such thing as a 'datetime' type.  There 
> is no such thing as 'Key'.  Also your mixed case identifiers won't be 
> preserved.  You want:
> 
> CREATE TABLE badusers (
>   id SERIAL PRIMARY KEY,
>   UserName varchar(30),
>   Date  timestamp,
>   Reason varchar(200),
>   Admin varchar(30) DEFAULT '-'
> );
> 
> CREATE INDEX UserName_Idx ON badusers(Username);
> CREATE INDEX Date_Idx ON badusers(Date);

Actually, to preserve the case you can wrap everything in quotes:
CREATE ...
    "UserName" varchar(30)

Of course that means that now you have to do that in every statement
that uses that field, too...

SELECT username FROM badusers
ERROR

SELECT "UserName" FROM badusers
bad user

I suggest ditching the CamelCase and going with underline_seperators.
I'd also not use the bareword id, instead using bad_user_id. And I'd
name the table bad_user. But that's just me. :)
-- 
Jim C. Nasby, Sr. Engineering Consultant      jnasby@pervasive.com
Pervasive Software      http://pervasive.com    work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf       cell: 512-569-9461
В списке pgsql-hackers по дате отправления
От: Jim C. Nasby
Дата:
От: Dann Corbit
Дата:
FAQ