dongling4288 2014-02-20 05:56
浏览 62

关于使用分隔符将值存储到MYSQL数据库的方法的建议[关闭]

I have 2 fields in my order table. Product name and product level.

Example lift A, lift A will go level 1 , 2, 3. Lift B will go level 4, 5, 6

I wanted to use delimeter to store the value in product name and product level.

How to store them in such a way it is easier to insert the value in and retrieve the value and display it in a table format.

  • 写回答

1条回答 默认 最新

  • donglu9896 2014-02-20 06:00
    关注

    The short answer: No you don't want to do this. Never store more than one value in a single column.

    Instead, you should store six different rows in the database:

     lift   level
       A       1
       A       2
       A       3
       B       4
       B       5
       B       6
    

    This way it is easy to store and retrieve rows.

    The long answer: Database engines are designed to be able to look up and provide information quickly. One of the key factors in this is indexing.

    Think of a database index like a telephone book. If you want to find a name "Flintstone" for example, first you would skip partway into the "F" section, then look for "Fl", followed by "Fli", etc. A database index does the same.

    Now, following the same example, what if you wanted to find everybody's name who contained the characters "lint". You could not use the index because while there may be a name "Flintstone", there most likely is a "Clinton", and possible a "Splinter". Your only recourse would be to look through every name trying to find your string.

    This latter example is exactly what you'd end up by storing a value of "1,2,3" (or "1;2;3" or even "123") in a single column. If you ever wanted to know which lift is matched to level 2, you could not do this without first extracting the value "1,2,3", then looking inside for a match of 2.

    This only becomes more difficult when joins come into play and you had more information for each level. For example if you have a table "level_names" which stored a name for each level. If you had the "1,2,3" schema, what would you want for a result row when joined to this table? "A", "1,2,3", "First,Second,Third"? Now what if a name contained a comma? You would be setting yourself up for a parsing nightmare and doing a lot more heavy lifting in the application than should be required (database engines, whether they can or can't natively do this, are really not designed for this kind of string/output manipulation)

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题