为什么不用Diagrams,后面笔者发现DBML的局限性有点大,如果时个人数据库设计不如直接画图,如果是团队使用学习成本又不小,很纠结
DBML转可视化网站:
https://dbdocs.io/?utm_source=dbdiagram&utm_medium=promo_button
DBML语法说明文档:
https://www.dbml.org/home/#dbml-history
https://www.dbml.org/docs/#table-definition
备注:
1.字段后可以加的括号内内容:
- note: 'string to add notes': add a metadata note to this column
- primary keyor- pk: mark a column as primary key. For composite primary key, refer to the ‘Indexes’ section
- nullor- not null: mark a column null or not null
- unique: mark the column unique
- default: some_value: set a default value of the column, please refer to the ‘Default Value’ section below
- increment: mark the column as auto-increment- 2.可以设置的默认值
- number value starts blank: - default: 123or- default: 123.456
- string value starts with single quotes: default: 'some string value'
- expression value is wrapped with parenthesis: default:now() - interval ‘5 days’``
- boolean (true/false/null): - default: falseor- default: null- 3.索引的使用
- Index with single field (with index name): - CREATE INDEX on users (created_at)
- Index with multiple fields (composite index): CREATE INDEX on users (created_at, country)
- Index with an expression: CREATE INDEX ON films ( first_name + last_name )
- (bonus) Composite index with expression: CREATE INDEX ON users ( country, (lower(name)) )
Table bookings {
id integer
country varchar
booking_date date
created_at timestamp
indexes {
(id, country) [pk] // composite primary key
created_at [note: 'Date']
booking_date
(country, booking_date) [unique]
booking_date [type: hash]
(`id*2`)
(`id*3`,`getdate()`)
(`id*3`,id)
}
}
4.外键的使用
三种类型:
- <: one-to-many. E.g:- users.id < posts.user_id
- >: many-to-one. E.g:- posts.user_id > users.id
- -: one-to-one. E.g:- users.id - user_infos.user_id
三种语法:
//Long form
Ref name_optional {
table1.column1 < table2.column2
}
//Short form:
Ref name_optional: table1.column1 < table2.column2
// Inline form
Table posts {
id integer
user_id integer [ref: > users.id]
}
//设置级联关系
Ref: products.merchant_id > merchants.id [delete: cascade, update: no action]
级联关系:
- delete / update: cascade | restrict | set null | set default | no action
 Define referential actions. Similar to- ON DELETE/UPDATE CASCADE/...in SQL.
多对多关系:
在DBML中视为两个多对一关系
5.备注
项目备注:
Project DBML {
Note: '''
# DBML - Database Markup Language
DBML (database markup language) is a simple, readable DSL language designed to define database structures.
## Benefits
* It is simple, flexible and highly human-readable
* It is database agnostic, focusing on the essential database structure definition without worrying about the detailed syntaxes of each database
* Comes with a free, simple database visualiser at [dbdiagram.io](http://dbdiagram.io)
'''
}
表格备注:
Table users {
id int [pk]
name varchar
Note: 'Stores user data'
// or
Note {
'This is a note of this table'
}
}
数据列备注
column_name column_type [note: 'replace text here']
6.枚举
用于定义表明状态,种类等
enum job_status {
created [note: 'Waiting to be processed']
running
done
failure
}
Table jobs {
id integer
status job_status
}
7.别名
Table very_long_user_table as U {
...
}
Ref: U.id < posts.user_id
DBML个人使用Vscode快捷语法:
在文件 > 首选项 > 用户代码片段(File->Preferences->User Snippets)选择你要修改哪种文件的模板,然后修改JSON文件即可
 
                         
                                

