外键自动插入值

时间:2016-02-02 05:33:25

标签: php mysql

如何将TBL_PACKAGE中的值自动插入到TBL_MEMBER的外键中?

示例:如果我有两个表:

TBL_MEMBER表

+----+-----------+---------------+-----------------+
| ID | USER_NAME | PACKAGE_ID_FK | UPDATE_TIMES_FK |
+----+-----------+---------------+-----------------+
|            No data available in table            |
+----+-----------+---------------+-----------------+

TBL_PACKAGE

+----+--------------+--------------+
| ID | PACKAGE_NAME | UPDATE_TIMES |
+----+--------------+--------------+
|  1 | FREE         |            5 |
|  2 | PREMIUM      |           10 |
+----+--------------+--------------+

注册USER1USER1将获得FREE个包然后PACKAGE_ID_FK会自动将值插入1UPDATE_TIMES_FK自动插入值为5

TBL_MEMBER 应如下所示:

+----+-----------+---------------+-----------------+
| ID | USER_NAME | PACKAGE_ID_FK | UPDATE_TIMES_FK |
+----+-----------+---------------+-----------------+
|  1 | USER1     |             1 |               5 |
+----+-----------+---------------+-----------------+

当注册用户购买PREMIUM个包时​​,会自动将PACKAGE_ID_FK值设置为2,将UPDATE_TIMES_FK值设置为10

USER1购买PREMIUM套餐后

TBL_MEMBER 应如下所示:

+----+-----------+---------------+-----------------+
| ID | USER_NAME | PACKAGE_ID_FK | UPDATE_TIMES_FK |
+----+-----------+---------------+-----------------+
|  1 | USER1     |             2 |              10 |
+----+-----------+---------------+-----------------+

0 个答案:

没有答案