title: DDL meta:

  • name: description content: The Database Definition Language (DDL) is a language used to describe real-world entities to be stored in a database. Easyswoole provides a DDL library that is easy for users to define a database table structure.
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|DDL|database statement generation|sql DDL

DDL

The Database Definition Language (DDL) is a language used to describe real-world entities to be stored in a database. Easyswoole provides a DDL library that is easy for users to define a database table structure.

Installation

  1. composer require easyswoole/ddl

Test code

  1. use EasySwoole\DDL\Blueprint\Table;
  2. use EasySwoole\DDL\DDLBuilder;
  3. use EasySwoole\DDL\Enum\Character;
  4. use EasySwoole\DDL\Enum\Engine;
  5. $sql = DDLBuilder::table('user', function (Table $table) {
  6. $table->setTableComment('User table')//Set the table name/
  7. ->setTableEngine(Engine::MYISAM)//Setting the table engine
  8. ->setTableCharset(Character::UTF8MB4_GENERAL_CI);//Set table character set
  9. $table->colInt('user_id', 10)->setColumnComment('User ID')->setIsAutoIncrement()->setIsPrimaryKey();
  10. $table->colVarChar('username')->setColumnLimit(30)->setIsNotNull()->setColumnComment('用户名');
  11. $table->colChar('sex', 1)->setIsNotNull()->setDefaultValue(1)->setColumnComment('Gender: 1 male, 2 female');
  12. $table->colTinyInt('age')->setIsUnsigned()->setColumnComment('age')->setIsNotNull();
  13. $table->colInt('created_at', 10)->setIsNotNull()->setColumnComment('Creation time');
  14. $table->colInt('updated_at', 10)->setIsNotNull()->setColumnComment('Update time');
  15. $table->indexUnique('username_index', 'username');//Setting index
  16. });
  17. echo $sql;
  18. //Results are as follows
  19. CREATE TABLE `user` (
  20. `user_id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID',
  21. `username` varchar(30) NOT NULL COMMENT 'username',
  22. `sex` char(1) NOT NULL COMMENT 'Gender: 1 male, 2 female',
  23. `age` tinyint UNSIGNED NOT NULL COMMENT 'age',
  24. `created_at` int(10) NOT NULL COMMENT 'Creation time',
  25. `updated_at` int(10) NOT NULL COMMENT 'Update time',
  26. UNIQUE INDEX `username_index` (`username`)
  27. )
  28. ENGINE = MYISAM DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = 'user table';