Subject | unique index problem: 'emails' table |
---|---|
Author | David Garamond |
Post date | 2003-11-23T07:50:14Z |
I am trying to create a table to store unique email addresses. I want to
separate the local and the host part ("local@host") since they
(especially host) appear often in WHERE clauses and thus indexing both
parts would be a benefit.
create table emails (
local varchar(126) not null,
host varchar(72) not null,
email computed by (local||'@'||host)
);
create index emails_local on emails(local);
create index emails_host on emails(host);
Now, for this table, the problem is expressing unique constraint for
'email'. This won't work of course since computed fields currently
cannot be indexed:
unique(email)
This also won't work because '@' is not the name of a field:
unique(local, '@', host)
This works but is incorrect since it can mistakenly say that
'sean@...' and 'seanwilliam@...' are duplicates:
unique(local, host)
There are several alternatives, none is perfect, but so I think far the
best compromise one is:
create table emails (
local varchar(124) not null,
ampersand char(1) default '@' not null check (ampersand='@'),
host varchar(72) not null,
email computed by (local||ampersand||host)
);
Any other better alternatives?
--
dave
separate the local and the host part ("local@host") since they
(especially host) appear often in WHERE clauses and thus indexing both
parts would be a benefit.
create table emails (
local varchar(126) not null,
host varchar(72) not null,
email computed by (local||'@'||host)
);
create index emails_local on emails(local);
create index emails_host on emails(host);
Now, for this table, the problem is expressing unique constraint for
'email'. This won't work of course since computed fields currently
cannot be indexed:
unique(email)
This also won't work because '@' is not the name of a field:
unique(local, '@', host)
This works but is incorrect since it can mistakenly say that
'sean@...' and 'seanwilliam@...' are duplicates:
unique(local, host)
There are several alternatives, none is perfect, but so I think far the
best compromise one is:
create table emails (
local varchar(124) not null,
ampersand char(1) default '@' not null check (ampersand='@'),
host varchar(72) not null,
email computed by (local||ampersand||host)
);
Any other better alternatives?
--
dave