| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // -- https://dbdiagram.io/home
- //// -- LEVEL 1
- //// -- Tables and References
- // Creating tables
- Table users as U {
- id int
- full_name varchar
- created_at timestamp
- country_code int
- }
- Table merchants {
- id int
- merchant_name varchar
- country_code int
- "created at" varchar
- admin_id int [ref: > U.id] // inline relationship (many-to-one)
- }
- Table countries {
- code int
- name varchar
- continent_name varchar
- }
- // Creating references
- // You can also define relaionship separately
- // > many-to-one; < one-to-many; - one-to-one
- Ref: U.country_code > countries.code
- Ref: merchants.country_code > countries.code
- //----------------------------------------------//
- //// -- LEVEL 2
- //// -- Adding column settings
- Table order_items {
- order_id int [increment, ref: > orders.id] // auto-increment
- product_id int
- quantity int [default: 1] // default value
- }
- Ref: order_items.product_id > products.id
- Table orders {
- id int [pk] // primary key
- 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 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 products {
- id int [pk]
- name varchar
- merchant_id int [not null]
- price int
- status products_status
- created_at datetime [default: `now()`]
- Indexes {
- (merchant_id, status) [name:'product_status']
- id [unique]
- }
- }
- Ref: products.merchant_id > merchants.id // many-to-one
|