PHPackages                             chevere/sql-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. chevere/sql-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

chevere/sql-parser
==================

MySQL schema parser

0.5.1(10mo ago)12841MITPHPPHP ^8.1CI failing

Since Sep 28Pushed 9mo agoCompare

[ Source](https://github.com/chevere/sql-parser)[ Packagist](https://packagist.org/packages/chevere/sql-parser)[ RSS](/packages/chevere-sql-parser/feed)WikiDiscussions 0.5 Synced today

READMEChangelog (2)Dependencies (3)Versions (4)Used By (1)

SQLParser - Parse MySQL schemas in PHP, fast
============================================

[](#sqlparser---parse-mysql-schemas-in-php-fast)

[![Build](https://camo.githubusercontent.com/213883db17f9a3444c8242221b2c70e7d6eef41e46ad1e76c2a3eb9391536a9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636865766572652f73716c2d7061727365722f746573742e796d6c3f6272616e63683d302e35267374796c65)](https://github.com/chevere/sql-parser/actions)[![Code size](https://camo.githubusercontent.com/354caf5a184cd82a3a439934f72c7a78c3e3259d6cd8c60a9b1f7f6116e5dbd2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f73716c2d7061727365723f7374796c65)](https://camo.githubusercontent.com/354caf5a184cd82a3a439934f72c7a78c3e3259d6cd8c60a9b1f7f6116e5dbd2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c616e6775616765732f636f64652d73697a652f636865766572652f73716c2d7061727365723f7374796c65)[![Apache-2.0](https://camo.githubusercontent.com/0936fa14ed8d09fb0675935182cf932ff74656e22231c486c842c8234d41abe7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f636865766572652f73716c2d7061727365723f7374796c65)](LICENSE)[![PHPStan](https://camo.githubusercontent.com/21e27b5fe1595d5b77491a594f06d4e4811247579aa1ad0b39b31f978108ba62/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230332d626c756576696f6c65743f7374796c65)](https://phpstan.org/)[![Mutation testing badge](https://camo.githubusercontent.com/4b772af29f1286c36d0e25e22427f1244c19c0bc80b9aa14890e09f0b55b9f1b/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532466368657665726525324673716c2d706172736572253246302e35)](https://dashboard.stryker-mutator.io/reports/github.com/chevere/sql-parser/0.5)

[![Quality Gate Status](https://camo.githubusercontent.com/426171ccf4d04120e54640a0bcf134391cdfbb0c87a3538ce9de95e6c57926b1/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![Maintainability Rating](https://camo.githubusercontent.com/7b8e1fd3702545656c8e41e246adea1719ec41a4414c89792133defd10950b0d/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![Reliability Rating](https://camo.githubusercontent.com/80c91d4e4ba872b038a2c7100841be47d9899e706c37d1df73394e3bcefac4af/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![Security Rating](https://camo.githubusercontent.com/be3bada24ab59fa28ac114c1c9e87fbd93de64dc94c1f59bced00e102a8fe859/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![Coverage](https://camo.githubusercontent.com/2318684422bec3c6dc0db3d0096c4bdad1d882d9d5b0033e73a78ed9b04efb05/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![Technical Debt](https://camo.githubusercontent.com/331ce7c9c9424de1595c4ff508285222c9be9dc65aa0bcf1b066ebf7d0c93136/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d636865766572655f73716c2d706172736572266d65747269633d7371616c655f696e646578)](https://sonarcloud.io/dashboard?id=chevere_sql-parser)[![CodeFactor](https://camo.githubusercontent.com/7fa9871a68d1ea21b49fa541d7f4f1c27f5742241ad0fcea7aad59874e063373/68747470733a2f2f7777772e636f6465666163746f722e696f2f7265706f7369746f72792f6769746875622f636865766572652f73716c2d7061727365722f6261646765)](https://www.codefactor.io/repository/github/chevere/sql-parser)

This library takes MySQL `CREATE TABLE` statements and returns a data structure representing the table that it defines. MySQL syntax [version 5.7](https://dev.mysql.com/doc/refman/5.7/en/create-table.html) is supported. This library does not try to validate input - the goal is to deconstruct valid `CREATE TABLE` statements.

Installation
------------

[](#installation)

You can install this package using composer. To add it to your `composer.json`:

```
composer require chevere/sql-parser

```

You can then load it using the composer autoloader:

```
require_once 'vendor/autoload.php';
use iamcal\SQLParser;

$parser = new SQLParser();
```

If you don't use composer, you can skip the autoloader and include `src/SQLParser.php` directly.

Usage
-----

[](#usage)

To extract the tables defined in SQL:

```
$parser = new SQLParser();
$parser->parse($sql);

print_r($parser->tables);
```

The `tables` property is an array of tables, each of which is a nested array structure defining the table's structure:

```
CREATE TABLE `achievements_counts` (
  `achievement_id` int(10) unsigned NOT NULL,
  `num_players` int(10) unsigned NOT NULL,
  `date_updated` int(10) unsigned NOT NULL,
  PRIMARY KEY (`achievement_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

```
[
    'achievements_counts' => [
        'name' => 'achievements_counts',
        'fields' => [
            [
                'name' => 'achievement_id',
                'type' => 'INT',
                'length' => '10',
                'unsigned' => true,
                'null' => false,
            ],
            [
                'name' => 'num_players',
                'type' => 'INT',
                'length' => '10',
                'unsigned' => true,
                'null' => false,
            ],
            [
                'name' => 'date_updated',
                'type' => 'INT',
                'length' => '10',
                'unsigned' => true,
                'null' => false,
            ],
        ],
        'indexes' => [
            [
                'type' => 'PRIMARY',
                'cols' => [
                    [
                        'name' => 'achievement_id',
                    ],
                ],
            ],
        ],
        'props' => [
            'ENGINE' => 'InnoDB',
            'CHARSET' => 'utf8',
        ],
    ],
]
```

You can also use the lexer directly to work with other piece of SQL:

```
$parser = new SQLParser();
$parser->lex($sql);

print($parser->tokens);
```

The `tokens` property contains an array of tokens. SQL keywords are returned as uppercase, with multi-word terms (e.g. `DEFAULT CHARACTER SET`) as a single token. Strings and escaped identifiers are not further processed; they are returned exactly as expressed in the input SQL.

By default, the tokenizer will ignore unterminated comments and strings, and stop parsing at that point, producing no further tokens. You can set `$parser->throw_on_bad_syntax = true;` to throw an exception of type `iamcal\SQLParserSyntaxException` instead.

Performance
-----------

[](#performance)

My test target is an 88K SQL file containing 114 tables from Glitch's main database.

The first version, using [php-sql-parser](http://code.google.com/p/php-sql-parser/), took over 60 seconds just to lex the input. This was obviously not a great option.

The current implementation uses a hand-written lexer which takes around 140ms to lex the same input and imposes less odd restrictions. This seems to be the way to go.

History
-------

[](#history)

This library was created to parse multiple `CREATE TABLE` schemas and compare them, so figure out what needs to be done to migrate one to the other.

This is based on the system used at b3ta, Flickr and then Tiny Speck to check the differences between production and development databases and between shard instances. The original system just showed a diff (see [SchemaDiff](https://github.com/iamcal/SchemaDiff)), but that was a bit of a pain.

Unsupported features
--------------------

[](#unsupported-features)

MySQL table definitions have a *lot* of options, so some things just aren't supported. They include:

- `UNION` table properties
- `TABLESPACE` table properties
- table partitions
- `FLOAT[(bits)]` fields
- Deprecated `YEAR(2|4)` fields
- `ASCII` attribute as a shorthand for `CHARACTER SET latin1`
- `UNICODE` attribute as a shorthand for `CHARACTER SET ucs2`
- `NATIONAL` modified for `CHAR` and `VARCHAR` fields

If you need support for one of these features, open an issue or (better) send a pull request with tests.

The specs for each of the four field groupings can be found here:

-
-
-
-

Alternatives
------------

[](#alternatives)

If you're using PHP, then [Modyllic](https://github.com/onlinebuddies/modyllic) is a great SQL parser and set of schema management tools.

If you're using Hack, then [Hack SQL Fake](https://github.com/slackhq/hack-sql-fake) allows you to parse SQL and create a fake MySQL server for testing, with many (but not all!) features of MySQL.

Publishing
----------

[](#publishing)

To publish a new version:

- Commit changes to master
- Add git tag
- Go to  and hit "Update"

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance55

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 85.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~306 days

Total

3

Last Release

292d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a87fbbcb1518fe64878bc53dedd9f61b81243cbb21b73c26f5e299d501375984?d=identicon)[rodolfo](/maintainers/rodolfo)

---

Top Contributors

[![iamcal](https://avatars.githubusercontent.com/u/173750?v=4)](https://github.com/iamcal "iamcal (109 commits)")[![rodber](https://avatars.githubusercontent.com/u/20590102?v=4)](https://github.com/rodber "rodber (13 commits)")[![demmer](https://avatars.githubusercontent.com/u/3308504?v=4)](https://github.com/demmer "demmer (2 commits)")[![WilliamFalci](https://avatars.githubusercontent.com/u/36926081?v=4)](https://github.com/WilliamFalci "WilliamFalci (1 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (1 commits)")[![DirtyRacer1337](https://avatars.githubusercontent.com/u/17379553?v=4)](https://github.com/DirtyRacer1337 "DirtyRacer1337 (1 commits)")[![richardudovich](https://avatars.githubusercontent.com/u/828730?v=4)](https://github.com/richardudovich "richardudovich (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/chevere-sql-parser/health.svg)

```
[![Health](https://phpackages.com/badges/chevere-sql-parser/health.svg)](https://phpackages.com/packages/chevere-sql-parser)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
