Skip to contents

Survey database schema

This is the survey database schema we currently use:

surveys

This table contains survey-level metadata, such as survey name, start date, and end date, with one row per survey. The primary key is survey_id, which is a camel-case identifier for the survey (e.g., "housing_wave_1"). The survey_name column provides a more descriptive title. We may consider adding additional metadata fields in the future.

questions

This table contains one row per survey per question. The composite primary key is (survey_id, question_id), where:

  • survey_id: links to the corresponding field in the surveys table.
  • question_id: represents the name of the question column in the survey dataframe. This means there’s no single unique identifier column, but rather the combination of survey_id and question_id uniquely identifies each entry.

The question_text column stores the question label, while other metadata fields include module and response_type, which will be further explained later.

respondents

Each row in this table represents a respondent’s participation in a survey. The composite primary key is (survey_id, respondent_id), where:

  • survey_id: links to the corresponding field in the surveys table.
  • respondent_id: represents the identifier for the respondent, usually found in columns like response_id or caseid in the survey data.

Currently, the only respondent-level metadata is the weight field.

answers

This table contains one row per answer value in a survey dataframe. For instance, answers to a multiple-select question with three levels will normally be stored in columns named like some_question_1, some_question_2, and some_question_3. These will be represented as three separate entries in the questions table and three entries per each respondent in the answers table.

The composite primary key is (survey_id, respondent_id, question_id).

Each answer is recorded in both character (answer_chr) and numeric (answer_num) formats:

  • For text responses (e.g., open-ended questions), the relevant value will be in the answer_chr field.
  • For numeric responses, the relevant value will be in the answer_num field.
  • For factors and labeled numerics, both fields will contain values that can be practically useful.

Adding new surveys to the database

The process of adding a new survey to the database consists of three steps:

  1. Marking column metadata: Here, we mark all the relevant column-level metadata, such as module or response_type.

  2. Creating survey data decomposition: We start with a monolithic survey dataframe and want to store this data in the four tables: surveys, questions, respondents, and answers. So first, we prepare a list of dataframes, each corresponding to one of these tables.

  3. Uploading the decomposition: The four dataframes will then be uploaded to the database, populating their respective tables.

Marking column metadata

We start with a survey dataframe. Apart from the respondent ID and weight columns (and potentially other respondent-level columns), each question column will have a corresponding row in the questions table where relevant column-level metadata is stored. In this step, we attach all relevant metadata as column attributes using helper functions.

We provide a general function, assign_col_attribute(), to assign any kind of attribute. However, it’s recommended to use specific functions tailored for each field:

assign_col_mr_group() and assign_col_mr_group_auto() for marking multiple response groups (mr_group). The ‘auto’ function is not guaranteed to work correctly every time, but you may find it convenient to use it in conjunction with the normal one.

Here’s what it would look like:

data_marked <-
  example_survey_data_climate |>
  assign_col_module("demographics", XIDEO, XPARTY7) |>
  assign_col_module("questionnaire", starts_with("Q")) |>
  assign_col_response_type("multiple_response", starts_with("Q17_Q16")) |>
  assign_col_response_type("single_response", XIDEO, XPARTY7, Q11) |>
  assign_col_mr_group("Q17_Q16", starts_with("Q17_Q16"))

Check the marking with get_dict_for_db(data_marked). This function will effectively return the questions table representation.

Creating survey data decomposition

The function for creating a survey decomposition is srv_decompose_for_db().

  • For the surveys table, you’ll have to provide survey-level metadata, such as survey ID, survey name, and dates via the function parameters.
  • For the respondents table, you’ll have to specify the columns where respondent IDs and weights are stored.
  • For the questions table, you’ve already provided all necessary question-level metadata in the previous step.
  • For the answers table no additional effort is required.

Here’s an example:

decomposition <-
  srv_decompose_for_db(
    data_marked,
    respondent_id_col = response_id,
    weight_col = weight,
    survey_id = "climate",
    survey_name = "Opinions on Climate",
    date_start = as.Date("2022-10-12"),
    date_end = as.Date("2022-10-14")
  )

Uploading the decomposition

This step is simple. The function is db_insert_survey_decomposition(), and you’ll need to provide two components: a connection to the survey database and the survey decomposition. For details on establishing the connection, see the corresponding vignette.

An example:

con <-
  rds_connect_to_database(
    database = rds_get_database("my-database-id"),
    secret = secretManager_get_secret("my-secret")
  )

db_insert_survey_decomposition(con, decomposition)

Other considerations

Updating existing surveys

The database is designed so that adding or removing all records corresponding to a single survey is as straightforward as possible. If you need to make even minor updates to a survey, feel free to drop it completely and re-upload from scratch. Use the db_drop_survey() function for this purpose.

Splitting data by cycles

In some cases, you may start with a dataframe containing data for multiple cycles of a questionnaire. By cycles, we mean different years, rounds, or waves. Each cycle needs to be uploaded as a separate survey entry in the database. However, we can start by marking columns in the combined dataframe.

After marking the relevant metadata on the combined dataframe, the next step is to split the data into separate tables for each cycle. Once split, each cycle’s table can be decomposed and uploaded individually.

To assist with splitting, we provide the helper function split_data_by_cycles(), where you’ll need to specify the column containing cycle information and other parameters. Here’s an example:

# provided example_survey_data_housing is already marked
# this returns a list of dataframes with the same markings
data_spl <-
  split_data_by_cycles(
    example_survey_data_housing,
    cycle_col = wave,
    name_prefix = "housing_",
    drop_all_na_cols = TRUE
  )