Skip to Main Content
PostgreSQL 9 Administration Cookbook - Second Edition
book

PostgreSQL 9 Administration Cookbook - Second Edition

by Simon Riggs, GIANNI CIOLLI, Hannu Krosing, Gabriele Bartolini
April 2015
Intermediate to advanced content levelIntermediate to advanced
504 pages
11h 41m
English
Packt Publishing
Content preview from PostgreSQL 9 Administration Cookbook - Second Edition

Preventing duplicate rows

Preventing duplicate rows is one of the most important aspects of data quality for any database. PostgreSQL offers some useful features in this area, extending beyond most relational databases.

Getting ready

Identify the set of columns that you wish to make unique. Does this apply to all rows, or just a subset of rows?

Let's start with our example table:

postgres=# SELECT * FROM newcust;
 customerid
------------
          1
          2
          3
          4
(4 rows)

How to do it…

To prevent duplicate rows, we need to create a unique index that the database server can use to enforce uniqueness of a particular set of columns. We can do this in the following three similar ways for basic data types:

  • Create a primary key constraint on the set of columns. We are ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PostgreSQL 10 Administration Cookbook - Fourth Edition

PostgreSQL 10 Administration Cookbook - Fourth Edition

Simon Riggs, Gianni Ciolli
Mastering PostgreSQL 10

Mastering PostgreSQL 10

Hans-Jürgen Schönig

Publisher Resources

ISBN: 9781849519069Supplemental Content