Dane Kania
Dane Kania

Reputation: 45

sql good practice for holding long strings as fields

I am creating a sql database with a table holding questionnaire answers. The questions are full sentences (about 150 characters each) and I want to know what is the best method for maintaining that information as the fields. I am till new to SQL, but I see two options:

  1. set each question as a number (1, 2, 3, 4...) and have a separate table holding the actual questions as the data that links to the number in the first table.

  2. some method in CREATE TABLE that lets you set the field as a sentence. I though quotes would work, but they do not.

EDIT:

a quick example of what i am trying to do:

CREATE TABLE survey(
    index_id INT PRIMARY KEY,
    'between 1 and 10, how do you feel about the transparency of the scientific community?' VARCHAR(5)
);

Thanks!

Upvotes: 0

Views: 1148

Answers (2)

Hogan
Hogan

Reputation: 70523

You are mixing up the data in a table and creating the table.

When you create the table you define the structure of the table

Then you can add data to the table

Then you can query the table.

So for example create a table.

create table questionanswer (
  questionnumber integer,
  answer varchar(200)
)

add data to the table

insert into questionanswer (questionnumber, answer)
   values (1, 'election day')

query the table for values

select answer
from questionanswer
where questionnumber = 1

Upvotes: 1

tadman
tadman

Reputation: 211580

Generally using VARCHAR(255) with encoding utf8mb4 is a good default. If you need long-form data, like essays, multiple paragraphs, etc. then use TEXT or LONGTEXT.

This is really a one-table problem:

CREATE TABLE questions (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  questionnaire_id INT NOT NULL,
  num INT NOT NULL DEFAULT 0,
  question VARCHAR(255) NOT NULL
);

Where if you want you can have multiple questionnaires by adding another questionnaire table, or just use that number as-is for partitioning the questions.

Upvotes: 0

Related Questions