【问题标题】:Postgresql ALTER TABLE ADD KEY equivalentPostgresql ALTER TABLE ADD KEY 等效项
【发布时间】:2021-03-12 18:48:46
【问题描述】:

在 postgresql 中这个等价物是什么?

--
-- Index for the table `Artist`
--
ALTER TABLE `Artist`
    ADD PRIMARY KEY (`idArtist`),
    ADD UNIQUE KEY `name` (`name`,`firstName`);
    ADD KEY `idFilm` (`idFilm`);

【问题讨论】:

    标签: postgresql alter-table


    【解决方案1】:

    添加主键也是一样。

    alter table artist
      add primary key (idartist);
    

    对于唯一约束,您有两种选择:

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

    或唯一索引:

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

    我认为key 的东西,只是添加了一个常规索引:

    create index idfilm on artist (idfilm);
    

    【讨论】:

    • 唯一约束在幕后实现为唯一索引。不同之处在于索引由表“拥有”,因此如果删除拥有的表,它会自动删除。
    • @DarwinvonCorax:如果您创建唯一约束,它也会在您删除表时自动删除(包括隐式创建的索引)
    • 感谢您的回答!只是语法错误,需要在字段名前提供关键字INDEX:CREATE UNIQUE INDEX "name" on "Artist" ("name","firstName");documentation
    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 2016-02-06
    • 2011-02-06
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多