SQL
Tables names:
BOOLEAN
is TINYINT
. See MySQL :: MySQL 5.7 Reference Manual :: 11.1.1 Numeric Type Overview
MySQL unique keys can be
NULL
whereas primary keys cannot be There can be only one Primary key in a table but one or more unique keys
Actions when a table is modified:
Syntax
Race condition
Aka concurrency
Use single atomic statement (like INNER JOIN
) or transaction
Game Leaderboard
For performances, you can create temporary/virtual table CREATE TEMPORARY TABLE IF NOT EXISTS table2 AS (SELECT * FROM table1)
. It's unique per session (this is not supported by non CGI PHP). Else use a regular table. See also views
game end timestamp
player's ID
player's ip (see Get client IP)
player's email
player's mobile
game over reason
game's round
game beginning timestamp
game score
game's round
game's score
game's round
leaderboards are read-heavy. Favor memory to store fields used to compute rank
Row number
Insert new lines
Insert in multiple tables, use transaction to rollbak in case of error (conflict, database connection lost, etc.)
Import file:
Column size
Facebook user ID (numeric string):
VARCHAR(128)
IPV6 and IPV4:
VARCHAR(50)
(canonical format: as text) orVARBINARY(16)
(binary, supported with functions by MySQL 5.6)sex (ISO/IEC 5218):
TINYINT
0 = Not Known
1 = Male
2 = Female
9 = Not applicable
Variables
MySQL :: MySQL 5.7 Reference Manual :: 1.8.2.1 SELECT INTO TABLE Differences (MySQL: "You can use SELECT ... INTO with user-defined variables")
Prepare statement
Aka SQL injection protection, supported natively by the server.
Doesn't support column name nor value. For latest, user variable can be used (eg. SET @var1 = 1, @var2 = 2
), see also stored procedure - Set variable with dynamic name in trigger (MySQL) - Database Administrator Stack Exchange Often doesn't support multiple statements (prepare individualy), or use procedure
Optimisation
Indexes:
A clustered index is like the contents of a phone book. You can open the book at 'Hilditch, David' and find all the information for all of the 'Hilditch's right next to each other. Here the keys for the clustered index are (lastname, firstname).
Redundant data vs join tables (foreign keys):
Insert lot of rows:
compine multiple records to one
INSERT
instead of one record perINSERT
mysqlimport --local database data.txt
import tab-delimited text files or usemysqlimport --local --fields-optionally-enclosed-by='"' --fields-terminated-by=, --lines-terminated-by='\r\n' database data.csv
for CSV (add--ignore-lines=1
to skip a header row). Note: use value0
for auto increment fields.
Compress data
Use COMPRESS()
in VARBINARY
or BLOB
: 4-byte length + zlib data (zlib header + deflate data)
Errors
Transaction : commit or rollback
Use SQLSTATE
codes in your client, it's more standardized.
Clear a table
Truncate a table with referenced fields as foreign keys
String comparison
Aka like
Row match vs affected
match the
WHERE
clauseaffected row inserted, deleted, changed
mysql/Readme.md at master · mysqljs/mysql - Node.js MySQL client
changedRows
vsaffectedRows
Time
CAST(date AS DATETIME)
MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions -
TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01')
Enum
FIND_IN_SET
and ENUM
If number of values in set change, the col size will be recomputed
Or:
status
can contains one or more (bits flags):
1 = Prospective Customer 2 = Cancelled Service 4 = Pool Service 8 = lawn mowing 16 = window washing 32 = tree trimming 64 = house painting 128 = mobile oil change
All of window washing customers even if they are cancelled:
All of tree trimming AND pool service customers i.e. 32+4:
Concatenate list as string
JSON with SQLite
Merge data
Last updated