Jview TechnicalNote en v1 SQLBasics
Jview TechnicalNote en v1 SQLBasics
December 2007
2
Table of Contents
1 Overview............................................................................................................ 1
1.1 Purpose ...................................................................................................... 1
1.2 Scope .........................................................................................................1
2 Jigsaw Server and Database Introduction ......................................................... 2
2.1 Server ......................................................................................................... 2
2.1.1 Accessing the Production Server......................................................... 2
2.2 Database .................................................................................................... 5
2.2.1 Accessing the Database ...................................................................... 5
3 SQL Commands ................................................................................................ 7
3.1 Before Beginning ........................................................................................ 7
3.1.1 Viewing Text ........................................................................................ 7
3.1.2 Copy and Paste Text ........................................................................... 8
3.1.3 Resize Window.................................................................................... 8
3.2 Basic Commands........................................................................................ 8
3.2.1 Main Menu........................................................................................... 9
3.2.2 View Column Names in a Table .......................................................... 9
3.2.3 SQL Help ........................................................................................... 10
3.2.4 Quit .................................................................................................... 11
3.3 The ‘select’ Command .............................................................................. 11
3.3.1 Select All Entries in a Table............................................................... 11
3.3.2 Select Specific Data .......................................................................... 12
3.3.3 Select Data in an Ordered Manner .................................................... 13
3.3.4 Select Data with Wildcards ................................................................ 13
3.4 A Few Shortcuts ....................................................................................... 14
3.4.1 Clear the Screen................................................................................ 14
3.4.2 Delete all Text After the Cursor ......................................................... 14
3.4.3 Delete all Text Before Cursor ............................................................ 14
3.4.4 View Old Commands ......................................................................... 14
3.4.5 Search Through Previously Typed Commands ................................. 15
4 Basic Database Layout .................................................................................... 17
4.1 A Sample Table ........................................................................................ 17
4.2 A Sample Relationship ............................................................................. 18
4.3 A Real Life Example ................................................................................. 18
4.3.1 Changing Records in MineOPS......................................................... 22
4.3.2 Viewing Relationships ....................................................................... 23
i
ii
1 Overview
1.1 Purpose
The purpose of this document is to provide an introduction to Structured Query Language
(SQL). This document will introduce basic SQL query commands, how to connect to the Jigsaw
server, and give a basic introduction to the Jigsaw production database (postgreSQL). This
document is not intended to be a stand alone user guide, but rather to give an introduction on
viewing and retrieving data generated from the MineOPS Application.
1.2 Scope
This document is intended to serve as an introductory guide to the SQL computer language,
intended for personnel involved with the MineOPS system. The intended users are:
• Dispatch Engineers
• Managers
The data contained on the Jigsaw Server can be viewed, modified, added to or deleted from
using standard SQL commands. Access to the database should be limited to those with the
correct privileges, as sever and critical damage can be inflicted upon the MineOPS Application
from the database.
This introduction to basic SQL commands is intended only for those new to SQL and databases
in general. This document is directed towards the user unfamiliar with SQL, and because of this,
there is no attempt to explain the more advanced features of the database in this document. At
other times, certain topics will be presented in a manner to simplify the explanation.
1
2 Jigsaw Server and Database Introduction
This section will quickly introduce the Jigsaw server, and the database contained on it, as well
as methods of accessing them. If you already have access to the jmineops database, you may
skip to Section 3.
2.1 Server
There are two Jigsaw Dell Poweredge 2950 Servers, named ngg01 and ngg02 respectively.
The first server contains the production database, and the second is the backup server. To view
information from the MineOPS Application, the production server, ngg01, should be used. The
next section details the steps used to access the server.
http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
Putty is an .exe file, and does not need to be installed. To access putty, simply double click the
icon from the download location, shown below.
2
A window asking permission to run will pop up. Select ‘Run’, as shown below.
The main Configuration window will appear. Enter the server I.P. address under the Host Name
field and select ‘Open’, as shown below. The server I.P. is: 10.223.102.10.
3
The application will now ask for the user name and password. Enter jigsaw as both the name
and password, shown below.
You know have a shell on the server, and should see the prompt shown below.
The server is running Ubuntu 7.04, and linux commands may be executed from this shell. The
production database, jmineops, will be introduced in the next section.
4
2.2 Database
The database behind the MineOps Application is located on the main Jigsaw server. The
database is a PostgreSQL database. PostgreSQL is an open source, object-relational database
management system. It accepts the standard SQL commands.
This document will introduce some of these SQL commands used to search and view data in
the PostgreSQL (PSQL) database. The next section demonstrates how to gain access to the
PSQL database from the ngg01 server.
ngg01:~$ sample_linux_command
The line above tells the linux prompt to execute the command
sample_linux_command. The user must type the text – ‘sample_linux_command’
exactly as shown, since linux is case sensitive.
jmineops=> sample_psql_command
The line above tells the postgres database prompt to execute the command
sample_psql_command. The user should type the text – ‘sample_psql_command’
exactly as shown. SQL commands may be entered as all lowercase characters,
or all uppercase characters. For simplicity, this document will use lowercase bold
text for the SQL commands, and the fields to be executed will be put in brackets
[].
5
A further example of this is shown below:
This convention will be looked at again in more detail in the next section.
Since this is the first real command executed, it will be broken down below in parts:
jmineops=>
The user is now connected to the database, and is ready to issue SQL commands to be
executed. The next section will describe these commands in detail.
6
3 SQL Commands
Once access to the PSQL database has been obtained, the user is ready to execute SQL
commands. SQL commands are recognized by many different databases. PSQL, the Jigsaw
database, is one of these. The following sections will break down common tasks used to access
and view data.
if there is a large amount of data to display however, the contents will be brought up for the user
to scroll up and down to view, using the up and down arrows on the keyboard. To exit out of this
view of the data, the ‘q’ key must be pressed. A common mistake is to begin typing commands
before exiting the view. To fix this, any text accidentally entered must be deleted, and the ‘q’
button must be typed. An example of this is shown below.
The first screen shows a green box in the bottom left corner of the screen. This shows that the
window can be moved up and down, but is not ready to accept select statements until q has
been pressed.
7
The second screen shows a common error; the user is trying to enter a command, but the
window is still in view mode. The ‘q’ button must be pressed to clear this screen.
The command prompt, shown below, must be visible for the database to accept a command.
jmineops=>
jmineops=> \d
All the tables are labeled under the Type column, and can be viewed with a ‘select’ statement.
jmineops=> \d table_name
9
The columns are displayed on the left side. This command is helpful for sorting data, when you
don’t remember the exact name of the columns in a table.
jmineops=> \h
This will list the commands that can be entered, shown below:
10
The only command needed however, will be the ‘select’ statement. Section 4.3 deals with this
command.
3.2.4 Quit
To exit out of the PSQL database, type \q.
jmineops=> \q
The part in bold is how every select statement must begin. In it’s simplest form, the table name
follows the bold section. Finally, all select statements must end with a semicolon.
Using the select statement in its basic form will display the whole contents of a table. At times
you will want to view a particular record, or set of records. The next sub-section examines this
topic.
The first part of the statement is the same, we just expand it, starting with the where clause.
The where clause specifies that you only want to view part of a table, based on certain criteria.
After typing where, you must specify by which column you want to sort the data by. You can
choose any column to sort the data by.
After choosing the column, you must type the equal sign followed by the column field you wish
to view. To demonstrate this, we will use the workers table as an example.
Viewing the table using a simple select statement gives 298 results, which may be inconvenient
for searching through the data.
jmineops=> \d workers
Every column is a candidate for sorting. Some examples are listed below:
12
The three examples above were useful for selecting one item from a table. If you want to select
multiple data from the complete list, for example all workers from crew a, you use the same
principle.
This will order the data by the column chosen. Some examples are listed below.
You can combine the where clause with the order by clause to achieve even more customized
data. In this case, the where should come before the order by, which appears at the end just
before the semicolon.
Wildcards can help with these situations. Wildcards are used to substitute for a string. For
example, all trucks in the equipment table begin with HT. Currently, the first truck is named
HT001 and the last is named HT024. Instead of viewing all 24 trucks individually, you can use a
wild crad to replace the numbers. The letters HT are common to all trucks, so the wild card
would represent the numbers of the trucks. The format is shown below:
This will order the data by the column chosen. Some examples are listed below.
13
Ex1: jmineops=> select * from workers order by first_name;
This example will display the workers table, displayed alphabetically, starting with
the letter ‘a’.
You can combine the where clause with the order by clause to achieve even more customized
data. In this case, the where should come before the order by, which appears at the end just
before the semicolon.
This command will delete all the text typed under the cursor, and all text after the cursor, until
the end of the line is reached. All text before the cursor remains untouched.
This command will delete all the text before the cursor. The character under the cursor
becomes the first character on the line. All text after the cursor remains untouched.
14
3.4.5 Search Through Previously Typed Commands
<CTRL>r
This command will search through all commands typed in the history of commands stored by
the database. After typing <CTRL>r, the prompt is ready for you to begin typing the command
you swish to search for.
Once you begin typing, the database will search for all commands typed that match exactly
what you have typed so far.
For example, if you want to find the command: ‘select * from enum_tables where id=924;’ from
history, you could use the <CTRL>r method.
To search for the desired command, you type <CTRL>r, and then begin typing select. After you
type ‘s’, you are eliminating all text in the history that does not begin with s. The commands in
history remaining are listed below:
The most recent command in history that begins with s will appear, along with the text typed so
far.
You can continue typing, and eliminate more entries until you find your or selection, or continue
to press <CTRL>r to cycle through the remaining commands that match the text typed so far.
Once the command you want is found, press the right arrow key to select it.
16
4 Basic Database Layout
This section will briefly introduce how tables are layed out, and how relationships between
tables exist. If you have worked previously with relational databases, you may skip to Section 4.
The ‘id’ field is present in every table in the database. This number is unique for every
row, and is used to distinguish specific rows. Every row must contain an id.
The ‘updated_at’ filed is also present in every table. This filed is shown as a date n utc
time. The date is displayed first, followed by the time the record was updated at. A
sample date is: 2007-12-09 17:07:47. A ‘created_at’ filed may also be present in
addition, or replacement of the ‘updated_at’ field.
The ‘field_1’ and ‘field_2’ columns are informational fields specific to the table. They
may be numbers, text string or dates, depending on the table.
The ‘field_3_id’ column contains a numerical value that is being used by another
table.
After the last row, the number of rows is listed in parenthesis. In the sample table,
there are no rows, thus no data present.
If two rows were added to the table, it would be displayed as shown below:
Some things to note about the updated table are shown below.
Row two has a column, ‘file_2’ without data. Some columns are mandatory, while
others may be left blank. This will be looked at more in detail in the next section.
The filed_3_id column contains two integers. This is the topic of the next sub-section.
Each row contains a unique record, and must contain a unique id.
17
4.2 A Sample Relationship
This section shows a typical relation ship found in the PSQL database. Using the sample table
from the previous section, we have the following.
The ‘fied_3_id’ is referencing another table (or possibly a column in the same table). Frequently,
the table being references is actually a table name with the name of the column, minus the ‘_id’
part. Using this layout, we can look for the table called ‘field_3’.
Row 1 in the ‘example’ table is referring to ‘item_5’ in the ‘field_3’ table. The value 5 in
the sample table matches id 5 in the field_3 table, and thus its name is given as
‘item_5’. In the next Section, a real example from the MineOPS Application will be
used to clarify this relationship.
There are only four rows in the ‘field_3’ table. Rows 2,3,4 and 7 have been deleted (or
edited).
Even though both tables have a column field_2, they can have different data, and do
not relate to each other.
The ‘field_b_id’ column in the second table also references another table. Usually, id
columns with large values probably reference the ‘enum_tables’ table. .
18
The Equipment Screen from the MineOPS Application is shown below.
19
Selecting EX001, the following screen is displayed.
The relationship between the office application and the database, as well as the effect of
changing the fields in MineOPS will be examined.
20
id: 30 – The unique row identifier for excavator EX001
type: Shovel – The class the equipment belongs to, predefined by Jigsaw
name: EX001 – The name of the equipment record
created_at: 2007-07-27 17:22:32 - The date and time the record was created at
updated_at: 2007-07-27 17:22:32 - The date and time the record was created at
deleted_at: The date and time the item was deleted from the database, if applicable
revision: Unused
device_id: 40 – References the fortieth row in the devices table
status_id: 1327 – References the entry in the enum_tables table with the id of 1327
reason_id: 2 – References the second row of the reasons table
lineup_status_id: Unused
equipment_type_id: 1055 - References the entry in the enum_tables table with the id of 1055
activity_id: 924 - References the entry in the enum_tables table with the id of 924
activity_start: 2007-12-10 17:34:36 – The date and time the current activity began
size: Unused
tiedown_id: Unused
unit_id: Unused
length: Unused
height: Unused
project_id: 1 – References the first row in the projects table
time_late: Unused
prestart_check: Unused
warnings: Unused
21
4.3.1 Changing Records in MineOPS
To switch the view in MineOPS to view another piece of equipment, you simply click the mouse.
An example of this is shown below, switching from EX001 to HT001.
This simple action accessed the database to retrieve the equipment record. The same action
could be completed in the database with the following command:
22
Making the change in MineOPS has the effect of changing rows in the database. In this case we
changed from row 30 (id=30) for EX001 to row 1 (id=1) for HT001.
4.3.2.1 device_id
The device_id column for EX001 contains a value of 40. To view the relationship, we need to
look at row forty of the devices table.
23
The matching row in the devices table contains an entry for EX001. Looking at the columns, we
see that this table contains networking information for EX001. Changing the I.P. address in the
devices table, for example, will give the record in the equipment table a more complete picture
of EX001.
Both the equipment and devices table are used to set up the system correctly. The next section
shows a field that portrays the current information about a piece of equipment.
4.3.2.2 status_id
The status_id column for EX001 contains a value of 1327. This value references the
enum_tables table. We will look at that table, where the id is equal to 1327.
24
This row shows that the record for EX001 in the equipment tables is referencing the row in the
enum_tables that contains a value for status of ready. This field is also being used in the
MineOPS Appplication, shown below:
Changing the status in the office application changes the id number the equipment table is
referencing from the enum_tables table.
4.3.2.3 reason_id
The reason_id column for EX001 contains a value of 2. To look at the relationship, we need to
look at row two of the reasons table.
25
This row shows that the record for EX001 in the equipment tables is referencing the second row
(id=2) in the reasons table. This field is also being used in the MineOPS Appplication, shown
below:
The display above uses two columns from the reasons table: the descrip, and code fields.
Changing the reason, or STATUS as it is referred to in the office software, changes the row of
the reasons table that EX001 is referencing in the equipment table.
4.3.2.4 equipment_type_id
26
The equipment_type_id column for EX001 contains a value of 1055. This value references the
enum_tables table. We will look at that table, where the id is equal to 1055.
This row shows that the record for EX001 in the equipment tables is referencing the row in the
enum_tables that contains a value showing that the excavator is a Liebherr 994B. This field is
set when the equipment is added to the system, and does not need to changed once in the
system.
4.3.2.5 activity_id
The activity_id column for EX001 contains a value of 924. This value references the
enum_tables table. We will look at that table, where the id is equal to 924.
27
This row shows that the record for EX001 in the equipment tables is referencing the row in the
enum_tables that contains a value for an Activity of Waiting. This filed is constantly changing, as
the excavator moves from loading to waiting.
Not every field that contains a value in the equipment table is used in the Equipment Operations
Screen. In fact, only the following fields were used: Equipment (id), Class (type), Status
(status_id), STATUS (reason_id), Equipment type (equipment_type_id) and Deleted At
(deleted_at).
That leaves two fields in the Equipment Operations Screen that were not found in the
Equipment Table.
The first is the Operator field. This field is getting its’ information from the workers table. The
second field is the locations table. This information is collected from the Garmin GPS device
installed on the equipment in the field.
This shows that windows in the office software can contain values from multiple tables, and
finding these values in the database can be tedious.
28