英文:
How to have a column set to a custom type?
问题
I want to set the column questions
in the quizs table to be equal to an array of type _question. Is there any way to do this in SQL or is there a better approach?
我想将表格quizs
中的列questions
设置为与类型_question
的数组相等。在SQL中是否有实现这一目标的方法,或者是否有更好的方法?
英文:
I am new to using SQL and relational databases. I am creating a basic quiz web app where people can create their own quizzes and have all the data stored in a cloud SQL database. All of this works fine but I have a problem now. Here are two types of data:
_question {
question_id: number;
title: string;
prompts: string[];
correct_answer: number;
}
_quiz {
quiz_id: number;
title: string;
questions: _question[];
created_on_epoch: number;
}
This is all using TypeScript to define the data. I use this as a blueprint and convert it into creating a SQL table with the types above. For example, I have a table in the database created with this command CREATE TABLE quizs(quiz_id SERIAL PRIMARY KEY, title VARCHAR(150), questions {DONT KNOW WHAT TO PUT HERE}, created_on_epoch bigint);
I want to set the column questions
in the quizs table to be equal to an array of type _question. Is there any way to do this in SQL or is there a better approach?
答案1
得分: 2
可以在PostgreSQL中向表格添加一个string数组类型的列。要向名为my_table的表格添加一个名为questions的string数组类型的列,DDL应该如下所示:
ALTER TABLE my_table ADD COLUMN questions text[];
请注意,在数据类型后面的[]指定了这是一个数组数据类型。text数据类型用于在PostgreSQL中存储字符串。
一旦添加了列,您可以使用ARRAY构造函数将值插入数组列中。例如:
INSERT INTO my_table (questions) VALUES (ARRAY['question1', 'question2', 'question3']);
要从questions表格更新quizs表格,您可以使用子查询和ARRAY构造函数。例如:
UPDATE quizs
SET questions = ARRAY(
SELECT ROW(question, answer)::question
FROM questions
WHERE quiz_id = quizs.id
);
要确定数组列是字符串还是数字类型,可以使用pg_typeof()函数。
SELECT pg_typeof(questions) FROM my_table;
这将返回questions列的数据类型,它可能是text[](字符串数组)或integer[](数字数组)。
但我鼓励您考虑更传统的表格结构,使用“规范化”方法,或者考虑使用JSON而不是数组。还请注意,许多关系型数据库管理系统不支持数组数据类型,但有更多的系统支持JSON。这个决策可能受到您打算用作应用程序前端的技术的影响。通过使用这里所见的简单表格结构,您可能会在以后通过查询复杂性付出更多的代价。
英文:
It is possible to add a column of type string array to a table in PostgreSQL. To add a column called questions of type string array to a table called my_table, the DDL would look like this:
ALTER TABLE my_table ADD COLUMN questions text[];
Note that the [] after the data type specifies that this is an array data type. The text data type is used to store strings in PostgreSQL
Once the column is added, you can insert values into the array column using the ARRAY constructor. For example:
INSERT INTO my_table (questions) VALUES (ARRAY['question1', 'question2', 'question3']);
To update the quizs table from the questions table, you can use a subquery and the ARRAY constructor. e.g.
UPDATE quizs
SET questions = ARRAY(
SELECT ROW(question, answer)::question
FROM questions
WHERE quiz_id = quizs.id
);
To determine whether an array column string or numeric type, use the pg_typeof() function.
SELECT pg_typeof(questions) FROM my_table;
This will return the data type of the questions column, which will be either text[] for a string array or integer[] for a numeric array.
BUT
I would encourage you to consider a more conventional table structure, using a "normalized" approach, or, perhaps consider use of JSON instead of arrays. Also note that many Relational DBMS systems do not support array data types, but many more will have some JSON support. (& This decision might be influenced by what tech you intend to use as the front-end for your application.)
What you may save now through the simple table structure seen here will probably cost you more later via query complexities.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论