dbTalk Databases Forums  

Re: alter table ... alter column ... set unique

comp.databases.postgresql comp.databases.postgresql


Discuss Re: alter table ... alter column ... set unique in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-30-2007 , 02:17 AM






begin Thomas Guettler schrieb:
Quote:
Hi,

CREATE TABLE ...
"file" varchar(100) NOT NULL UNIQUE

unfortunately I forgot to use "UNIQUE" when I created the table.

I searched the docs, but only found a way to alter if the column has an
index.

How can I alter the table if the column has no index?
Create a new unique index for the particular column.

Btw.: Hi Thomas, how are you?


end
Andreas
--
Andreas Kretschmer
Linux - weil ich es mir wert bin!
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net


Reply With Quote
  #2  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-30-2007 , 11:02 AM






Thomas Guettler <guettli.usenet (AT) thomas-guettler (DOT) de> wrote:
Quote:
CREATE TABLE ...
"file" varchar(100) NOT NULL UNIQUE

unfortunately I forgot to use "UNIQUE" when I created the table.

I searched the docs, but only found a way to alter if the column has an
index.

How can I alter the table if the column has no index?

Create a new unique index for the particular column.
I'd recommend that you add a unique constraint rather than an unique index.

Both add an index, but this way there is also a constraint definition.

Syntax:

ALTER TABLE t ADD CONSTRAINT indexname UNIQUE (file);

Yours,
Laurenz Albe


Reply With Quote
  #3  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-30-2007 , 11:41 AM



begin Laurenz Albe <invite (AT) spam (DOT) to.invalid> wrote:
Quote:
Thomas Guettler <guettli.usenet (AT) thomas-guettler (DOT) de> wrote:
CREATE TABLE ...
"file" varchar(100) NOT NULL UNIQUE

unfortunately I forgot to use "UNIQUE" when I created the table.

I searched the docs, but only found a way to alter if the column has an
index.

How can I alter the table if the column has no index?

Create a new unique index for the particular column.

I'd recommend that you add a unique constraint rather than an unique index.

Both add an index, but this way there is also a constraint definition.

Syntax:

ALTER TABLE t ADD CONSTRAINT indexname UNIQUE (file);
Always the same:

test=# create table u (i int, v text);
CREATE TABLE
test=*# alter table u add constraint con_u unique (i);
NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "con_u" for table "u"
ALTER TABLE
test=*# \d u
Table "public.u"
Column | Type | Modifiers
--------+---------+-----------
i | integer |
v | text |
Indexes:
"con_u" UNIQUE, btree (i)

test=*# rollback;
ROLLBACK
test=# create table u (i int, v text);
CREATE TABLE
test=*# create unique index idx_u on u(i);
CREATE INDEX
test=*# \d u
Table "public.u"
Column | Type | Modifiers
--------+---------+-----------
i | integer |
v | text |
Indexes:
"idx_u" UNIQUE, btree (i)

test=*# rollback;
ROLLBACK
test=# create table u (i int unique, v text);
NOTICE: CREATE TABLE / UNIQUE will create implicit index "u_i_key" for table "u"
CREATE TABLE
test=*# \d u
Table "public.u"
Column | Type | Modifiers
--------+---------+-----------
i | integer |
v | text |
Indexes:
"u_i_key" UNIQUE, btree (i)



I can't see any differences. (except for the index-names)


Okay, another try:

test=# create table u (i int, v text);
CREATE TABLE
test=*# create unique index idx_u on u(i);
CREATE INDEX
test=*# insert into u values (1, 'foo');
INSERT 0 1
test=*# insert into u values (1, 'foo');
ERROR: duplicate key violates unique constraint "idx_u"


We have an unique index and this enforces an unique constraint.



end
Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°


Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-31-2007 , 04:22 AM



Andreas Kretschmer <akretschmer (AT) spamfence (DOT) net> wrote:
Quote:
CREATE TABLE ...
"file" varchar(100) NOT NULL UNIQUE

unfortunately I forgot to use "UNIQUE" when I created the table.

I searched the docs, but only found a way to alter if the column has an
index.

How can I alter the table if the column has no index?

Create a new unique index for the particular column.

I'd recommend that you add a unique constraint rather than an unique index.

Both add an index, but this way there is also a constraint definition.

I can't see any differences. (except for the index-names)
Ok, here's the difference:

test=> CREATE TABLE a(id INTEGER CONSTRAINT id_unique UNIQUE);
NOTICE: CREATE TABLE / UNIQUE will create implicit index "id_unique"
for table "a"
CREATE TABLE

test=> SELECT conname, contype, conkey FROM pg_constraint
test-> WHERE conname='id_unique';

conname | contype | conkey
-----------+---------+--------
id_unique | u | {1}
(1 row)

test=> DROP TABLE a;
DROP TABLE

test=> CREATE TABLE a(id INTEGER);
CREATE TABLE

test=> CREATE UNIQUE INDEX id_unique ON a(id);
CREATE INDEX

test=> SELECT conname, contype, conkey FROM pg_constraint
test-> WHERE conname='id_unique';

conname | contype | conkey
---------+---------+--------
(0 rows)

Yours,
Laurenz Albe


Reply With Quote
  #5  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-31-2007 , 04:41 AM



begin Laurenz Albe schrieb:
Quote:
Ok, here's the difference:
Oh, thanks for the clarification.



end
Andreas
--
Andreas Kretschmer
Linux - weil ich es mir wert bin!
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net


Reply With Quote
  #6  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-31-2007 , 08:43 AM



Andreas Kretschmer <akretschmer (AT) spamfence (DOT) net> wrote:
Quote:
Oh, thanks for the clarification.
Klar, gerne.
Irgendwie komisch, einen Thread mit 3 deutschsprachigen Leuten auf
Englisch zu haben :^)

Laurenz


Reply With Quote
  #7  
Old   
Andreas Kretschmer
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-31-2007 , 09:14 AM



begin Laurenz Albe schrieb:
Quote:
Andreas Kretschmer <akretschmer (AT) spamfence (DOT) net> wrote:
Oh, thanks for the clarification.

Klar, gerne.
Irgendwie komisch, einen Thread mit 3 deutschsprachigen Leuten auf
Englisch zu haben :^)
Hehe, daß dachte ich auch schon die ganze Zeit...


end
Andreas
--
Andreas Kretschmer
Linux - weil ich es mir wert bin!
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net


Reply With Quote
  #8  
Old   
Robert Klemme
 
Posts: n/a

Default Re: alter table ... alter column ... set unique - 05-31-2007 , 09:33 AM



On 31.05.2007 16:14, Andreas Kretschmer wrote:
Quote:
begin Laurenz Albe schrieb:
Andreas Kretschmer <akretschmer (AT) spamfence (DOT) net> wrote:
Oh, thanks for the clarification.
Klar, gerne.
Irgendwie komisch, einen Thread mit 3 deutschsprachigen Leuten auf
Englisch zu haben :^)

Hehe, daß dachte ich auch schon die ganze Zeit...
Well, it's an English newsgroup - so there might actually be more people
reading this thread than you posters and *they* will likely benefit from
English prose. :-)

Kind regards

robert


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.