为什么不用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 columnprimary keyorpk: mark a column as primary key. For composite primary key, refer to the ‘Indexes’ sectionnullornot null: mark a column null or not nullunique: mark the column uniquedefault: some_value: set a default value of the column, please refer to the ‘Default Value’ section belowincrement: mark the column as auto-increment2.可以设置的默认值
number value starts blank:
default: 123ordefault: 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: falseordefault: null3.索引的使用
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 integercountry varcharbooking_date datecreated_at timestampindexes {(id, country) [pk] // composite primary keycreated_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 formRef name_optional {table1.column1 < table2.column2}//Short form:Ref name_optional: table1.column1 < table2.column2// Inline formTable posts {id integeruser_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 toON DELETE/UPDATE CASCADE/...in SQL.
多对多关系:
在DBML中视为两个多对一关系
5.备注
项目备注:
Project DBML {Note: '''# DBML - Database Markup LanguageDBML (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 varcharNote: 'Stores user data'// orNote {'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']runningdonefailure}Table jobs {id integerstatus job_status}
7.别名
Table very_long_user_table as U {...}Ref: U.id < posts.user_id
DBML个人使用Vscode快捷语法:
在文件 > 首选项 > 用户代码片段(File->Preferences->User Snippets)选择你要修改哪种文件的模板,然后修改JSON文件即可
