relational database defined
A relational database
is a collection of data items organised as a set of tables.
Relationships can be defined between the data in one table and the data in another or many other tables.
The relational database system will provide mechanisms by which you can query the data in the tables, re-assemble the data in various ways without altering the data in the actual tables.
This querying is usually done using SQL (Structured Query Language). This is a relatively straight forward language to learn, certainly for simple queries.
You could have a relational database with only one table, but then you wouldn’t have any relationships and it would be more like a spreadsheet.
Databases are designed to allow efficient querying against very large tables, more than the 1M rows allowed in an Excel spreadsheet.
What is a table?
As were have noted above, a single table is very much like a spreadsheet. It has rows and it has columns. A row represents a single observation and the columns represents the various variables contained within that observation. The rows may or may not have an index or key column as a means of uniquely identifying a row. The columns typically have a name associated with them indicating the variable name, or in one of the examples we are going to use a question number from a survey.
A column always represents the same variable for each row contained in the table. Because of this the data in each column will always be of the same type, such as an Integer, or Text value for all rows in the table.
Datatypes are discussed in the next section.
What is a data type?
- A data type is a description of the kind of data in a table column.
- Typical examples will be Integer or Text. In SQLite there is quite a number of possible data type.
Show most used data type table from notes
Why do tables have key columns?
Whenever you create a table, you will have the option of designating one of the columns as the primary key column
.
The main property of the primary key column is that the values contained in it must uniquely identify that particular row. That is you cannot have duplicate primary keys.
This can be an advantage which adding rows to the table as you will not be allowed to add the same row (or a row with the same primary key) twice.
- The primary key column for a table is usually of type Integer although you could have Text.
For example if you had a table of car information, then the “Reg_No”
column could be made the primary key as it can be used to uniquely identify a particular row in the table.
A table doesn’t have to have a primary key although they are recommended for larger tables.
What different types of keys are there?
In addition to the primary key
, a table may have one or more Foreign keys
. A foreign key does not have to be unique or identified as a foreign key when the table is created.
A foreign key in one table will relate to the primary key in another table. This allows a relationship to be created between the two tables. If a table needs to be related to several other tables, then there will be a foreign key (column) for each of those tables.
How does the database represent missing data?
All relational database systems have the concept of a NULL
value. NULL
can be thought of as being of all data types or of no data type at all. It represents something which is simply not known.
When you create a database table, for each column you are allowed to indicate whether or not it can contain the NULL value. Like primary keys, this can be used as a form of data validation.
In many real life situations you will have to accept that the data isn’t perfect and will have to allow NULL or missing values in your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