Create Tables
Common examples of CREATE TABLE statements are presented below.
1CREATE TABLE IF NOT EXISTS customers (
2 id INTEGER,
3 customer_name VARCHAR[60],
4 email VARCHAR[150],
5 address VARCHAR,
6 city VARCHAR,
7 ip VARCHAR[40],
8 country VARCHAR[15],
9 age INTEGER,
10 active BOOLEAN,
11 PRIMARY KEY (id)
12);
13
14CREATE TABLE IF NOT EXISTS products (
15 id INTEGER,
16 product VARCHAR NOT NULL,
17 price VARCHAR NOT NULL,
18 created_at TIMESTAMP,
19 PRIMARY KEY (id)
20);
21
22CREATE TABLE IF NOT EXISTS orders (
23 id INTEGER AUTO_INCREMENT,
24 customerid INTEGER,
25 productid INTEGER,
26 created_at TIMESTAMP,
27 PRIMARY KEY id
28);
29
30CREATE TABLE customer_review(
31 customerid INTEGER,
32 productid INTEGER,
33 review VARCHAR,
34 created_at TIMESTAMP,
35 PRIMARY KEY (customerid, productid)
36);IF NOT EXISTS#
With this clause the CREATE TABLE statement will not fail if a table with same name already exists.
Note: If the table already exists, it is not compared against the provided table definition neither it is updated to match it.
NOT NULL#
Columns marked as not null can not have a null value assigned.
PRIMARY KEY#
Every table in immudb must have a primary key. Primary key can use at least 1 and up to 8 columns.
Columns used in a primary key can not have NULL values assigned,
even if those columns are not explicitly marked as NOT NULL.
Primary key creates an implicit unique index on all contained columns.
AUTO_INCREMENT#
A single-column PRIMARY KEY can be marked as AUTO_INCREMENT.
immudb will automatically set a unique value of this column for new rows.
When inserting data into a table with an INSERT statement,
the value for such primary key must be omitted.
When updating data in such table with UPSERT statement,
the value for such primary key is obligatory
and the UPSERT statement can only update existing rows.
The type of an AUTO_INCREMENT column must be INTEGER.
Internally immudb will assign sequentially increasing values for new rows
ensuring this value is unique within a single table.
Foreign keys#
Explicit support for relations to foreign tables is not currently supported in immudb.
It is possible however to create ordinary columns containing foreign key values that can be used in JOIN statements.
Application logic is responsible for ensuring data consistency and foreign key constraints.
1SELECT * FROM orders
2INNER JOIN customers ON customers.id = orders.customerid
3INNER JOIN products ON products.id = orders.productid;