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

时间:2018-11-06 22:28:32

标签: mysql sql

我正在使用持有调查表答案的表创建一个sql数据库。问题是完整的句子(每个句子约150个字符),我想知道什么是将这些信息作为字段进行维护的最佳方法。我对SQL还是很陌生,但是我看到两个选择:

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

  2. CREATE TABLE中的一些方法,可用于将字段设置为句子。我虽然引号会起作用,但它们不会起作用。

编辑:

一个我想做的简单例子:

from PIL import Image
import os

image_name = "mypic"
count = 0

for filename in os.listdir('pictures'):
    if filename.endswith('.jpg'):
        image_file = open('pictures/' +filename)
        image = Image.open(image_file)

        # next 3 lines strip exif
        data = list(image.getdata())
        image_without_exif = Image.new(image.mode, image.size)
        image_without_exif.putdata(data)

        image_without_exif.save('new-pictures/' + image_name + str(count) + '.jpg')
        count = count + 1;

谢谢!

2 个答案:

答案 0 :(得分:1)

您正在混合表中的数据并创建表。

创建表时,您定义表的结构

然后您可以将数据添加到表中

然后您可以查询表。

例如,创建一个表。

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

将数据添加到表

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

查询表中的值

select answer
from questionanswer
where questionnumber = 1

答案 1 :(得分:0)

通常最好使用VARCHAR(255)utf8mb4编码。如果您需要长篇数据,例如论文,多段等,请使用TEXTLONGTEXT

这确实是一个表问题:

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
);

如果需要,可以通过添加另一个调查表来获得多个调查表,或者仅按原样使用该数字对问题进行分区。

相关问题