3 Day Training Databases and Scripting What is a Database? A structured collection of records or data that is organized for efficient storage and retrieval. There are several common types of databases, and each type has its own data model (how the data is structured). The Relational Model is the most commonly used. Common examples are MySQL, Microsoft SQL Server, and Oracle. Tables The foundation of every database system is a table. Every database consists of one or more tables, which store the database’s data. Each table is identified by a name (e.g. "Customers" or "Orders") and consists of columns and rows. The database table columns have their own unique names and data types. Table columns can have various attributes defining the column functionality (a primary key, an index on the column, a certain default value, etc.). While columns describe the data types, the rows contain the actual data. Relational Database A relational database consists of separate tables, having explicitly defined relationships, and whose elements may be combined through queries. Here we have a downtime event table that is related to a specific piece of equipment. The main benefit comes from saving storage space. We are storing the Equipment details once, and being able to retrieve them for each downtime record. What is SQL? SQL or Structured Query Language is a standard computer language for accessing and manipulating database systems. SQL statements are used to create, maintain and query relational databases. There are 4 types of commands and we will be using all of them. Examples: • SELECT Firstname FROM Contacts WHERE Lastname = ‘Smith’ • INSERT INTO Contacts VALUES (‘Joe’, ‘Smith’) • UPDATE Contacts SET Lastname = ‘Howard’ WHERE ID = 1 • DELETE FROM Contacts WHERE Lastname = ‘Smith’ SELECT The SELECT statement is used to fetch data from a database. The result is stored in a result table, called the result-set. The SELECT clause allows you to get all columns (*), or a list of column names. You also have access to many functions to concatenate, do math, alias column names, and much more. Examples: SELECT * FROM Customers SELECT Name, Address FROM Customers SELECT SUM(duration) as ‘Total Seconds’ FROM Downtime WHERE Clause The WHERE clause is used to filter only those records that match a certain criteria. Operators Allowed in the WHERE clause: = - Equal > - Greater than >= - Greater than or equal BETWEEN - Between an inclusive range IN - Value in a list of values <> - Not equal < - Less than <= - Less than or equal LIKE - Search for a pattern AND & OR operators are used to filter records based on more than one condition. Examples: SELECT * FROM Customers WHERE State = ‘CA’ SELECT * FROM Customers WHERE Address LIKE ‘%St%’ AND State = ‘CA’ UPDATE The UPDATE statement is used to update records in a table. If a WHERE clause is not used, all records will be updated. Examples: UPDATE Customers SET Name = ‘Inductive Automation’ UPDATE Customers SET Address = ‘2110 21st Street’ WHERE ID = 1 INSERT The INSERT statement is used to insert a new row in a table. You can optionally use sub-queries to fill in your values. Example: INSERT INTO Customers (Name, Address, City, State, Zip, Country, Phone) VALUES (‘Inductive Automation’, ‘2110 21st Street’, ‘Sacramento’, ‘CA’, ‘95818’, NULL, ‘800-266-7798’) INSERT INTO Customers VALUES (SELECT * FROM OtherCustomers) DELETE The DELETE statement is used to delete records in a table. If a WHERE clause is not used, all records will be deleted! Examples: DELETE FROM Customers DELETE FROM Customers WHERE Name = ‘Inductive Automation’ ORDER BY Clause The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword. Examples: SELECT * FROM Customers ORDER BY State ASC, Name ASC SELECT * FROM history ORDER BY timestamp DESC JOIN The JOIN keyword is used in an SQL statement to combine data from two or more Tables based on a specific condition. The purpose is to bind data together, across tables, without repeating all of the data in every table. It is common to alias the table names for shorter column names. • INNER JOIN: Return any rows where the ON condition is true • LEFT JOIN: Return all rows from the left table (downtime in the example below), and attach data from the right table where the ON condition is true • RIGHT JOIN: Return all rows from the right table (equipment in the example below), and attach data from the left table where the ON condition is true • FULL JOIN: Return all rows from both tables and join data where the ON condition is true Example: SELECT * FROM downtime d INNER JOIN equipment e ON e.ID = d.equipmentID GROUP BY Clause The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Example: SELECT CauseID, SUM(Duration) FROM Downtime GROUP BY CauseID Example Bringing it all together, you can have complicated queries that return very useful information. SELECT FROM d.ID, e.Name as ‘Equipment Name’, SUM(d.Duration) as ‘Total Seconds’ Downtime d INNER JOIN Equipment e ON d.EquipmentID = e.ID WHERE e.Name LIKE ‘%Filler%’ GROUP BY e.Name ASC Further Help Inductive Automation http://www.inductiveautomation.com (800) 266-7798 - Press 2 for Technical Support support@inductiveautomation.com SQL Tutorials http://www.sqlzoo.net http://www.w3schools.com/sql