2022-10-31 05:52:27 +03:00
# CREATE DATABASE
2023-01-05 21:25:45 +03:00
2022-10-31 05:52:27 +03:00
```
CREATE DATABASE < database-name >
```
2022-08-06 17:06:18 +03:00
2023-01-05 21:25:45 +03:00
Example:
2022-08-06 17:06:18 +03:00
`CREATE DATABASE my-database`
2022-08-14 00:20:30 +03:00
Discussion:
2023-01-05 21:25:45 +03:00
`CREATE DATABASE` must be the only command in a script. The script will fail if there are prior commands. As the first command it will succeed and subsequent commands will be ignored.
2022-08-14 00:20:30 +03:00
2023-01-05 21:25:45 +03:00
API:
```
+$ create-database $:([%create-database name=@t])
```
2022-08-06 17:06:18 +03:00
2022-10-31 05:52:27 +03:00
# CREATE INDEX
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
CREATE [ UNIQUE ] [ NONCLUSTERED | CLUSTERED ] INDEX < index-name >
ON [ < db-qualifer > ]{ < table-name > | < view-name > }
( < column-name > [ ASC | DESC ] [ ,...n ] )
```
2022-08-06 17:06:18 +03:00
Examples:
2022-08-10 23:23:12 +03:00
```
CREATE INDEX ix_vendor-id ON product-vendor (vendor-id);
2023-01-05 21:25:45 +03:00
CREATE UNIQUE INDEX ix_vendor-id2 ON dbo.product-vendor
2022-08-10 23:23:12 +03:00
(vendor-id DESC, name ASC, address DESC);
CREATE INDEX ix_vendor-id3 ON purchasing..product-vendor (vendor-id);
```
2022-08-06 17:06:18 +03:00
Discussion:
2022-08-10 21:45:17 +03:00
Index name cannot start with 'pk-' as these names are internally reserved for primary keys.
2022-08-06 17:06:18 +03:00
A table or view can only have up to one `CLUSTERED` index, including the primary key for tables.
The `UNIQUE` option is not available for views.
2023-01-05 21:25:45 +03:00
API:
```
+$ create-index
$:
%create-index
name=@t
object-name=qualified-object :: because index can be over table or view
is-unique=?
is-clustered=?
columns=(list ordered-column)
==
```
2022-08-06 17:06:18 +03:00
2022-10-31 05:52:27 +03:00
# CREATE NAMESPACE
2022-08-06 17:06:18 +03:00
2022-08-10 21:45:17 +03:00
`CREATE NAMESPACE [<database-name>.]<namespace-name>`
2022-08-06 17:06:18 +03:00
2023-01-05 21:25:45 +03:00
Example:
2022-08-06 17:06:18 +03:00
`CREATE NAMESPACE my-namespace`
2023-01-05 21:25:45 +03:00
API:
```
+$ create-namespace $:([%create-namespace database-name=@t name=@t])
```
2022-08-06 17:06:18 +03:00
2022-10-31 05:52:27 +03:00
# CREATE PROCEDURE
2022-08-06 17:06:18 +03:00
2022-08-21 22:12:55 +03:00
```
2022-08-29 21:29:37 +03:00
CREATE { PROC | PROCEDURE }
2022-08-21 22:12:55 +03:00
[< db-qualifer > ]< procedure-name >
[ { #< parameter-name > < data-type > } ] [ ,...n ]
2022-10-31 05:52:27 +03:00
AS { < urql command > ; | *hoon } [ ;...n ]
2022-08-21 22:12:55 +03:00
```
Discussion:
TBD
2022-08-29 21:29:37 +03:00
Cannot be used to create database.
2022-08-21 22:12:55 +03:00
2022-10-31 05:52:27 +03:00
# CREATE TABLE
2022-08-21 22:12:55 +03:00
2022-08-10 23:23:12 +03:00
```
CREATE TABLE
[ < db-qualifer > ]< table-name >
2022-08-25 22:21:07 +03:00
( { < column-name > < aura > }
2022-08-10 23:23:12 +03:00
[ ,... n ] )
PRIMARY KEY [ NONCLUSTERED | CLUSTERED ] ( < column-name > [ ,... n ] )
[ { FOREIGN KEY < foreign-key-name > ( < column-name > [ ASC | DESC ] [ ,... n ] )
REFERENCES [ < namespace-name > . ] < table-name > ( < column-name > [ ,... n ]
2022-08-29 21:29:37 +03:00
[ ON DELETE { NO ACTION | CASCADE } ]
[ ON UPDATE { NO ACTION | CASCADE } ] }
2022-08-10 23:23:12 +03:00
[ ,... n ] ]`
```
2022-08-06 17:06:18 +03:00
Example:
2022-08-10 23:23:12 +03:00
```
CREATE TABLE order-detail
(invoice-nbr @ud , line-item @ud , product-id @ud , special-offer-id @ud , message @t )
PRIMARY KEY CLUSTERED (invoice-nbr, line-item)
FOREIGN KEY fk-special-offer-order-detail (productid, specialofferid)
REFERENCES special-offer (product-id, special-offer-id)
```
2022-08-06 17:06:18 +03:00
Discussion:
`PRIMARY KEY` must be unique.
`SET NULL` only applies to columns defined as `unit` . Columns defined otherwise will be treated as the default `NO ACTION` .
`SET DEFAULT` applies the column's default constant, if available, otherwise the bunt of the aura.
2023-01-05 21:25:45 +03:00
API:
```
+$ create-table
$:
%create-table
table=qualified-object
columns=(list column)
primary-key=create-index
foreign-keys=(list foreign-key)
==
```
2022-08-06 17:06:18 +03:00
2022-10-31 05:52:27 +03:00
# CREATE TRIGGER
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
CREATE TRIGGER [ < db-qualifer > ]< trigger-name >
ON { < table-name > | < view-name > }
[ ENABLE | DISABLE ]
```
2022-08-10 21:45:17 +03:00
TBD hoon triggers
2022-08-06 17:06:18 +03:00
Discussion:
Not for initial release.
2022-10-31 05:52:27 +03:00
# CREATE TYPE
2022-08-06 17:06:18 +03:00
`CREATE TYPE <type-name>`
TBD
Discussion:
Probably will be available only at server (ship) level, and so shared by all databases.
Possibly part of initial or early release.
2022-10-31 05:52:27 +03:00
# CREATE VIEW
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
`CREATE VIEW [ <db-qualifer> ]<view-name> AS <query>`
2022-08-06 17:06:18 +03:00
Discussion:
Views are read only.
2023-01-05 21:25:45 +03:00
When a column is derived from an arithmetic expression, a function, or a constant; or when two or more columns may otherwise have the same name, typically because of a JOIN; distinct column names must be assigned in the SELECT statement using AS. Otherwise view columns acquire the same names as the columns in the SELECT statement.
API:
```
+$ create-view
$:
%create-view
view=qualified-object
query=query
==
```