tp6混合easyswoole DLL 多应用模式 修改版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
6.7 KiB

5 years ago
  1. # DDL
  2. 数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言。
  3. 其实就是我们在创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。
  4. ## 测试代码
  5. ```php
  6. use EasySwoole\DDL\Blueprint\Table;
  7. use EasySwoole\DDL\DDLBuilder;
  8. use EasySwoole\DDL\Enum\Character;
  9. use EasySwoole\DDL\Enum\Engine;
  10. $sql = DDLBuilder::table('user', function (Table $table) {
  11. $table->setIfNotExists()->setTableComment('用户表'); //设置表名称/
  12. $table->setTableEngine(Engine::MYISAM); //设置表引擎
  13. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);//设置表字符集
  14. $table->colInt('user_id', 10)->setColumnComment('用户ID')->setIsAutoIncrement()->setIsPrimaryKey(); //创建user_id设置主键并自动增长
  15. $table->colVarChar('username', 30)->setIsNotNull()->setColumnComment('用户名')->setDefaultValue('');
  16. $table->colChar('sex', 1)->setIsNotNull()->setColumnComment('性别:1男,2女')->setDefaultValue(1);
  17. $table->colTinyInt('age')->setIsNotNull()->setColumnComment('年龄')->setIsUnsigned();
  18. $table->colText('intro')->setIsNotNull()->setColumnComment('简介');
  19. $table->colDate('birthday')->setIsNotNull()->setColumnComment('出生日期');
  20. $table->colDecimal('money', 10, 2)->setIsNotNull()->setColumnComment('金额')->setDefaultValue(0);
  21. $table->colInt('created_at', 10)->setIsNotNull()->setColumnComment('创建时间');
  22. $table->colInt('updated_at', 10)->setIsNotNull()->setColumnComment('更新时间');
  23. $table->indexUnique('username_index', 'username')->setIndexComment('用户名唯一索引');//设置索引
  24. $table->indexNormal('username_age_index', ['username', 'age'])->setIndexComment('用户名-年龄');
  25. });
  26. //以下是生成的sql的语句
  27. CREATE TABLE IF NOT EXISTS `user` (
  28. `user_id` int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '用户ID',
  29. `username` varchar(30) NOT NULL DEFAULT '' COMMENT '用户名',
  30. `sex` char(1) NOT NULL DEFAULT 1 COMMENT '性别:1男,2女',
  31. `age` tinyint UNSIGNED NOT NULL COMMENT '年龄',
  32. `intro` text NOT NULL COMMENT '简介',
  33. `birthday` date NOT NULL COMMENT '出生日期',
  34. `money` decimal(10,2) NOT NULL DEFAULT 0 COMMENT '金额',
  35. `created_at` int(10) NOT NULL COMMENT '创建时间',
  36. `updated_at` int(10) NOT NULL COMMENT '更新时间',
  37. UNIQUE INDEX `username_index` (`username`) COMMENT '用户名唯一索引',
  38. INDEX `username_age_index` (`username`,`age`) COMMENT '用户名-年龄'
  39. )
  40. ENGINE = MYISAM DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '用户表';
  41. ------------------------------------------------------------------------------------
  42. $stuQql = DDLBuilder::table('student', function (Table $table) {
  43. $table->setIfNotExists()->setTableComment('学生表'); //设置表名称/
  44. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);//设置表字符集
  45. $table->colInt('stu_id')->setIsAutoIncrement()->setIsPrimaryKey()->setIsUnsigned()->setColumnComment('学生ID'); //创建stu_id设置主键并自动增长
  46. $table->colVarChar('stu_name', 30)->setColumnComment('学生姓名');
  47. $table->colChar('sex', 1)->setColumnComment('性别:1男,2女')->setDefaultValue(1);
  48. $table->colDate('birthday')->setIsNotNull(false)->setColumnComment('出生日期');
  49. $table->colInt('created_at', 10)->setColumnComment('创建时间');
  50. $table->colInt('updated_at', 10)->setColumnComment('更新时间');
  51. $table->indexNormal('stu_name_index', 'stu_name')->setIndexComment('学生姓名--普通索引');//设置索引
  52. });
  53. CREATE TABLE IF NOT EXISTS `student` (
  54. `stu_id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学生ID',
  55. `stu_name` varchar(30) NOT NULL COMMENT '学生姓名',
  56. `sex` char(1) NOT NULL DEFAULT 1 COMMENT '性别:1男,2女',
  57. `birthday` date NULL DEFAULT NULL COMMENT '出生日期',
  58. `created_at` int(10) NOT NULL COMMENT '创建时间',
  59. `updated_at` int(10) NOT NULL COMMENT '更新时间',
  60. INDEX `stu_name_index` (`stu_name`) COMMENT '学生姓名--普通索引'
  61. )
  62. ENGINE = INNODB DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '学生表';
  63. ------------------------------------------------------------------------------------
  64. $courseSql = DDLBuilder::table('course', function (Table $table) {
  65. $table->setIfNotExists()->setTableComment('课程表'); //设置表名称/
  66. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);//设置表字符集
  67. $table->colInt('id', 3)->setIsPrimaryKey()->setIsAutoIncrement()->setIsUnsigned()->setZeroFill()->setColumnComment('课程id');
  68. $table->colVarChar('course_name', 100)->setColumnComment('课程名称');
  69. $table->colChar('status', 1)->setDefaultValue(1)->setColumnComment('课程状态:1正常,0隐藏');
  70. $table->colInt('created_at', 10)->setColumnComment('创建时间');
  71. $table->colInt('updated_at', 10)->setColumnComment('更新时间');
  72. $table->indexUnique('course_name_index', 'course_name')->setIndexComment('课程名称--唯一索引');//设置索引
  73. });
  74. CREATE TABLE IF NOT EXISTS `course` (
  75. `id` int(3) UNSIGNED ZEROFILL NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '课程id',
  76. `course_name` varchar(100) NOT NULL COMMENT '课程名称',
  77. `status` char(1) NOT NULL DEFAULT 1 COMMENT '课程状态:1正常,0隐藏',
  78. `created_at` int(10) NOT NULL COMMENT '创建时间',
  79. `updated_at` int(10) NOT NULL COMMENT '更新时间',
  80. UNIQUE INDEX `course_name_index` (`course_name`) COMMENT '课程名称--唯一索引'
  81. )
  82. ENGINE = INNODB DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '课程表';
  83. ------------------------------------------------------------------------------------
  84. $scoreSql = DDLBuilder::table('score', function (Table $table) {
  85. $table->setIfNotExists()->setTableComment('成绩表'); //设置表名称/
  86. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);//设置表字符集
  87. $table->colInt('id')->setIsUnsigned()->setIsAutoIncrement()->setIsPrimaryKey()->setColumnComment('自增ID');
  88. $table->colInt('stu_id')->setIsUnsigned()->setColumnComment('学生id');
  89. $table->colInt('course_id')->setIsUnsigned()->setZeroFill()->setColumnComment('课程id');
  90. $table->colFloat('score', 3, 1)->setColumnComment('成绩');
  91. $table->colInt('created_at', 10)->setColumnComment('创建时间');
  92. });
  93. CREATE TABLE IF NOT EXISTS `score` (
  94. `id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  95. `stu_id` int UNSIGNED NOT NULL COMMENT '学生id',
  96. `course_id` int UNSIGNED ZEROFILL NOT NULL COMMENT '课程id',
  97. `score` float(3,1) NOT NULL COMMENT '成绩',
  98. `created_at` int(10) NOT NULL COMMENT '创建时间'
  99. )
  100. ENGINE = INNODB DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '成绩表';
  101. ```