113 lines
2.4 KiB
Plaintext
113 lines
2.4 KiB
Plaintext
//// -- LEVEL 1
|
|
//// -- Schemas, Tables and References
|
|
|
|
// Creating tables
|
|
// You can define the tables with full schema names
|
|
Table ecommerce.merchants {
|
|
id int
|
|
~country_code
|
|
merchant_name varchar
|
|
|
|
"created at" varchar
|
|
admin_id int [ref: > U.id, not null]
|
|
|
|
Indexes {
|
|
(id, country_code) [pk]
|
|
}
|
|
}
|
|
|
|
// If schema name is omitted, it will default to "public" schema.
|
|
Table users as U {
|
|
id int [pk, increment] // auto-increment
|
|
full_name varchar
|
|
created_at timestamp
|
|
~country_code
|
|
}
|
|
|
|
Table countries {
|
|
code int [pk]
|
|
name varchar
|
|
continent_name varchar
|
|
}
|
|
|
|
TablePartial country_code {
|
|
country_code int [ref: > countries.code]
|
|
}
|
|
|
|
//----------------------------------------------//
|
|
|
|
//// -- LEVEL 2
|
|
//// -- Adding column settings
|
|
|
|
Table ecommerce.order_items {
|
|
order_id int [ref: > ecommerce.orders.id] // inline relationship (many-to-one)
|
|
product_id int
|
|
quantity int [default: 1] // default value
|
|
Indexes {
|
|
(order_id, product_id) [pk]
|
|
}
|
|
}
|
|
|
|
Ref: ecommerce.order_items.product_id > ecommerce.products.id
|
|
|
|
Table ecommerce.orders {
|
|
~auto_id
|
|
user_id int [not null, unique]
|
|
status varchar
|
|
created_at varchar [note: 'When order created'] // add column note
|
|
}
|
|
|
|
//----------------------------------------------//
|
|
|
|
//// -- Level 3
|
|
//// -- Enum, Indexes
|
|
|
|
// Enum for 'products' table below
|
|
Enum ecommerce.products_status {
|
|
out_of_stock
|
|
in_stock
|
|
running_low [note: 'less than 20'] // add column note
|
|
}
|
|
|
|
// Indexes: You can define a single or multi-column index
|
|
Table ecommerce.products {
|
|
~auto_id
|
|
name varchar
|
|
merchant_id int [not null]
|
|
price int
|
|
status ecommerce.products_status
|
|
created_at datetime [default: `now()`]
|
|
|
|
Indexes {
|
|
(merchant_id, status) [name:'product_status']
|
|
id [unique]
|
|
}
|
|
}
|
|
|
|
Table ecommerce.product_tags {
|
|
~auto_id
|
|
name varchar
|
|
}
|
|
|
|
Table ecommerce.merchant_periods {
|
|
~auto_id
|
|
merchant_id int
|
|
~country_code
|
|
start_date datetime
|
|
end_date datetime
|
|
}
|
|
|
|
TablePartial auto_id {
|
|
id int [pk]
|
|
}
|
|
|
|
// Creating references
|
|
// You can also define relationship separately
|
|
// > many-to-one; < one-to-many; - one-to-one; <> many-to-many
|
|
Ref: ecommerce.products.merchant_id > ecommerce.merchants.id // many-to-one
|
|
Ref: ecommerce.product_tags.id <> ecommerce.products.id // many-to-many
|
|
|
|
// Composite foreign key
|
|
Ref: ecommerce.merchant_periods.(merchant_id, country_code) > ecommerce.merchants.(id, country_code)
|
|
Ref user_orders: ecommerce.orders.user_id > public.users.id
|