Constraints
A constraint is a rule the database enforces on a table's rows: a primary key identifies each row uniquely, a unique constraint prevents duplicates, a foreign key keeps a reference pointing at an existing row in another table, and a check constraint validates values against an expression. The Constraint API adds, inspects and drops these on Centia.io tables without writing DDL, and constraints can also be declared inline when the table is created. A primary key matters in particular for the Feature API and WFS-T editing, which address rows by their key.
Data types are a way to limit the kind of data that can be stored in a table. For many applications, however, the constraint they provide is too coarse. For example, a column containing a product price should probably only accept positive values. But there is no standard data type that accepts only positive numbers. Another issue is that you might want to constrain column data with respect to other columns or rows. For example, in a table containing product information, there should be only one row for each product number.
Add constraint
A constraint can be of either type primary|unique|foreign|check.
- Web App
- HTTP
- CLI
POST https://api.centia.io/api/v4/schemas/rockhall/tables/inductees/constraints HTTP/1.1
Content-Type: application/json
Authorization: Bearer abc123
{
"name": "inductees-id-primary",
"constraint": "primary",
"columns": ["id"]
}
centia constraint add "rockhall" "inductees" "id" unique "inductees-id-primary"
Get constraint
You can either get a single constraint or a collection of constraints.
- Web App
- HTTP
- CLI
GET https://api.centia.io/api/v4/schemas/rockhall/tables/inductees/constraints HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123
GET https://api.centia.io/api/v4/schemas/rockhall/tables/inductees/constraints/inductees-id-primary HTTP/1.1
Accept: application/json; charset=utf-8
Authorization: Bearer abc123
centia constraint get "rockhall" "inductees" "inductees-id-primary"
An index object looks like this.
{
"name": "inductees-id-primary",
"constraint": "unique",
"columns": ["id"]
}
Delete constraint
- Web App
- HTTP
- CLI
DELETE https://api.centia.io/api/v4/schemas/rockhall/tables/inductees/constraints/inductees-id-primary HTTP/1.1
centia constraint drop "rockhall" "inductees" "inductees-id-primary"