introduction to sql on graham - sharcnet
TRANSCRIPT
![Page 1: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/1.jpg)
Introduction to
SQL on GRAHAMED ARMSTRONG
SHARCNET
AUGUST 2018
![Page 2: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/2.jpg)
Background Information
2
![Page 3: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/3.jpg)
Background InformationWhat is a (Relational) Database
Dynamic collection of information.
Organized into tables, rows, and columns.
Often indexed to improve access time.
They exist in a variety of flavours.
3
![Page 4: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/4.jpg)
Background InformationTypes of Databases
Distributed / NoSQL
Object Oriented / PostgreSQL
Relational / MySQL, MariaDB
Dynamic collection of information.
Organized into tables, rows, and columns.
Often indexed to improve access time.
They exist in a variety of flavours.
4
![Page 5: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/5.jpg)
Background InformationWhat is SQL
Structured Query Language
The standard for accessing & manipulating relational databases.
There is a standard for how SQL works.
5
![Page 6: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/6.jpg)
Requesting a Database
Send a request to [email protected] with the following
information:
Your Compute Canada username.
Amount of database space needed for your project.
The system you would like an account on (Graham / Cedar).
We will create an account with a randomly generated password. The
necessary information will be stored in a '.my.cnf' file in your home
directory.
6
![Page 7: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/7.jpg)
MySQL Configuration
[client]
ssl
ssl-cipher=DHE-RSA-AES256-SHA:AES128-SHA
user=your_username
password=YyG1ZJYRxkmdfV0U
database=your_username
host=199.241.163.99
7
![Page 8: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/8.jpg)
Create, Use, Delete
Databases
8
![Page 9: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/9.jpg)
Create, Use, Delete Databases
$ ssh [email protected]
$ mysql (with .my.cnf)
$ mysql -h hostname -u username (w/o .my.cnf)
$ mysql --local-infile=1
CREATE DATABASE my_database;
SHOW DATABASES;
USE my_database;
DROP DATABASE my_database; * You won’t receive a warning
9
![Page 10: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/10.jpg)
Tables
10
![Page 11: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/11.jpg)
Tables
A database is a collection of tables.
DATABASE
TABLE TABLE TABLE
11
![Page 12: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/12.jpg)
Tables
TABLE
A database is a collection of tables.A table is a collection of data entries (tuples).
12
![Page 13: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/13.jpg)
Tables
TABLE
A database is a collection of tables.A table is a collection of data entries (tuples).An entry is a row.
13
![Page 14: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/14.jpg)
Tables
TABLE
A database is a collection of tables.A table is a collection of data entries (tuples).An entry is a row.
A data point (type) is a column.
14
![Page 15: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/15.jpg)
Table Schema
The table schema describes the contents of a table.
DOGS
NAME AGE BREED COLOR
15
![Page 16: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/16.jpg)
Table Schema
The table schema describes the contents of a table.
DOGS
NAME: STRING AGE: NUMBER BREED: STRING COLOR: STRING
16
![Page 17: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/17.jpg)
SQL Data Types
17
![Page 18: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/18.jpg)
SQL Data Types
Text Data Types
CHAR
VARCHAR
TINYTEXT
BLOB
MEDIUMTEXT
LONGTEXT
LONGBLOB
ENUM
SET
Number Data Types
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
FLOT
DOUBLE
DECIMAL
Date Data Types
DATE
DATETIME
TIMESTAMP
TIME
YEAR
18
![Page 19: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/19.jpg)
SQL Data Types
Text Data Types
CHAR
VARCHAR
TINYTEXT
BLOB
MEDIUMTEXT
LONGTEXT
LONGBLOB
ENUM
SET
Number Data Types
TINYINT
SMALLINT
MEDIUMINT
INT
BIGINT
FLOT
DOUBLE
DECIMAL
Date Data Types
DATE
DATETIME
TIMESTAMP
TIME
YEAR
19
![Page 20: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/20.jpg)
SQL Data Types
CHAR
VARCHAR
INT
DOUBLE
DATE
20
![Page 21: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/21.jpg)
SQL Data Types
CHAR / VARCHAR
INT
DOUBLE
DATE
Variables in CHAR are fixed length string up to 255 characters in length.
Variable in VARCHAR are variable length strings up to 65,535* characters in length.
If your content is a fixed size, you'll get better performance with CHAR.
*Shared across all columns.
21
![Page 22: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/22.jpg)
SQL Data Types
CHAR/VARCHAR
INT
DOUBLE
DATE
Type Storage (Bytes) Minimum Value
Signed
Minimum Value
Unsigned
Maximum Value
Signed
Maximum Value
Unsigned
TINYINT 1 -128 0 127 255
SMALLINT 2 -32768 0 32767 65535
MEDIUMINT 3 -8388608 0 8388607 16777215
INT 4 -2147483648 0 2147483647 4294967295
BIGINT 8 -263 0 263-1 264-1
Required Storage and Range for Integer Types Supported by MySQL
https://dev.mysql.com/doc/refman/8.0/en/integer-types.html
22
![Page 23: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/23.jpg)
SQL Data Types
CHAR/VARCHAR
INT
DOUBLE
DATE
The FLOAT and DOUBLE data types are APPROXIMATE. If you require and exact
decimal value, such as for currency, use DECIMAL.
23
![Page 24: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/24.jpg)
SQL Data Types
VARCHAR
INT
DOUBLE
DATE
The DATE data type represents a calendar value. There are a number of
interpretation rules that MySQL uses, as such you should stick to the SQL standard
format (YYYY-MM-DD).
24
![Page 25: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/25.jpg)
Creating a Table
25
![Page 26: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/26.jpg)
Creating a Table
CREATE TABLE employees (
name varchar(64),
id int,
start date
);
SQL command
Table name
Column names
Column data types
CREATE TABLE employees (
name varchar(64),
id int,
start date
);
CREATE TABLE employees (
name varchar(64),
id int,
start date
);
CREATE TABLE employees (
name varchar(64),
id int,
start date
);
CREATE TABLE employees (
name varchar(64),
id int,
start date
);
26
![Page 27: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/27.jpg)
Setting and Getting Values- A PREVIEW
INSERT INTO employees VALUES(
'Adam',
1,
'2018-07-07'
);
SELECT * FROM employees;
INSERT INTO employees (name) VALUES (‘Adam’);
27
![Page 28: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/28.jpg)
Creating a Table: Unsigned Integer
Table name
Table column names
Table column data types
CREATE TABLE employees (
name varchar(64),
id int UNSIGNED,
start date
);
28
![Page 29: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/29.jpg)
Creating a Table: - Reserved Keywords & Spaces
CREATE TABLE employees (
`first name` varchar(64),
`index` int UNSIGNED,
start date
);
29
![Page 30: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/30.jpg)
Creating a Table: - Reserved Keywords
CREATE TABLE employees (
`first name` varchar(64),
`index` int UNSIGNED,
start date
);
30
![Page 31: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/31.jpg)
Inserting Data Into a Table
31
![Page 32: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/32.jpg)
INSERT INTO employees (name, id, start)
values ('Adam', 1, '2018-07-07');
Inserting Data Into a Table 32
![Page 33: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/33.jpg)
INSERT INTO employees(
name,
id,
start
)VALUES(
'Adam',
1,
'2018-07-07'
);
Inserting Data Into a Table 33
![Page 34: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/34.jpg)
INSERT INTO employees(name, id, start)
VALUES(‘Adam’, 1, ‘2018-07-07’)
,(‘Steve’, 2, ‘2016-06-04’)
,(‘Craig’, 3, ‘2016-06-04’);
Inserting Multiple Data 34
![Page 35: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/35.jpg)
Retrieving Your Data
SELECT
35
![Page 36: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/36.jpg)
Retrieving Your Data
SELECT * FROM employees;
36
![Page 37: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/37.jpg)
Retrieving Your Data
SELECT * FROM employees;
SQL Keywords
37
![Page 38: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/38.jpg)
Retrieving Your Data
SELECT * FROM employees;
Column Selector
* means all
Table Selector
* means all
38
![Page 39: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/39.jpg)
Retrieving Your Data
SELECT * FROM employees;
SELECT name FROM employees;
SELECT name, id FROM employees;
SELECT name, id, start FROM employees;
SELECT id, start, name FROM employees;
39
![Page 40: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/40.jpg)
Terminating a Command
\c
40
![Page 41: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/41.jpg)
Selecting Rows by Content
WHERE
41
![Page 42: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/42.jpg)
SELECT * FROM employees WHERE id = 3;
Selecting Rows by Content
SELECT * FROM employees WHERE name = ‘Adam’;
SELECT * FROM employees WHERE name = ‘ADAM’;
SELECT * FROM employees WHERE name = ‘A%’;
SELECT * FROM employees WHERE binary name=‘Adam’;
SELECT * FROM employees
WHERE name=‘Adam’
AND id = 7;
42
![Page 43: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/43.jpg)
Change Existing Data
UPDATE
43
![Page 44: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/44.jpg)
Change Existing Data
UPDATE employees SET name=‘Chris’
WHERE name=‘Adam’;
UPDATE employees SET start=‘2018-05-09’
WHERE id=‘1’;
UPDATE employees SET start=‘2000-01-01’
WHERE start IS null;
44
![Page 45: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/45.jpg)
Removing Data
DELETE
45
![Page 46: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/46.jpg)
Removing Data
DELETE FROM employees WHERE name=‘Adam’;
DELETE FROM employees WHERE start=end;
DELETE FROM employees;
46
![Page 47: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/47.jpg)
Executing SQL Files
SOURCE
47
![Page 48: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/48.jpg)
Running SQL Files
$ mysql < instructions.sql
$ mysql --verbose < instructions.sql
$ mysql -e < “select * from table”
mysql> source instructions.sql
48
![Page 49: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/49.jpg)
Importing Data
LOAD DATA
49
![Page 50: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/50.jpg)
Load Data
LOAD DATA LOCAL INFILE ‘file’ INTO TABLE table
50
![Page 51: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/51.jpg)
Load Data
LOAD DATA LOCAL INFILE ‘file’ INTO TABLE tableLOAD DATA LOCAL INFILE ‘file’ INTO TABLE table
fields
terminated by ‘\t’
enclosed by ‘’
escaped by ‘\\’
lines
terminated by ‘\n’
starting by ‘’;
51
![Page 52: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/52.jpg)
Load Data
LOAD DATA LOCAL INFILE ‘file’ INTO TABLE table
fields
terminated by ‘\t’
enclosed by ‘’
escaped by ‘\\’
lines
terminated by ‘\n’
starting by ‘’;
LOAD DATA LOCAL INFILE ‘file’ INTO TABLE table
fields
terminated by ‘,’
52
![Page 53: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/53.jpg)
Load Data
CREATE TABLE employees(
first_name VARCHAR(64),
last_name VARCHAR(64),
id int AUTO_INCREMENT,
start DATE,
finish DATE,
PRIMARY KEY(id)
)
LOAD DATA LOCAL INFILE 'employeeData.tab’
INTO TABLE employees
(first_name, last_name, @ignore, start, finish);
53
![Page 54: Introduction to SQL on GRAHAM - SHARCNET](https://reader031.vdocument.in/reader031/viewer/2022012410/616a5aad11a7b741a3519570/html5/thumbnails/54.jpg)
Save Data
mysql -ss -e "select * from employees" > data.tab
54