Schema
A formal definition of the structure, constraints, and organization of data, defining what entity types, properties, and relationships are allowed in a system.
Core Concepts
A schema is a blueprint that defines the structure and constraints of data, specifying what types of entities exist, what properties they can have, and how they relate to each other.
Types of Schemas
Database Schema
-- Relational database schema
CREATE TABLE persons (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE,
age INT CHECK (age >= 0)
);
CREATE TABLE companies (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
industry VARCHAR(50)
);
CREATE TABLE employment (
person_id INT REFERENCES persons(id),
company_id INT REFERENCES companies(id),
role VARCHAR(100),
start_date DATE,
PRIMARY KEY (person_id, company_id)
);
Graph Schema
// Neo4j schema constraints
CREATE CONSTRAINT person_email IF NOT EXISTS
FOR (p:Person) REQUIRE p.email IS UNIQUE;
CREATE CONSTRAINT company_name IF NOT EXISTS
FOR (c:Company) REQUIRE c.name IS UNIQUE;
// Indexes for performance
CREATE INDEX person_name IF NOT EXISTS
FOR (p:Person) ON (p.name);
JSON Schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"age": {
"type": "integer",
"minimum": 0
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "email"]
}
In Knowledge Graphs
TrustGraph Schema
// Define schema for Knowledge Graph
await trustgraph.schema.define({
nodeTypes: [
{
label: "Person",
properties: {
name: { type: "string", required: true },
email: { type: "string", format: "email", unique: true },
age: { type: "integer", minimum: 0 }
}
},
{
label: "Company",
properties: {
name: { type: "string", required: true },
industry: { type: "string" },
founded: { type: "date" }
}
}
],
relationshipTypes: [
{
type: "WORKS_AT",
from: ["Person"],
to: ["Company"],
properties: {
since: { type: "date" },
role: { type: "string" }
}
}
]
});
// Validate data against schema
await trustgraph.ingest({
sources: ["data.json"],
validateSchema: true // Enforces schema constraints
});
Benefits
- Data Quality: Ensures data consistency
- Documentation: Self-documenting data structure
- Validation: Catch errors early
- Query Optimization: Database can optimize based on schema
- Evolution: Track schema changes over time
Schema vs Schema-less
| Approach | Pros | Cons |
|---|---|---|
| Schema-first | Data quality, performance, documentation | Less flexible, migration overhead |
| Schema-less | Flexibility, rapid development | No validation, potential inconsistencies |
| Schema-optional | Best of both - validate when needed | Requires discipline |
See Also
Examples
- •Database schema defining tables, columns, and foreign keys
- •Graph schema specifying valid node labels and relationship types
- •JSON Schema validating API request/response formats