Database Design Standards
Scope
This page centralizes relational database conventions for qOntology. Recommendations guide new work and reviews; they do not imply that every existing table already conforms. Current SQL, entities and mappers remain the source of truth for existing structures.
1. Module boundaries and naming
Assign tables to the business module that owns them. Prefer service contracts over directly modifying another module’s internal records. Maintain shared field conventions here rather than copying them into every module. These examples exist in the initialization script:
| Business area | Prefix | Existing table |
|---|---|---|
| Basic management | basic_ | basic_project |
| Ontology modeling | om_ | om_object_type |
| Ontology applications | app_ | app_struct_task |
| AI management | ai_ | ai_model |
| System administration | system_ | system_user |
Use meaningful lowercase identifiers with underscores for new tables and columns. Avoid spaces, hyphens and vague names such as data1 or info2. The system module uses system_ tables; verify prefixes in SQL instead of guessing from page labels.
Database names such as qontology_dev, qontology_test and qontology_prod are environment naming examples, not actual connection settings. Verify the application connection when switching environments.
2. Tables and columns
Define what a record represents, its uniqueness scope and lifecycle before choosing columns. Document relationship direction, event meaning and retention for relationship, log and history tables. Suffixes such as _rel and _log can express purpose; this guide does not rename existing tables.
Document meaning, units, nullability, defaults and status values for new columns. Keep names consistent for the same meaning, and align status values across documentation, code and SQL. Reference IDs should use types compatible with the referenced column.
| Purpose | Recommendation | Review point |
|---|---|---|
| IDs and references | BIGINT | Match referenced ID types |
| Names and codes | VARCHAR(n) | Choose a business-driven limit |
| Exact numeric values | DECIMAL(p,s) | Specify precision and units |
| Time | DATETIME | Define timezone and write ownership |
| Flags | TINYINT / BIT | Follow target-table and mapping conventions |
| Long text | TEXT | Assess size and search requirements |
3. Check the existing schema
The following fields are from sql/mysql/initialization/latest初始化.sql. This is an explanatory extract, not a complete table creation script:
| Table | Column | Current definition |
|---|---|---|
basic_project | id | BIGINT, AUTO_INCREMENT, PRIMARY KEY |
basic_project | name | VARCHAR(128), NULL |
basic_project | namespace | VARCHAR(256), NULL |
basic_project | del_flag | BIT(1), NOT NULL, DEFAULT b'0' |
om_object_type | workspace_id | BIGINT, NOT NULL |
om_object_type | version_id | BIGINT, NULL |
om_object_type | del_flag | TINYINT(1), NOT NULL, DEFAULT 0 |
Conventions are not guarantees
Existing flags use both BIT and TINYINT. A default value on update_time does not guarantee automatic updates; inspect service logic, MyBatis filling and actual SQL. Column names alone do not prove unique indexes, foreign keys, tenant isolation or automatic updates.
4. Column order and audit data
For new tables, group columns as primary key, scope or version references, business fields, state and audit data. For example: id → workspace_id → version_id → name → status → create_time → update_time. This improves readability, not index performance.
Specify whether creator and updater fields store IDs or display names. Design deletion flags together with query filtering, uniqueness and recovery. State whether the application or database writes timestamps.
5. Indexes, constraints and queries
- Derive indexes from real filters, joins and sorting, and verify with
EXPLAINinstead of indexing every column. - Include the correct project, workspace or version scope in uniqueness rules; resolve duplicates before adding a unique constraint.
- Use
idx_anduk_to distinguish ordinary and unique indexes, documenting their purpose in migrations. - Do not blindly add ordinary indexes to large extracted text; establish search requirements and volume first.
- Keep authorization and scope filters in API queries and bind parameters. Indexes do not enforce access control.
6. Changes and review
- Compare
sql/pdman/qOntology.pdma.json, target-version SQL, entities and mappers. - Maintain initialization for new environments and separate upgrade scripts for existing ones; do not rerun initialization over a populated business database.
- Assess nulls, duplicates, soft-deleted records, historical versions and large-table changes; prepare backup and rollback steps.
- Test record counts, important queries and business flows before and after migration, and align frontend/backend field contracts.
- Document the upgrade sequence for the relevant version.
See Source Code Tour for module relationships and source locations.
