TrustGraphGet Started

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

  1. Data Quality: Ensures data consistency
  2. Documentation: Self-documenting data structure
  3. Validation: Catch errors early
  4. Query Optimization: Database can optimize based on schema
  5. Evolution: Track schema changes over time

Schema vs Schema-less

ApproachProsCons
Schema-firstData quality, performance, documentationLess flexible, migration overhead
Schema-lessFlexibility, rapid developmentNo validation, potential inconsistencies
Schema-optionalBest of both - validate when neededRequires 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

Related Terms

Learn More