AlterTable.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace EasySwoole\DDL\Test;
  3. require_once '../vendor/autoload.php';
  4. use EasySwoole\DDL\Blueprint\Alter\Table as AlterTable;
  5. use EasySwoole\DDL\DDLBuilder;
  6. $alterStuInfoSql = DDLBuilder::alter('student', function (AlterTable $table) {
  7. $table->setRenameTable('student_info')->setTableComment('学生信息表');
  8. $table->dropColumn('age');
  9. $table->dropIndex('ind_age');
  10. $table->modifyColumn()->varchar('stu_name', 50)->setColumnComment('学生姓名');
  11. $table->changeColumn('sex')->tinyint('gender', 1)->setDefaultValue('2')->setColumnComment('性别:1男,2未知');
  12. $table->modifyIndex('ind_stu_name')->normal('ind_stu_name', 'stu_name')->setIndexComment('学生姓名--普通索引');
  13. $table->addColumn()->varchar('phone', 30)->setColumnComment('学生联系方式');
  14. $table->addIndex()->normal('ind_phone', 'phone')->setIndexComment('学生联系方式-普通索引');
  15. });
  16. echo $alterStuInfoSql . PHP_EOL . PHP_EOL;
  17. $alterStuScoreSql = DDLBuilder::alter('score', function (AlterTable $table) {
  18. $table->setRenameTable('student_score')->setTableComment('学生成绩表');
  19. $table->dropForeign('fk_course_id');
  20. $table->addIndex()->normal('ind_score', 'score')->setIndexComment('学生成绩--普通索引');
  21. $table->modifyForeign('fk_stu_id')->foreign('fk_stu_id', 'stu_id', 'student_info', 'stu_id');
  22. });
  23. echo $alterStuScoreSql . PHP_EOL;
  24. //以下是输出sql语句
  25. /*
  26. ALTER TABLE `student` RENAME TO `student_info`;
  27. ALTER TABLE `student_info`
  28. COMMENT = '学生信息表',
  29. DROP `age`,
  30. CHANGE `sex` `gender` tinyint(1) NOT NULL DEFAULT '2' COMMENT '性别:1男,2未知',
  31. MODIFY `stu_id` bigint(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '学生ID',
  32. ADD `phone` varchar(30) NOT NULL COMMENT '学生联系方式',
  33. DROP INDEX `ind_age`,
  34. DROP INDEX `ind_stu_name`,
  35. ADD INDEX `ind_stu_name` (`stu_name`) COMMENT '学生姓名--普通索引',
  36. ADD INDEX `ind_phone` (`phone`) COMMENT '学生联系方式-普通索引';
  37. ALTER TABLE `score` RENAME TO `student_score`;
  38. ALTER TABLE `student_score`
  39. COMMENT = '学生成绩表',
  40. DROP INDEX `ind_score`,
  41. ADD INDEX `ind_score` (`score`) COMMENT '学生成绩--普通索引';
  42. ALTER TABLE `student_score` DROP FOREIGN KEY `fk_course_id`;
  43. ALTER TABLE `student_score` DROP FOREIGN KEY `fk_stu_id`;
  44. ALTER TABLE `student_score` ADD CONSTRAINT `fk_stu_id` FOREIGN KEY (`stu_id`) REFERENCES `student_info` (`stu_id`);
  45. */