-2

What is the equivalent of this in postgresql ?

--
-- Index for the table `Artist`
--
ALTER TABLE `Artist`
    ADD PRIMARY KEY (`idArtist`),
    ADD UNIQUE KEY `name` (`name`,`firstName`);
    ADD KEY `idFilm` (`idFilm`);
Izaya
  • 1,270
  • 2
  • 13
  • 31

1 Answers1

1

Adding a primary key is the same.

alter table artist
  add primary key (idartist);

For the unique constraint you have two choices:

alter table artist
  add constraint name unique (name, firstname);

or a unique index:

create unique index name on artist (name, firstname);

I think the key thing, simply adds a regular index:

create index idfilm on artist (idfilm);
  • A unique constraint is implemented behind the scenes as a unique index. The difference is that the index is "owned" by the table, so it gets dropped automagically if the owning table is dropped. – Darwin von Corax Mar 12 '21 at 19:15
  • @DarwinvonCorax: if you create a unique constraint it is also automatically dropped when you drop the table (including the index that was created implicitly) –  Mar 12 '21 at 19:36
  • Thank's for you answer! Just a syntaxe error, you need to provide the keyword INDEX before the field name: `CREATE UNIQUE INDEX "name" on "Artist" ("name","firstName");` [documentation](https://www.postgresql.org/docs/9.3/indexes-unique.html) – Izaya Mar 12 '21 at 20:47