11.6. Unique Indexes

|      11.6. Unique Indexes       |                  |                   |                  |                                  |
|:-------------------------------:|------------------|-------------------|------------------|----------------------------------|
|[Prev](indexes-bitmap-scans.html)|[Up](indexes.html)|Chapter 11. Indexes|[Home](index.html)| [Next](indexes-expressional.html)|

---

## 11.6. Unique Indexes

[]()

 Indexes can also be used to enforce uniqueness of a column's value, or the uniqueness of the combined values of more than one column.

```
CREATE UNIQUE INDEX name ON table (column [, ...]);

```

 Currently, only B-tree indexes can be declared unique.

 When an index is declared unique, multiple table rows with equal indexed values are not allowed. Null values are not considered equal. A multicolumn unique index will only reject cases where all indexed columns are equal in multiple rows.

PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table. The index covers the columns that make up the primary key or unique constraint (a multicolumn index, if appropriate), and is the mechanism that enforces the constraint.

### Note

 There's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index.

---

|[Prev](indexes-bitmap-scans.html)|[Up](indexes.html)| [Next](indexes-expressional.html)|
|:--------------------------------|:----------------:|---------------------------------:|
|11.5. Combining Multiple Indexes |[Home](index.html)|   11.7. Indexes on Expressions   |

 // \<![CDATA[ \<-- For SVG support if ('WebSocket' in window) { (function() { function refreshCSS() { var sheets = [].slice.call(document.getElementsByTagName("link")); var head = document.getElementsByTagName("head")[0]; for (var i = 0; i \< sheets.length; ++i) { var elem = sheets[i]; head.removeChild(elem); var rel = elem.rel; if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") { var url = elem.href.replace(/(&|\\?)\_cacheOverride=\\d+/, ''); elem.href = url + (url.indexOf('?') \>= 0 ? '&' : '?') + '\_cacheOverride=' + (new Date().valueOf()); } head.appendChild(elem); } } var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://'; var address = protocol + window.location.host + window.location.pathname + '/ws'; var socket = new WebSocket(address); socket.onmessage = function(msg) { if (msg.data == 'reload') window.location.reload(); else if (msg.data == 'refreshcss') refreshCSS(); }; console.log('Live reload enabled.'); })(); } // ]]\>