More than one value for a column in a database

Is it possible to store more than one value in a database column. if so, what type should i use? and through my java code, how can I insert values.

for example, I want to have a column “language” that can store values ​​like java, C ++, C #, etc. for one line.

EDIT : I want to have a student table with all the student information with a column to hold the names of languages ​​that he knows.

+3
source share
8 answers

You should create a “language” table with all the different languages ​​that you want to use, and then use a foreign key to refer to this table from the LanguageID column in another table.

Edit: If you need more than one language for a given record, you will also need to create a link table that associates the record with that language. You can then put any number of different languages ​​for this entry by creating new entries in the link table.

+9
source

Storing multiple values ​​in one column is usually not a good idea, since it violates the principles of normalizing the database .

, " ". , , .

(, , Telcontar), .

+5

, "" , , , .

ISO, "en-en" ( , ) , , , "en-en de- de" .

"" .

, , . , , , , , "" .

+2

( ), n-m.

(-SQL):

CREATE TABLE user {
    id INTEGER GENERATED PRIMARY KEY,
    name VARCHAR
}

(-SQL):

CREATE TABLE language {
    id INTEGER GENERATED PRIMARY KEY,
    name VARCHAR
}

(-SQL):

CREATE TABLE user_language {
    user_id INTEGER FOREIGN KEY REFERENCES user(id),
    language_id INTEGER FOREIGN KEY REFERENCES language(id)
}

, ( , ). SQL ARRAY, , , ResultSet#getArray() . PostgreSQL, , :

SELECT u.id, u.name, ARRAY(
    SELECT l.name
    FROM language l
    JOIN user_language ul ON u.id = ul.user_id
    WHERE l.id = language_id) AS languages
FROM user u

JDBC :

while (resultSet.next()) {
    Long id = resultSet.getLong("id");
    String name = resultSet.getString("name");
    Object[] languages = resultSet.getArray("languages").getArray();
    // Cast to String[] or convert to List<String> or so yourself.
}
+2

, , ?

-- Works in MySQL for sure
table whatever (
  language enum('JAVA','C++,'C#') NOT NULL DEFAULT 'JAVA'
};

, "--":

table whatever (
  id int
);
table language (
  id int,
  name varchar(256)
)
table whatever_language)(
  whatever_id int,
  language_id int
)
-- don't forget foreign key constraints
+1

, Zack, , , _-Langage ( _ ).

Language Original. ,

+1

, Zack , , VARCHAR ";" , / .

, Oracle, ARRAY, , , .

0

, , - " " ( ) .
, , .
(, ) . , : Person, Language , , , LanguageProficiency, . , (.. ).

The advantage of using the third table here (for some other suggestions, such as combining them in a text box) is that it is easy to expand to include additional attributes, for example, how many years a person has used the language or the rating of the level of language proficiency, or any result test. This is what you would like to know for each combination of person and language, so it belongs to the LanguageProficiency table.

0
source

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


All Articles