W
W
weranda2022-01-26 19:15:39
Database
weranda, 2022-01-26 19:15:39

How to correctly distribute data across tables?

Greetings.
I will give a short table of orders.
61f170e41b091825589246.png
This table has a date, a description, and a status. I don't really understand what to do with the statuses:
a) leave them in this table;
b) transfer to a separate table (order_statuses);
c) push into the settings table (settings) in one field in an array of some kind.

There will be several statuses, new ones can be added or old ones removed. The data in the table will be entered from the site. Next to the status field on the site there will be a choice of status from the drop-down list, for example. It turns out that the status names for their selection must be stored somewhere: they can be added directly to HTML, to JS and pulled from there, or placed in the database and pulled from it. So many options, my head is spinning :)

I don’t see any difficulties with placing data in HTML & JS. I see the difficulty for myself with the form in which to place these statuses in the database table. Give, please, a correct example with arguments that it became clear to me.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2022-01-27
@rozhnev

Here is the recommended database structure in such a case:

CREATE TABLE statuses (
  id int primary key auto_increment,
  	title varchar(64)
);

CREATE TABLE orders (
  id int primary key auto_increment,
  	created_at datetime default now(),
  	status_id int references statuses(id)
);

MySQL fiddle online

A
Alexander Nesterov, 2022-01-26
@AlexNest

a) False in terms of normalization - data redundancy.
b) In general - a valid option and, if you need to change options - the best.
c) It also violates the principles of normalization - arrays cannot be stored in one field.
d) Alternatively, use enum. The easiest option, but it will be problematic to change them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question