Question

J'ai une table dans PostgreSQL où le schéma ressemble à ceci:

CREATE TABLE "foo_table" (
    "id" serial NOT NULL PRIMARY KEY,
    "permalink" varchar(200) NOT NULL,
    "text" varchar(512) NOT NULL,
    "timestamp" timestamp with time zone NOT NULL
)

Maintenant, je veux rendre le permalien unique à travers la table en modifiant la table. Quelqu'un peut-il m'aider avec cela?

TIA

Était-ce utile?

La solution

Je l'ai compris dans la documentation PostgreSQL, la syntaxe exacte est la suivante:

ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn);

Merci Fred .

Autres conseils

Ou demandez à la base de données d'attribuer automatiquement un nom de contrainte à l'aide de:

ALTER TABLE foo ADD UNIQUE (thecolumn);

il est également possible de créer une contrainte unique de plus d'une colonne:

ALTER TABLE the_table 
    ADD CONSTRAINT constraint_name UNIQUE (column1, column2);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top