
Managing tables
DB Manager provides functions to create, rename, edit, delete, and empty tables using tools found under the Table menu. In this section, we will discuss each tool.
Creating a new table
Creating new tables using DB Manager is fairly straightforward. When creating a new table, you can specify whether it will be a spatial table or a nonspatial table. In this section, we will create a new spatial table in SpatiaLite to hold data about mountain peaks in a park; to do this, perform the following steps:
- Open DB Manager by clicking on DB Manager under Database. Expand SpatiaLite and select GiffordPinochet.sqlite in the Tree panel.
- Navigate to Table | Create Table to open the Create Table window.
- Enter
Peaks
as the table name. - Click on the Add field button to add a new table field. A new row will appear in the field list. Set the Name field to
Name
and the Type field tocharacter(20)
from the list of field type options. - Click on the Add field button to add a second field, with the Name field set to
Elevation
and the Type field set tointeger
. - Set the Primary key field to Name. This will require the peak names to be unique.
- Select Create geometry column and choose the following options:
- Create geometry column: POINT
- Name:
geom
- Dimensions: 2
- SRID:
26910
- Select Create spatial index to create a spatial index for the table.
- Your dialog should look like the following screenshot. If it does, click on the Create button to create the new table.
- If the table is created successfully, a prompt will confirm that everything went fine. Dismiss the dialog, then click on the Close button to close the Create Table window.
- To view the new
Peaks
table, you'll need to refresh the Tree panel by selecting GiffordPinochet.sqlite in the tree and then click on Refresh under Database, or press the F5 key on your keyboard. Note that thePeaks
table has the point icon, indicating that it is a geometry table.
Renaming a table
To rename a table, perform the following steps:
- Open DB Manager by clicking on DB Manager under Database. In the Tree panel, expand the tree and select the database that contains the table that you wish to rename.
- In the Tree panel, select the table you wish to edit. Right-click on the table and choose Rename from the contextual menu to rename it.
Editing table properties
To edit table properties, perform the following steps:
- Open DB Manager by clicking on DB Manager under Database. In the Tree panel, expand the tree and select the database that contains the table that you wish to edit.
- In the Tree panel, select the table that you wish to edit. Navigate to Table | Edit table to open the Table properties window.
- The Table properties window (shown in the following screenshot) has three tabs—Columns, Constraints, and Indexes—that allow the editing of their respective table properties.
The Columns tab lists all the fields, their type, whether they allow null values, and their default values. Below the field list, there are four buttons. The Add column button opens a window and allows you to create a new field and specify its properties. The Add geometry column button opens a window and allows you to create a new geometry field and specify its properties. The Edit column button opens a window and lets you change the selected field's properties. The Delete column button deletes the selected field.
The Constraints tab lists all the constraints on the table; their name, their type, and the column(s) that are affected by the constraints. The Add primary key/unique button opens a window and allows you to create a new primary key constraint. The Delete constraint button deletes the selected constraint.
The Indexes tab lists all the indexes on the table, their name, and the column(s) that are a part of the index. The Add index button opens a window that allows you to create a new index by selecting the field to index and provides an index name. The Add spatial index button adds a spatial index to the table. This option is only available if the table is a geometry field. The Delete index button deletes the currently selected index.
Deleting a table
There are two ways to delete a table from a database within QGIS: by using the Browser panel in QGIS Desktop or by using the DB Manager.
To delete a table using the Browser panel in QGIS Desktop, expand the database from which you wish to delete a table, then right-click on the table and choose Delete layer.
To delete a table using DB Manager, open DB Manager by clicking on DB Manager under Database. In the Tree panel, expand the tree and select the database that contains the table that you wish to delete. In the Tree panel, select the table that you wish to delete. Then, click on Delete table/view under Table. You can also right-click on the table in the Tree panel and choose Delete from the contextual menu.
Emptying a table
To remove every record from a table without deleting the table, open DB Manager by clicking on DB Manager under Database. In the Tree panel, expand the tree and select the database that contains the table that you wish to empty. In the Tree panel, select the table you wish to empty. Then, click on Empty table under Table.