CreateTable.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace EasySwoole\DDL\Test;
  3. require_once '../vendor/autoload.php';
  4. use EasySwoole\DDL\Blueprint\Create\Table as CreateTable;
  5. use EasySwoole\DDL\DDLBuilder;
  6. use EasySwoole\DDL\Enum\Character;
  7. use EasySwoole\DDL\Enum\Engine;
  8. use EasySwoole\DDL\Enum\Foreign;
  9. $stuSql = DDLBuilder::create('student', function (CreateTable $table) {
  10. $table->setIfNotExists()->setTableComment('学生表'); //设置表名称
  11. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI); //设置表字符集
  12. $table->setTableEngine(Engine::INNODB); //设置表引擎
  13. $table->setTableAutoIncrement(100); //设置表起始自增数
  14. $table->int('stu_id')->setIsAutoIncrement()->setIsPrimaryKey()->setIsUnsigned()->setColumnComment('学生ID'); //创建stu_id设置主键并自动增长
  15. $table->varchar('stu_name', 30)->setColumnComment('学生姓名');
  16. $table->char('sex', 1)->setColumnComment('性别:1男,2女')->setDefaultValue(1);
  17. $table->int('age', 2)->setColumnComment('年龄')->setDefaultValue(0);
  18. $table->date('birthday')->setIsNotNull(false)->setColumnComment('出生日期');
  19. $table->int('created_at', 10)->setColumnComment('创建时间');
  20. $table->int('updated_at', 10)->setColumnComment('更新时间');
  21. $table->normal('ind_stu_name', 'stu_name')->setIndexComment('学生姓名--普通索引');//设置索引
  22. $table->normal('ind_age', 'age')->setIndexComment('学生年龄--普通索引');//设置索引
  23. });
  24. echo $stuSql . PHP_EOL . PHP_EOL;
  25. $courseSql = DDLBuilder::create('course', function (CreateTable $table) {
  26. $table->setIfNotExists()->setTableComment('课程表'); //设置表名称
  27. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI); //设置表字符集
  28. $table->setTableEngine(Engine::INNODB); //设置表引擎
  29. $table->int('id', 3)->setIsPrimaryKey()->setIsAutoIncrement()->setIsUnsigned()->setZeroFill()->setColumnComment('课程id');
  30. $table->varchar('course_name', 100)->setColumnComment('课程名称');
  31. $table->char('status', 1)->setDefaultValue(1)->setColumnComment('课程状态:1正常,0隐藏');
  32. $table->int('created_at', 10)->setColumnComment('创建时间');
  33. $table->int('updated_at', 10)->setColumnComment('更新时间');
  34. $table->unique('uni_course_name', 'course_name')->setIndexComment('课程名称--唯一索引');//设置索引
  35. });
  36. echo $courseSql . PHP_EOL . PHP_EOL;
  37. $scoreSql = DDLBuilder::create('score', function (CreateTable $table) {
  38. $table->setIfNotExists()->setTableComment('成绩表'); //设置表名称
  39. $table->setTableCharset(Character::UTF8MB4_GENERAL_CI); //设置表字符集
  40. $table->setTableEngine(Engine::INNODB); //设置表引擎
  41. $table->int('id')->setIsUnsigned()->setIsAutoIncrement()->setIsPrimaryKey()->setColumnComment('自增ID');
  42. $table->int('stu_id')->setIsUnsigned()->setColumnComment('学生id');
  43. $table->int('course_id')->setIsUnsigned()->setZeroFill()->setColumnComment('课程id');
  44. $table->float('score', 3, 1)->setColumnComment('成绩');
  45. $table->int('created_at', 10)->setColumnComment('创建时间');
  46. $table->foreign('fk_stu_id', 'stu_id', 'student', 'stu_id')
  47. ->setOnDelete(Foreign::CASCADE)->setOnUpdate(Foreign::CASCADE);
  48. $table->foreign('fk_course_id', 'course_id', 'course', 'id')
  49. ->setOnDelete(Foreign::CASCADE)->setOnUpdate(Foreign::CASCADE);
  50. });
  51. echo $scoreSql;
  52. //以下是输出sql语句
  53. /*
  54. CREATE TABLE IF NOT EXISTS `student` (
  55. `stu_id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学生ID',
  56. `stu_name` varchar(30) NOT NULL COMMENT '学生姓名',
  57. `sex` char(1) NOT NULL DEFAULT 1 COMMENT '性别:1男,2女',
  58. `age` int(2) NOT NULL DEFAULT 0 COMMENT '年龄',
  59. `birthday` date NULL DEFAULT NULL COMMENT '出生日期',
  60. `created_at` int(10) NOT NULL COMMENT '创建时间',
  61. `updated_at` int(10) NOT NULL COMMENT '更新时间',
  62. INDEX `ind_stu_name` (`stu_name`) COMMENT '学生姓名--普通索引',
  63. INDEX `ind_age` (`age`) COMMENT '学生年龄--普通索引'
  64. )
  65. ENGINE = INNODB AUTO_INCREMENT = 100 DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '学生表';
  66. CREATE TABLE IF NOT EXISTS `course` (
  67. `id` int(3) UNSIGNED ZEROFILL NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '课程id',
  68. `course_name` varchar(100) NOT NULL COMMENT '课程名称',
  69. `status` char(1) NOT NULL DEFAULT 1 COMMENT '课程状态:1正常,0隐藏',
  70. `created_at` int(10) NOT NULL COMMENT '创建时间',
  71. `updated_at` int(10) NOT NULL COMMENT '更新时间',
  72. UNIQUE INDEX `uni_course_name` (`course_name`) COMMENT '课程名称--唯一索引'
  73. )
  74. ENGINE = INNODB DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '课程表';
  75. CREATE TABLE IF NOT EXISTS `score` (
  76. `id` int UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
  77. `stu_id` int UNSIGNED NOT NULL COMMENT '学生id',
  78. `course_id` int UNSIGNED ZEROFILL NOT NULL COMMENT '课程id',
  79. `score` float(3,1) NOT NULL COMMENT '成绩',
  80. `created_at` int(10) NOT NULL COMMENT '创建时间',
  81. CONSTRAINT `fk_stu_id` FOREIGN KEY (`stu_id`) REFERENCES `student` (`stu_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  82. CONSTRAINT `fk_course_id` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
  83. )
  84. ENGINE = INNODB DEFAULT COLLATE = 'utf8mb4_general_ci' COMMENT = '成绩表';
  85. */