Adding and removing columns in attribute tables

“Underlying each vector layer in QGIS is a flat table - the columns of which can be removed or added.”

Behind every vector layer in QGIS you will find a data table (attribute table). The data in this table describe the attributes of each feature. Depending on the origin of this data, the table can be a relational database table or view, a file-based table (e.g. .dbf or .csv), or remote data (e.g. a WFS layer). When QGIS has the needed permissions to edit the actual structure of that table, you can use its field management functions to do so.
In this exercise, we will take a look at how.

You try:

Goal: To add and remove columns from a layer's data table

  • Load the Countries layer from the adding_removing_columns.gpkg provided in the exercise data (the ne.sqlite only contains the attribute table data).
  • Open the attribute table for the layer
  • Enable the editing mode
  • Use the delete fields function to remove the columns listed in the specification table below
  • Use the new field column to add a new column called 'visited' giving it the options listed in the specification table below
  • Save your table edits

Check your results

Scroll across to the right of the table - do you see your newly added column?

Name Expectation

Countries layer

ne_10m_admin_0_countries in the exercise data

Fields to delete

name_alt, name_local

Field to add

  • name

visited

  • comment

Have I visited this country

  • type

text

  • length

3

More about

  • You can also manage the columns for the data table in the layer properties dialogue.
  • Some formats (e.g. shapefile) place limits on the number of columns you can have and on the length of the field names you can use - in particular, shapefiles limit you to 10 characters.
  • You need to ensure you have the needed permissions to add and remove columns - especially if the data source is a relational database.
  • Think carefully before you delete a column - once the data is gone there is no 'undo' option to get it back!
  • You should choose an appropriate type for new columns when you add them - the following are supported:
Type Notes
Whole number (integer and 64-bit integer) Use 64 bit when you need to store large whole numbers
Decimal Use when storing floating-point numbers
Text Use for free-form text - be sure to choose an appropriate length
Date Use for storing dates (without time)
Time Use for storing times (without dates)
Date & time Use when you need to store both data and time

Check your knowledge:

  1. After removing a column and saving your edits, you can press undo to get it back again:

    1. True
    2. False
  2. Indicate which statements are correct:

    1. there is no limit to the number of columns you can have
    2. when creating a column some column types require that you set the field length
    3. the only place to manage fields / columns is the table view
  3. Is it possible to organize the order for the columns you have created:

    1. No, you will need to save your edits and create a new layer using the refactor tool
    2. Yes, there is a native tool in attribute table that controls ordering columns
    3. You will need to use the layer as a virtual layer
  4. In the attribute table you can create columns using the New Field tool or Field calculator. What are the key differences betwen the two methods:

    1. Field calculator is used when you need to create an empty column
    2. Field calculator is used when you need to create a non empty column.