Primary Key
A primary key is a way to uniquely identify each row in a table. While it is possible to create a database table without a primary key, it is highly recommended to configure one for each table. A primary key is comprised of either a single column, or set of columns. When multiple columns are specified as a primary key, this is known as a composite primary key. No two distinct rows in a table can have the same value (or combination of values) in those columns.
While Primary Keys can be configured in several ways, they typically meet the following criteria:
- Integer Datatype: The datatype of the key column is typically an integer, and not a varchar. The primary key is only an identifier to a specific row in a table, so an integer datatype can easily be used. Some databases support a UID or a UUID (Universally Unique IDentifier) that looks like a character string, but is something specially made for primary keys.
- Automatically Incrementing: The value of the primary key increments as rows are added. The key is usually configured to automatically increment in the database so that external applications (such as Ignition) don't have to figure out the next available value when inserting a new row.
- Statically Defined: Any row that is inserted must fill in these value(s) without creating duplicates. Configuring the primary key as automatically incrementing means that the database will automatically handle this criteria.
- Non-NULL: NULL (empty) values should not be present in the primary key. This column (or columns) will usually not allow NULL values.
Index
Indexes speed up the querying process by providing swift access to rows in the data tables, similarly to the way a book’s index helps you find information quickly within that book. Indexes are extremely important when querying large sets of data. You should create an index for the set of columns you use commonly in a WHERE
clause. For example, you should add an index on the timestamp column of a historical table when querying the table by a start and end date. Ignition does this automatically when it creates tables for Tag History or Transaction Groups.
Foreign Key
A Foreign Key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must be the primary key in the referenced table. For example, you might have a list of suppliers with an integer ID column. Then your invoices would use the supplier's ID instead of the name. These linked tables can save a lot of space because you don't have to include everything about the supplier in each invoice.
Example
Supplier (SupplierNumber, Name, Address, Type)
Invoices (InvoiceNumber, SupplierNumber, Text, Cost)
Null Value
NULL is a special marker used in SQL to indicate that a data value does not exist in the database. This way it is clear that there is no data, instead of guessing if a value of 0 is correct or just missing data. By definition, NULL is not equal to anything, even other NULL values. Every database has a function to identify if a value is NULL, usually called isNULL() or something similar.
Comments can be added to any SQL query just like with scripting. Single line comments are done with two dashes and a space: '-- '
-- This is a single line comment in a SQL query.
SELECT * FROM my_table
You can also do multi line comments by wrapping text within a forward slash and an asterisk: /* text */
/* A multi line comment
can span multiple lines. The
comment will stop when it is closed
with another asterisk and forward slash.*/