Add documentation for create-index

This commit is contained in:
Hannu Hartikainen 2023-12-08 13:25:10 +02:00
parent e70e3713fc
commit 45d1230102

View file

@ -98,7 +98,7 @@ names: the "from" and the "to" names.
> Note: `:modify-column` is MySQL-specific and should be considered legacy and deprecated. `:alter-column` will produce `MODIFY COLUMN` when the MySQL dialect is selected.
### add-index, drop-index
### add-index, drop-index, create-index
Used with `:alter-table`,
`:add-index` accepts a single (function) expression
@ -125,6 +125,22 @@ user=> (-> (h/alter-table :fruit)
["ALTER TABLE fruit ADD PRIMARY KEY(id)"]
```
Some databases treat the standalone `:create-index` differently (e.g. PostgreSQL) while some treat it as an alias to `:alter-table` `:add-index` (e.g. MySQL). It accepts a pair of index specification and column specification:
```clojure
user=> (sql/format {:create-index [:my-idx [:fruit :appearance]]})
["CREATE INDEX my_idx ON fruit (appearance)"]
user=> (sql/format {:create-index [[:unique :another-idx] [:fruit :color :appearance]]})
["CREATE UNIQUE INDEX another_idx ON fruit (color, appearance)"]
```
PostgreSQL supports IF NOT EXISTS and expressions instead of columns. This may make `:create-index` more useful than `:add-index`:
```clojure
user=> (sql/format (h/create-index [:unique :another-idx :if-not-exists] [:fruit :color :%lower.appearance]))
["CREATE UNIQUE INDEX IF NOT EXISTS another_idx ON fruit (color, LOWER(appearance))"]
```
### rename-table
Used with `:alter-table`,