Fields and Data Types in Tables
Initially, we will only work in the 'postgres' database, and only create tables in the 'public' schema, which are the defaults. If you wish to create additional databases, you may right mouse click on the 'Databases' icon and select 'Create' - 'Database ...'. Similarly, if you wish to create a new schema within a database, right click on the 'Schemas' icon and 'Create' - 'Schema...'.
You may give any name to your databases, schemas, tables, and fields, however there are some suggested conventions that will prevent problems. You will use other software to interact with your database, and these software programs may not be able to deal with some characters you want to include in your names. It is best to avoid using spaces, punctuation, and capitalization in your names. The table name 'Pottery & Lithics#' is allowed but is not recommended. Use 'pottery_and_lithics_numbers' instead, substituting the underscore character for the spaces.
Here is what the schemas and tables look like in the Pgadmin view of PostgreSQL. Take note of the 'postgres' database and the 'public' schema in the hierarchy. The columns in the 'areas' table are also visible. Although you will see many other things in the hierarchy here, the majority of these settings you will never need to use, as they provide functionality that a typical archaeological project would not need.
This section will mainly focus on the main building blocks of the table – the fields. As we learned previously, tables are composed of columns, called fields, and each field stores a certain type of data. We might have a field containing the decimal numeric value of a rim diameter of a bowl, or a text field that stores a shape value of 'bowl'. Create a field whenever you have another piece of data you wish to collect for each row. Some fields should contain unique identifying information for each row, the keys we will discuss later.
Here we will create our first table and add its fields. Each archaeological project uses a slightly different way of naming and structuring its data, and for this guide we will follow the system outlined in Roosevelt, Cobb, et al (2015). That recording system was designed from the start for direct-digital recording in a relational database. The top spatial recording element is the area, this is a square trench excavated on a site. Here we will create a table to record all of the areas at a site.
We will create a new table in the public schema of our postgres database by right mouse clicking on the ‘tables’ icon and selecting ‘Create’ … ‘Table…’. Name the table ‘areas’, and proceed to the ‘columns’ tab where we will create our fields. The first two fields we will create are ‘area_easting’ and ‘area_northing’. These are the Universal Transverse Mercator (UTM) coordinates of the southwest corner of each area, simplified to include only the three digits from the thousands to the tens place. These are x, y coordinates in the unit of decameters, that give a standard location of the trench on the ground, while at the same time providing a unique name for the area. These two fields are integer numbers, since they do not contain digits after the decimal point.
To create a new field, press the + button on the right side of the columns tab. Enter the name of the field, and then select the data type. Ignore the other options for each column, for the time being. In addition to the area_easting and area_northing, we will add four more fields: a character varying 'description', that will provide a brief text description of the area, a date for the 'date_opened', when the area was first excavated, a numeric 'elevation_top_meters' to record the topmost elevation prior to excavation, and a boolean for 'is_active', which indicates if the area is currently being excavated. The following screencast demonstrates all of these steps. Should you receive a message about the table missing a primary key upon saving, you can simply ignore that for now.
You will notice as you are creating the fields, that you have many options for data type. An archaeological project would rarely have a need to use more than a few of these types. The usual data types you would use are the ones we have already created: integer, character varying, numeric, date, and boolean. Let us take a closer look at these types here. For complete details, you can also refer to the PostgreSQL documentation.
- integer: A number without digits after the decimal point. You can store any number from about -2 billion to +2 billion.
- character varying: This is how we store text. An individual character is a single letter, number, or punctuation mark. For example, ‘a’ or ‘A’, ‘1’, or ‘+’ are all individual characters. In order to form words or sentences, we need to string characters together. The ‘varying’ aspect of the data type means that you can store any number of characters in the field. Thus, you could store ‘A’ or you could store ‘20 Brown Burnished Bowls!’. You can specify a length limit in the database if you want to restrict the number of characters that a user can enter.
- numeric: Any type of number, including those with digits after the decimal place. You must indicate how many digits can be stored in this numeric field, using the two values: length and precision. In the Pgadmin view, length is the total number of digits you can store in the number, and precision is the maximum number of digits you can have to the right of the decimal point. Thus 2351.41 has a length of 6 and a precision of 2 (note that 50.1 would also fit into 6/2, but 50.002 would not). Please note that these terms are not standardized and you may see the use of the term scale in other documentation, and the term precision may mean something else.
- date: Stores a standard-formatted date, with month, day, and four-digit year.
- boolean: A logical indicator of true or false, stored in the computer as 1 or 0. Named after the 19th century British mathematician George Boole.
Exercise: Create a ‘contexts’ table and a ‘samples’ table.
For this exercise, you should first create the ‘areas’ table as shown above, then you will create the ‘contexts’ and ‘samples’ tables as described below. You will create these tables on your PostgreSQL database server, in the default database and schema. You will determine how to name the fields and which data type to assign to each field, following the instructions discussed above.The ‘contexts’ table records data about each context excavated within the area. Contexts are identified first by reference to their area, so you will need to add the area_easting and area_northing fields into this ‘contexts’ table. Within each area, each context receives a running number, starting from 1, this field is called a ‘context_number’. These three fields uniquely identify each context and indicate to the archaeologist the location of the context on the site.
We would also like to record additional information about the context:
- a context type to differentiate such types as walls, fills, pits, or other archaeologically determined contexts
- the dates when the context was opened and closed
- the top and bottom elevation limits of the context, in meters
- a descriptive narrative that discusses the characteristics of the context and its excavation
The ‘samples’ table records data about all the material found within the context, such as pottery sherds or lithic tools. Samples are identified first by reference to their area and context, so you will need to add the area_easting, area_northing, and context_number fields into this ‘samples’ table. Within each context, each sample receives a running number, starting from 1, this field is called a ‘sample_number’. These four fields uniquely identify the sample and indicate to the archaeologist where the sample was found.
We would also like to record additional information about the sample:
- count of the number of objects included in the sample
- a general material to differentiate such materials as stone, ceramic, organic, or other archaeologically determined materials
- a specific material to differentiate material types within the general classification, such as animal bone or shell within the general ‘organic’ material
- a general category to indicate the class of the object, such as vessel, tool, figurine, or other archaeologically determined categories
- a specific category to indicate the specific type of object within its general category, such as cup, bowl, or jug within the ‘vessel’ general category
- weight in kilograms
In a later lesson, we will learn how to export the database from PostgreSQL in order to share it with someone else.
This page has paths:
- Relational Databases for Archaeology Peter Cobb