Example-dbdiagram.sql 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // -- https://dbdiagram.io/home
  2. //// -- LEVEL 1
  3. //// -- Tables and References
  4. // Creating tables
  5. Table users as U {
  6. id int
  7. full_name varchar
  8. created_at timestamp
  9. country_code int
  10. }
  11. Table merchants {
  12. id int
  13. merchant_name varchar
  14. country_code int
  15. "created at" varchar
  16. admin_id int [ref: > U.id] // inline relationship (many-to-one)
  17. }
  18. Table countries {
  19. code int
  20. name varchar
  21. continent_name varchar
  22. }
  23. // Creating references
  24. // You can also define relaionship separately
  25. // > many-to-one; < one-to-many; - one-to-one
  26. Ref: U.country_code > countries.code
  27. Ref: merchants.country_code > countries.code
  28. //----------------------------------------------//
  29. //// -- LEVEL 2
  30. //// -- Adding column settings
  31. Table order_items {
  32. order_id int [increment, ref: > orders.id] // auto-increment
  33. product_id int
  34. quantity int [default: 1] // default value
  35. }
  36. Ref: order_items.product_id > products.id
  37. Table orders {
  38. id int [pk] // primary key
  39. user_id int [not null, unique]
  40. status varchar
  41. created_at varchar [note: 'When order created'] // add column note
  42. }
  43. //----------------------------------------------//
  44. //// -- Level 3
  45. //// -- Enum, Indexes
  46. // Enum for 'products' table below
  47. Enum products_status {
  48. out_of_stock
  49. in_stock
  50. running_low [note: 'less than 20'] // add column note
  51. }
  52. // Indexes: You can define a single or multi-column index
  53. Table products {
  54. id int [pk]
  55. name varchar
  56. merchant_id int [not null]
  57. price int
  58. status products_status
  59. created_at datetime [default: `now()`]
  60. Indexes {
  61. (merchant_id, status) [name:'product_status']
  62. id [unique]
  63. }
  64. }
  65. Ref: products.merchant_id > merchants.id // many-to-one