SQL database - when to use a separate table and column of an existing table?

So, I'm pretty new to SQL and databases in general (only for the very simple for a minimal site), and I'm trying to develop a better way to develop some models for a site with a lot of databases. So take, for example, a custom gallery. I have a gallery table with reasonable columns, for example, date, name, etc., And the galleries can belong to one category, of which there will not be such (no more than 6). Should I have a category as a gallery table column? Or have a separate table for categories and have a one-to-one relationship between categories and gallery tables? I would like to do something in my views, for example, sort all galleries into categories by date, loaded, is there any difference in performance / convenience between them? The presence of a category in the column of the Gallery table certainly seems easier to me than to me,but I'm not sure what the best practice is. Thank.

+4
source share
3 answers

First of all, you need to understand the conceptual difference.

Typically, you can safely consider the following equivalence:

Table ~~~ Entity

Column ~~~ Attribute

So, when you need to add a new piece of data in relation to the entity (existing table), the question you can ask yourself is:

Is this piece of data an attribute facility?

If yes, then you need a new column.

For example, let's say that you have a table that describes the Student object:

Table Student:

[PK] Id
[FK] IdClass
Name
Surname

Suppose you want to add a GPA for each student. This is obviously a student attribute, so you can add a GPA column to the Student table.

, , . , .

, - , .

, Department.Id FK Students.

, . .

+2

.

, .

+1

, , .

+1

Source: https://habr.com/ru/post/1533209/


All Articles