开发者

Best MySQL datatype

I am trying to figure a good way to store these a series of points in a database.

The couple ways I thought would be good is first a text type and then use a delimiter to sperate the points so it would look开发者_JAVA技巧 like this in the db

+----------------------------+
| x_points    | 1:2:3:4:5... |
| y_points    | 1:2:3:4:5... |
+----------------------------+

Then the web app would pull the points and plot them on a canvas.

Is there a better way to store points to a line in a db?

For complex functions the points could be 1000 points per graph and then a delimiter for each point so a boat load of characters.


Because of the comments I will try to elaborate more. I am using a canvas to plot out functions that a user inputs. The user will also be able to draw on the graph and I would like to store the line data of the drawing as well both I figured could be stored the same way and the computation of the points only needs to happen once.

A sample seniro would be a user can plot y=x^2 and then circle the y-intercept. Then they could link to that canvas and it would redraw their circle of the y-intercept and the graph. Of course this is simplistic example but I cannot figure out how to store the points on the canvas best.

Hope this helps more.


This is called a one-to-many relationship. You have one shape and many actual points. You would usually want to put the points into a separate table, like this.

+----------------------+      +-----------------+
|        Shape         |      |  Point_Values   |
+----------------------+      +-----------------+
| shape_id (INT)       |      | shape_id (INT)  |
| shape_name (VARCHAR) |      | point_x (INT)   |
+----------------------+      | point_y (INT)   |
                              +-----------------+

To make a new shape, insert a new shape into the Shape table. Then create one or more values in the Point_Values table. When you want to go back and get the values, you would use a join, like this:

SELECT s.shape_name, v.point_x, v.point_y
FROM Shape s
JOIN Point_Values v ON s.shape_id = v.shape_id
WHERE s.shape_id = 5

This has the advantage of being very flexible. Each shape can have 0 or more points, and there is an implicit enforcement that there must be an equal number of x and y points.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜