Introduction to SQL
Relational databases Practical level: A data storage that can effectively been queried with SQL query language A database consists of one or several tables Common relational database program: Oracle Access MySQL PostgreSQL
Exel-sheet Let's try to move this to SQL-database First_nameFamily_nameeye colourshoesizeweight LeenaLahtinenblue3858,6 PekkaSalobrown4175,9 HeikkiMäkeläbrown4385,3 PekkaVirtanenblue3654,8 PekkaVirtanenbrown4291,5 MattiLehtogreen4188,7
Creating tables At kaivos.csc.fi, an empty database is created by CSC The user must construct the tables used in the database In table construction you must define the field (column) names and the type of data that will be stored to each field You may also define test conditions or default values Datatypes define what data can be stored and how much space each data item requires
Some data types in MySQL String data CHAR(length) VARCHAR(length) TEXT long text BLOBbinary data, images, files Numerical data INT or INTEGER: Can have values – Also TINYINT, SMALLINT, MEDUIMINT, BIGINT DECIMAL (digits, decimals): exact decimal numbers FLOAT and DOUBLE: approximate numerical values DATES and time DATE, TIME and TIMESTAMP String data (text) VARCHAR(length) Numerical data INT or INTEGER ( String data (text) VARCHAR(length) Numerical data INT or INTEGER Can have values
Exel-sheet CREATE TABLE `list` ( `first_name` VARCHAR(20), `family_name` VARCHAR(40), `eye` VARCHAR(5), `shoe_size` INT, `weight` DECIMAL(4,1) ) CREATE TABLE `list` ( `first_name` VARCHAR(20), `family_name` VARHAR(40), `phone` VARCHAR(30), `shoe_size` INT, `weight` DECIMAL(4,1) ) First_nameFamily_nameeye colourshoesizeweight LeenaLahtinenblue3858,6 AskoSalobrown4175,9 HeikkiMäkeläbrown4385,3 PekkaVirtanenblue3654,8 PekkaVirtanenbrown4291,5 MattiLehtogreen4188,7
Using the table You could now load in the data to the database 1. Store the data as csv or tab delimited text file 2. Use mysqlimport tool (note that the column order in the text file must match to that of database) When data is loaded, you can start doing queries using MySQL client and SELECT-statements: SELECT * FROM list WHERE first_name=”Pekka”; SELECT shoe_size, weight FROM list WHERE weight>60;
The table created is not efficient No primary key defined Data repetition To improve the situation Add primary key field Split file (normalization)
New tables ID First_na me Family_nameeye_type shoe size weight 1LeenaLahtinen13858,6 2PekkaSalo24175,9 3HeikkiMäkelä24385,3 4PekkaVirtanen13654,8 5PekkaVirtanen24291,5 6MattiLehto34188,7 eye_ty pe colour 1blue 2brown 3green PERSONS EYES
Table 1. IDFirst_nameFamily_nameeye_type shoe sizeweight 1LeenaLahtinen13858,6 2PekkaSalo24175,9 3HeikkiMäkelä24385,3 4PekkaVirtanen13654,8 5PekkaVirtanen14291,5 6MattiLehto34188,7 CREATE TABLE `persons` ( `ID` INT, `first_name` VARCHAR(20), `family_name` VARHAR(40), `eye_type` SMALLINT, `shoe_size` SMALLINT, `weight` DECIMAL(4,1), PRIMARY KEY (`ID`) ) CREATE TABLE `persons` ( `ID` INT, `first_name` VARCHAR(20), `family_name` VARHAR(40), `eye_type` SMALLINT, `shoe_size` SMALLINT, `weight` DECIMAL(4,1), )
eye_typecolour 1blue 2brown 3green EYES CREATE TABLE `eyes` ( `eye_type` SMALLINT, `colour` VARCHAR(5), PRIMARY KEY (`eye_type`) )
Using the new database structure You could now load in the data to the database: – 1. Store the data as a tab delimited or csv text file – 2. Modify your data set to match the database structure – 3. Use mysqlimport tool When data is loaded, you can start doing queries using MySQL client and SELECT-statements: SELECT shoe_size, weight FROM persons WHERE weight>60; SELECT id, eye_colour FROM persons, eyes WHERE first_name=”Pekka” AND persons.eye_type=eyes_eye.type;
Main SQL commands SELECT retrieve data from the database DELETEdelete rows from database INSERT add data to a table UPDATEchange data in a table
SELECT SELECT column names FROM table name(s) WHERE selection condition ORDER BY how to order the results GROUP BY how to group the results HAVING what to search form the grouping results LIMIT maximun number of results to show SELECT DISTINCT print identical result lines only once SELECT column1, column2 FROM table_name
When constructing WHERE condition you can use: Comparison operations (not a complete list): =Equal >= Greater than or equal > Greater than GREATEST() Return the largest argument IS Boolean comparison LEAST() Return the smallest argument <= Less than or equal <Less than operator LIKE Simple pattern matching != Not equal operator Logical operations: AND OR NOT XOR Other things Use quotation marks with strings...WHERE first_name=”Pekka” Use brackets to define comparison order...WHERE a!=0 AND( b=0 OR C=0)
Aggregate functions for SELECT AVG() Return the average value of the argument COUNT(DISTINCT) Return the count of a number of different values COUNT() Return a count of the number of rows returned MAX() Return the maximum value MIN() Return the minimum value STD() Return the population standard deviation SUM() Return the sum VARIANCE() Return the population standard variance SELECT COUNT(*) FROM list WHERE first_name=”Pekka”; SELECT MAX(weight) FROM list;
DELETE and UPDATE Use DELETE to remove data from a table DELETE FROM list WHERE first_name=”test_patient”; Use UPDATE to change values in a table UPDATE list SET shoe_size=44 WHERE family_name=”Salo”
INSERT INTO You can add data to a table with command INSERT INTO Note that you must give proper values for all the columns if you wish to avoid null-values In scripts you should explicitly define the connection between a value and column name INSERT INTO list (First_name, Family_name, eyecolor, shoesize, weight) VALUES ( “Ville”, “Mäki”,”blue”,43,72.8)
With real (i.e. big) databases Use EXPLAIN command to test how heavy your query is Consider indexing some of the columns Keep the MySQL manual pages open and read them: