### Define a Table within a Specific Schema Source: https://dbml.dbdiagram.io/docs This example shows how to define a table that belongs to a specific schema. If the schema is not explicitly defined, tables will default to the 'public' schema. ```dbml Table core.user { ... } ``` -------------------------------- ### Define Table with Various Column Types and Default Values Source: https://dbml.dbdiagram.io/docs This example showcases defining a table with multiple columns, each having different data types and default values, including string literals, function calls, and numeric values. ```dbml Table users { id integer [primary key] username varchar(255) [not null, unique] full_name varchar(255) [not null] gender varchar(1) [not null] source varchar(255) [default: 'direct'] created_at timestamp [default: `now()`] rating integer [default: 10] } ``` -------------------------------- ### Define Table with One-to-Many Relationship (Many-to-One) Source: https://dbml.dbdiagram.io/docs Define a many-to-one relationship using the '>' symbol, indicating that a column in the current table references a column in another table. The example shows 'posts.user_id' referencing 'users.id'. ```dbml Table posts { id integer [primary key] user_id integer [ref: > users.id] // many-to-one } ``` -------------------------------- ### Define Table with Custom Check Constraint Source: https://dbml.dbdiagram.io/docs Use the 'checks' block to define custom constraints on table columns that cannot be expressed by standard SQL constraints. The example shows a check for positive money (debt + wealth >= 0) with a custom name. ```dbml Table users { id integer wealth integer debt integer checks { `debt + wealth >= 0` [name: 'chk_positive_money'] } } ``` -------------------------------- ### Define Table with One-to-Many Relationship (One-to-Many) Source: https://dbml.dbdiagram.io/docs Define a one-to-many relationship using the '<' symbol, indicating that a column in the current table is referenced by a column in another table. The example shows 'users.id' referencing 'posts.user_id'. ```dbml Table users { id integer [ref: < posts.user_id, ref: < reviews.user_id] // one to many } ``` -------------------------------- ### Define Project with Database Type and Description Source: https://dbml.dbdiagram.io/docs Use this to define the overall project settings, including the database type and a descriptive note. This is useful for setting the context for your database schema. ```dbml Project project_name { database_type: 'PostgreSQL' Note: 'Description of the project' } ``` -------------------------------- ### DBML Records Syntax for Populating a Table Source: https://dbml.dbdiagram.io/docs Populates the 'users' table using DBML's record syntax. It demonstrates how to provide values for each column, including string literals, integers, enum values, nulls, and function expressions. ```dbml records users(id, name, age, status, created_at) { 1, 'Alice', 30, Status.active, '2024-01-15 10:30:00' 2, 'Bob', null, 'inactive', `now()` 3, 'Charlie', , Status.pending, '2024-01-15' } ``` -------------------------------- ### Define Table Columns with Settings Source: https://dbml.dbdiagram.io/docs Illustrates defining columns within a table, including various settings like 'unique', 'not null', 'pk', 'default', and 'note'. Use square brackets for settings. ```dbml Table buildings { ... address varchar(255) [unique, not null, note: 'to include unit number'] id integer [ pk, unique, default: 123, note: 'Number' ] } ``` -------------------------------- ### Define a Table in a Specific Schema with Alias Source: https://dbml.dbdiagram.io/docs This demonstrates how to define a table with an alias for brevity and use it in subsequent references, such as foreign key definitions. ```dbml Table very_long_user_table as U { ... } Ref: U.id < posts.user_id ``` -------------------------------- ### Define Sample Data Records in DBML Source: https://dbml.dbdiagram.io/docs Records allow defining sample data for tables. They can be defined outside a table with an explicit column list, or inside a table with explicit or implicit column lists. Each table can only have one records block. ```dbml Table users { id int [pk] name varchar email varchar } records users(id, name, email) { 1, 'Alice', 'alice@example.com' 2, 'Bob', 'bob@example.com' } ``` ```dbml Table posts { id int [pk] title varchar published boolean records (id, title, published) { 1, 'First Post', true 2, 'Second Post', false } } ``` ```dbml Table comments { id int [pk] user_id int [ref: > users.id] post_id int [ref: > posts.id] title string records { 1, 2, 1, 'First comment of first post by the second user' } } ``` -------------------------------- ### Define and Use TablePartials in DBML Source: https://dbml.dbdiagram.io/docs TablePartials define reusable sets of fields, settings, and indexes. Use the '~' prefix to inject them into table definitions. Local table definitions and the last injected partial take precedence in case of conflicts. ```dbml TablePartial base_template [headerColor: #ff0000] { id int [pk, not null] created_at timestamp [default: `now()`] updated_at timestamp [default: `now()`] } TablePartial soft_delete_template { delete_status boolean [not null] deleted_at timestamp [default: `now()`] } TablePartial email_index { email varchar [unique] indexes { email [unique] } } Table users { ~base_template ~email_index name varchar ~soft_delete_template } ``` ```dbml Table users [headerColor: #ff0000] { id int [pk, not null] created_at timestamp [default: `now()`] updated_at timestamp [default: `now()`] email varchar [unique] name varchar delete_status boolean [not null] deleted_at timestamp [default: `now()`] indexes { email [unique] } } ``` -------------------------------- ### Define Referential Actions (Short Form) Source: https://dbml.dbdiagram.io/docs Specify referential actions for relationships using the short form 'Ref:' syntax. Supported actions include cascade, restrict, set null, set default, and no action for delete and update operations. ```dbml // short form Ref: products.merchant_id > merchants.id [delete: cascade, update: no action] ``` -------------------------------- ### Define Relationship using Short Form Source: https://dbml.dbdiagram.io/docs Define a relationship using the short form 'Ref:' syntax, providing a concise way to link columns between tables. This form also supports optional naming. ```dbml Ref name_optional: schema1.table1.column1 < schema2.table2.column2 ``` -------------------------------- ### DBML Table Definition with Various Data Types Source: https://dbml.dbdiagram.io/docs Defines a 'users' table with columns for ID, name, age, status, and creation timestamp. It showcases different data types including integer, varchar, enum, and timestamp. ```dbml Table users { id int name varchar age int status Status created_at timestamp } ``` -------------------------------- ### Define a Table in the Default Schema Source: https://dbml.dbdiagram.io/docs This syntax defines a table that belongs to the default 'public' schema. You can specify column names, types, and settings within the curly braces. ```dbml // table belonged to default "public" schema Table table_name { column_name column_type [column_settings] } ``` -------------------------------- ### Define One-to-One Relationship with Inline Form Source: https://dbml.dbdiagram.io/docs Define a one-to-one relationship using the inline form, where the 'ref' attribute on a column specifies the relationship. The column with the 'ref' definition is treated as the foreign key. ```dbml Table user_infos { user_id integer [ref: - users.id] } ``` -------------------------------- ### Define Referential Actions (Long Form) Source: https://dbml.dbdiagram.io/docs Define referential actions for relationships using the long form 'Ref' syntax. This allows for specifying delete and update actions like cascade, restrict, set null, set default, or no action. ```dbml // long form Ref { products.merchant_id > merchants.id [delete: cascade, update: no action] } ``` -------------------------------- ### Define Table with Various Index Types Source: https://dbml.dbdiagram.io/docs Define single-column, multi-column, composite, and expression-based indexes within the 'indexes' block of a table definition. Supports primary keys, unique indexes, and custom index names and types (btree, hash). ```dbml Table bookings { id integer country varchar booking_date date created_at timestamp indexes { (id, country) [pk] // composite primary key created_at [name: 'created_at_index', note: 'Date'] booking_date (country, booking_date) [unique] booking_date [type: hash] (`id*2`) (`id*3`,`getdate()`) (`id*3`,id) } } ``` -------------------------------- ### Define Relationship using Inline Form Source: https://dbml.dbdiagram.io/docs Define a relationship directly within the table definition using the 'ref' attribute on a column. This is a compact way to specify foreign key constraints. ```dbml Table schema2.table2 { id integer column2 integer [ref: > schema1.table1.column1] } ``` -------------------------------- ### Define Table with Nullable Foreign Key (Many-to-Zero) Source: https://dbml.dbdiagram.io/docs Define a many-to-zero relationship by combining a foreign key reference with the 'null' constraint. This indicates that the foreign key column can be null, representing an optional relationship. ```dbml Table follows { following_user_id int [ref: > users.id] // many-to-zero followed_user_id int [ref: > users.id, null] // many-to-zero } ``` -------------------------------- ### Define Relationship using Long Form Source: https://dbml.dbdiagram.io/docs Define a relationship using the long form 'Ref' syntax, which allows for optional naming and explicit definition of the relationship between two columns in different schemas or tables. ```dbml Ref name_optional { schema1.table1.column1 < schema2.table2.column2 } ``` -------------------------------- ### Define Cross-Schema Relationship Source: https://dbml.dbdiagram.io/docs Define relationships between tables located in different schemas. This can be done by prefixing table names with their schema names or by using the 'Ref:' syntax with fully qualified table names. ```dbml Table core.users { id integer [pk] } Table blogging.posts { id integer [pk] user_id integer [ref: > core.users.id] } // or this Ref: blogging.posts.user_id > core.users.id ``` -------------------------------- ### Define Table with Non-Nullable Foreign Key (Many-to-One) Source: https://dbml.dbdiagram.io/docs Define a many-to-one relationship with a non-nullable foreign key using the 'not null' constraint. This enforces that the foreign key column must always have a value, ensuring a mandatory relationship. ```dbml Table posts { id int [pk] user_id int [ref: > users.id, not null] // many-to-one } ``` -------------------------------- ### Define Composite Foreign Key Source: https://dbml.dbdiagram.io/docs Define a foreign key constraint that spans multiple columns by listing the columns in parentheses for both the referencing and referenced tables. This is useful for composite primary keys. ```dbml Ref: merchant_periods.(merchant_id, country_code) > merchants.(id, country_code) ``` -------------------------------- ### DBML Enum Definition Source: https://dbml.dbdiagram.io/docs Defines an enumeration type named 'Status' with three possible values: active, inactive, and pending. This is used to constrain a column to a predefined set of string values. ```dbml enum Status { active inactive pending } ``` -------------------------------- ### Define Enum Types in DBML Source: https://dbml.dbdiagram.io/docs Enums allow you to define a set of named values for a column. They can belong to the default 'public' schema or a specific schema. Use double quotes for enum values with special characters. ```dbml enum job_status { created [note: 'Waiting to be processed'] running done failure } enum v2.job_status { ... } Table jobs { id integer status job_status status_v2 v2.job_status } ``` ```dbml enum grade { "A+" "A" "A-" "Not Yet Set" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.