I am working on a project for a client and going through the initial database design. The project will be a simple web app for tracking processes and their outcomes within a matrix diagram, I am looking for a good way to store these in relational tables.
Right now I am thinking I have a general table for Routines which the x and y coords will map too and maybe off from that a lookup table containing the ID of coordinates in which a "hit" is recorded. Anyone have any better ways of doing this?
Thanks!
EDIT:
This is just the beginning of the project so I have limited detail as of yet, but my main reasoning behind multiple tables is because the matrices will be completely dynamic in size and generic so that each one may be different and they will be tied to a user
I also forgot to mention that order of the x/y values are important, which further supported my reasoning behind having multiple tables for x y and values, from this I strongly assume that needing to know each individual cell is important
EXAMPLE:
The basic example (albeit abstract) of this lies in the process regarding a restaurant. The actions being stuff along the lines of sit down, order food, look over menu, order drinks, eat, pay, etc. the outcomes being order taken, drinks delivered, food delivered, change given. While seemingly simple it becomes complex when taken into consideration things happen differently with each occurrence, also in the case of take out or buffets. the order of the actions and outcomes becomes integral in seeing the differences between the situations
The data tables used in a relational database store information about related objects. Each row holds a record with a unique identifier -- known as a key -- and each column contains the attributes of the data. Each record assigns a value to each feature, making relationships between data points easy to identify.
Whereas an array is merely a data structure who elements are accessed by a numeric value called an index, a matrix is an array with mathematical operations defined on it. A matrix can be one, two, three or more dimensional structures.
When you think that storing files in a relational database is the right answer, stop for a second, write up your list of requirements, and go talk to your systems administrator. Because if you're not doing a relational join with it, it doesn't belong in a relational database.
There are lots of way to do this, we would need a lot more information to be more specific about what would be best for you. However, here are the two SOP ways:
Either a separate table for each matrix:
CREATE TABLE YourMatrixName( RowNo smallint NOT NULL, ColNo smallint NOT NULL, CellValue varchar](50) NULL, CONSTRAINT [PK_Matrices] PRIMARY KEY CLUSTERED ([RowNo] ASC, [ColNo] ASC) ) ON [PRIMARY]; GO CREATE UNIQUE NONCLUSTERED INDEX IX_YourMatrixName ON dbo.YourMatrixName (ColNo, RowNo); GO
Or, all of the matrices in one table:
CREATE TABLE Matrices( MatrixName varchar(24) NOT NULL, RowNo smallint NOT NULL, ColNo smallint NOT NULL, CellValue varchar(50) NULL, CONSTRAINT [PK_Matrices] PRIMARY KEY CLUSTERED ([MatrixName] ASC, [RowNo] ASC, [ColNo] ASC) ) ON [PRIMARY]; GO CREATE UNIQUE NONCLUSTERED INDEX IX_Matrices ON dbo.Matrices (ColNo, RowNo); GO
These are standard normal form, virtually all other ways of doing it are not well normalized. Some advantages of these approaches:
The primary disadvantage is that there is typically a high space to data overhead. Many assume that there is also high overhead to Insert or retrieve new matrices but in fact there are several documented techniques that can make it quite fast.
Video memory, a very simple 2D matrix is stored as follows:
ABCD EFGH IJKL
in ram sequentially like an array as
A,B,C,D,E,F,G,H,I,J,K,L
element x,y can be found at array offset
[y*width+x]
for instance, x=2,y=2 (zero-based) refers to element K.
[y*width+x]=[2*4+2]=10.
array element 10 (again zero-based) = K, so you're good.
Storing in a comma-delimited list will let you put a matrix of any size in an nvarchar field. This assumes that you don't need to query individual cells in SQL, but just grab the matrix as a whole and process it client-side.
Your table may look like this:
tbl_matrices ---- id user_id matrix nvarchar(max)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With