简体   繁体   English

sql将长字符串保留为字段的良好实践

[英]sql good practice for holding long strings as fields

I am creating a sql database with a table holding questionnaire answers. 我正在使用持有调查表答案的表创建一个sql数据库。 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. 问题是完整的句子(每个句子约150个字符),我想知道什么是将这些信息作为字段进行维护的最佳方法。 I am till new to SQL, but I see two options: 我对SQL还是很陌生,但是我看到两个选择:

  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. 将每个问题设置为数字(1、2、3、4 ...),并有一个单独的表,其中包含实际问题作为链接到第一个表中数字的数据。

  2. some method in CREATE TABLE that lets you set the field as a sentence. CREATE TABLE中的一些方法,可让您将字段设置为句子。 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! 谢谢!

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

Generally using VARCHAR(255) with encoding utf8mb4 is a good default. 通常,将VARCHAR(255)utf8mb4编码utf8mb4是一个很好的默认设置。 If you need long-form data, like essays, multiple paragraphs, etc. then use TEXT or LONGTEXT . 如果您需要长篇数据,例如论文,多段等,请使用TEXTLONGTEXT

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. 如果需要,可以通过添加另一个调查表来获得多个调查表,或者仅按原样使用该数字来划分问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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