Schnoop Schema is a collection of PHP classes for describing a MySQL database schema.

database, sql, mysql, ddl



Build Status Coverage Status Latest Stable Version Total Downloads License

Schnoop Schema is a collection of PHP classes for describing a MySQL database schema and providing the DDL statements for that schema.

Its intended purpose is to be a code-generator, by making it easy to use PHP to script to creation of DDL statements for tables, routines, etc.

This package does not interact with a database server, but for an implementation that does, checkout milesasylum/Schnoop.

With Schnoop-Scheme you can describe the following database definitions:

  • Databases
  • Tables
    • Columns
    • Indexes
    • Foreign keys
    • Triggers
  • Functions
  • Procedures

Disclaimer: It is not advisable to use this package in production environment -- it's is suitable only for development environments to assist with code generation.


Create database


use MilesAsylum\SchnoopSchema\MySQL\Database\Database;

$database = new Database('schnoop_db');
echo $database->getCreateStatement();
CREATE DATABASE `schnoop_db` DEFAULT COLLATION 'utf8mb4_general_ci'$$

Create table


use MilesAsylum\SchnoopSchema\MySQL\Table\Table;

// Create Table.
$table = new Table('schnoop_tbl');

Add columns to the table


// ...
use MilesAsylum\SchnoopSchema\MySQL\Column\Column;
use MilesAsylum\SchnoopSchema\MySQL\DataType\IntType;
use MilesAsylum\SchnoopSchema\MySQL\DataType\VarCharType;
use MilesAsylum\SchnoopSchema\MySQL\DataType\TimestampType;

// ...

// Add an ID column.
$idType = new IntType();
$idColumn = new Column('id', $idType);

// Add a name column.
$nameType = new VarCharType(50);
$nameColumn = new Column('name', $nameType);

// Add an updated-at column.
$updatedAtType = new TimestampType();
$updatedAtColumn = new Column('updated_at', $updatedAtType);

Add indexes to the table


// ...
use MilesAsylum\SchnoopSchema\MySQL\Constraint\PrimaryKey;
use MilesAsylum\SchnoopSchema\MySQL\Constraint\Index;
use MilesAsylum\SchnoopSchema\MySQL\Constraint\IndexedColumn;

// ...

// Add a primary key
$pkIndex = new PrimaryKey();
    new IndexedColumn($idColumn->getName())

// Add an index on the first 8 characters of the name column.
$nameIndexedColumn = new IndexedColumn($nameColumn->getName());
$nameIndex = new Index('name_idx');


  • Add support for Views.