4 SQL

4.1 General

https://www.w3schools.com/sql/default.asp ‘Getting started with SQL’ using SQLite. Download database found in link by vlicking the ZIP button and copy the contents to a folder of choice. The navigate in the top menu to Database -> add a database. https://github.com/thomasnield/oreilly_getting_started_with_sql

4.2 Setting up a database

Always strive to gave a primary key on every table, which provides a unique identify to each record. Foreign key in a child table points to the primary key in its parent table. A database schema is a diagram showing tables, their columns, and their relationships.

4.3 Useful expressions

Operator Description Example
abs() Calculates the absolute value abs(x)
coalesce() Converts a possible null value into a default coalesce(z,y)
instr() Checks if a text string contains another text string instr(‘HTX’, ‘TX’)
length() Provides the number of characters in a string length(‘Test’)
trim() Removes extraneous on both sides of a string trim(’ Test ’)
ltrim() Removes extraneous on the left side of a string ltrim(’ Test’)
rtrim() Removes extraneous on the right side of a string rtrim(‘Test’)
random() Returns a random number random()
round() Rounds a decimal to specified number round(100.101, 2)
replace() Replaces a substring of text in a string replace(‘Test df’, ’ df’, ’’)
substr() Extracts a range of characters from a string with their num position substr(‘Test’, 2,3)
lower() Turns all letters in a string to lowercase lower(‘Test’)
upper() Turns all letters in a string to uppercase upper(‘Test’)

4.4 SQL databases and R