简体   繁体   中英

How to design a relational database with dynamic columns?

Let's say that I have a courses table with the following structure.

     courses (Primary keys: (id) )
+-------+--------------------+
|  id   | name               |
+-------+--------------------+
| 1     | Chinese - English  |
| 2     | Japanese - English |
| 3     | Chinese - Spanish  |
+-------+--------------------+

Courses will have columns like this.

   course_columns (Primary keys: (id), Indexes: (course_id) )
+-------+-----------+----------+
|  id   | course_id | title    |
+-------+-----------+----------+
| 1     | 1         | Chinese  |
| 2     | 1         | English  |
| 3     | 1         | Pinyin   |
| 4     | 2         | Japanese |
| 5     | 2         | English  |
| 6     | 2         | Kana     |
| 7     | 2         | Romaji   |   
| 8     | 3         | Chinese  |
| 9     | 3         | Spanish  |
+-------+-----------+----------+

Lastly, courses will have words like this

    course_word_data (Primary Keys: (id, column_id) )
+------+-----------+-----------+
|  id  | column_id | content   |
+------+-----------+-----------+
| 1    | 1         | 你好      |
| 1    | 2         | hello     |
| 1    | 3         | nĭ hăo    |
| 2    | 1         | 谢谢      |
| 2    | 2         | goodbye   |
| 2    | 3         | xièxie    |
| 3    | 4         | 好む      |
| 3    | 5         | to prefer |
| 3    | 6         | このむ    |
| 3    | 7         | konomu    |
| 4    | 8         | 你好      |
| 4    | 9         | hola      |
+------+-----------+-----------+

Now let me say that I am pretty much a newbie when it comes to database and for the last few days I've been thinking about how to design this better. The use-case scenario is like the following:

  1. User creates a course
  2. User adds arbitrary number of columns with titles to the course
  3. User can modify or delete existing columns
  4. User can add, modify or delete words to the course

Also, if a word in a course is edited by the course owner, it should be course-specific and should not affect other courses.

Here are my questions:

  1. Is this a good database design? My problem is that if there are ten columns in a course, then that means there are ten rows in course_word_data table for a single word. Basically for m columns and n words, there are m*n rows for a single course. Let's say that there are 5000 courses and the average number of columns per course is 6. If every course has an average of 1000 words, this means there are 5000*6*1000 = 30 million rows. Is it okay to do the design this way, or should I reconsider my design? What would be a better way to do it?

  2. Is it possible to reduce the number of repeating words? As you can see, the word 「你好」exist in the table two times. Is there an efficient way to reduce it?

An example of a course (think of this as a grid element on a webpage)

    +------------+--------------+--------------+
    | English    | Pinyin       | Chinese      |
    +------------+--------------+--------------+
    | hello      | nĭ hăo       | 你好         |
    | goodbye    | xièxie       | 谢谢         |
    +------------+--------------+--------------+
    (Columns can be added or removed by the user. 
     For example part of speech column which indicates 
     whether a column is noun, ver, adjective etc. )

I apologize for the length of the question in advance.

Invoke Separation of Concerns principle here; It is not the same an HTML table (a View) than a database table (Persistence).

"User add columns" in a View context would be equivalent to "User SELECTs columns" in a query context. Should a user create languages? Maybe not, languages are there already.

Side note: In translation problems, it is useful a word context .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM