如何在mysql中使用自动增量创建自定义序列ID [英] how to create custom sequence ids with auto increment in mysql

查看:447
本文介绍了如何在mysql中使用自动增量创建自定义序列ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有project_id和project_name列的'Project'表和另一个带有coloumns Task_id和task_name的'Task'表。







i想要task_id格式的project_id +自动增量





例如:



项目表



project_id project_name

------------ ------------

1 abc

2 xyz

3 tre



任务表



Task_id task_name project_id

------------- -----------------

11 ererer 1

12 asdklasl 1

21 akldjalkdj 2

22 Zdasdad 2

23 ZDmzxm 2

31 dzdldxz 3



for每个新项目的自动增量都必须从1开始。

I have a 'Project' table with columns project_id and project_name and another table 'Task' with coloumns Task_id and task_name.



i want task_id format of project_id + Auto Increment


for example:

project table

project_id project_name
------------------------
1 abc
2 xyz
3 tre

Task Table

Task_id task_name project_id
------------------------------
11 ererer 1
12 asdklasl 1
21 akldjalkdj 2
22 Zdasdad 2
23 ZDmzxm 2
31 dzdldxz 3

for every new project auto increment has to start with 1.

推荐答案

I我认为这是一个坏主意。这种逻辑会给数据库设计和编程带来不必要的复杂性,如何使用数据库。这有什么好处?



例如,如果你有项目1和11,并且他们有任务11和1,那么会发生什么。这两个任务的关键值都是111.如何处理这种情况...



我建议使用纯代理键。查询并从每个表中获取所有相关数据时,您始终可以加入表。
In my opinion this would be a bad idea. This logic wuold introduce unnecessary complexity to the databsae design and to the programming, how the data base is used. What would be the benefit of this?

For example consider what happens if you have projects 1 and 11 and they have tasks 11 and 1 correspodingly. Both these tasks would have a key value of 111. How to handle this situation...

I would suggest using pure surrogate keys. You can always join the tables when querying and fetch all the relevant data from each table.


这篇关于如何在mysql中使用自动增量创建自定义序列ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆