SQL
Last updated
Was this helpful?
Last updated
Was this helpful?
Was this helpful?
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:
Aka concurrency
Use single atomic statement (like INNER JOIN
) or transaction
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
-- insert game
INSERT INTO games (time, player, ip, email, mobile, over_reason, round, beginning, score)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
-- get rank
SELECT (SELECT 1 + COUNT(*) FROM games WHERE round = ? AND score > ?) as rank;
-- get top 10
SELECT player, score FROM games
WHERE round = ?
ORDER BY score DESC
LIMIT 10;
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
INSERT INTO test VALUES(replace('a\nb\nc\nd\ne', '\n', char(10)));
Insert in multiple tables, use transaction to rollbak in case of error (conflict, database connection lost, etc.)
BEGIN;
INSERT INTO users (username, password)
VALUES('test', 'test');
INSERT INTO profiles (userid, bio, homepage)
VALUES(LAST_INSERT_ID(),'Hello world!', 'http://www.stackoverflow.com');
COMMIT;
INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;
INSERT INTO table (id, name, age) VALUES(1, "A", 19) ON DUPLICATE KEY UPDATE name="A", age=19
Import file:
Facebook user ID (numeric string): VARCHAR(128)
IPV6 and IPV4: VARCHAR(50)
(canonical format: as text) or VARBINARY(16)
(binary, supported with functions by MySQL 5.6)
sex (ISO/IEC 5218): TINYINT
0 = Not Known
1 = Male
SET @myvar = 10
SELECT @myvar := 10
UPDATE mytable SET col = @tmp := col, col = 10
MySQL :: MySQL 5.7 Reference Manual :: 1.8.2.1 SELECT INTO TABLE Differences (MySQL: "You can use with user-defined variables")
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
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 per INSERT
mysqlimport --local database data.txt
import tab-delimited text files or use mysqlimport --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 value 0
for auto increment fields.
./mysqltuner.pl --user root --pass root --defaults-file .../my.cnf --mysqladmin .../mysqladmin --mysqlcmd .../mysql
./mysqltuner.pl --user root --pass root --defaults-file /Applications/MAMP/conf/my.cnf --mysqladmin /Applications/MAMP/Library/bin/mysqladmin --mysqlcmd /Applications/MAMP/Library/bin/mysql
Use COMPRESS()
in VARBINARY
or BLOB
: 4-byte length + zlib data (zlib header + deflate data)
Transaction : commit or rollback
Use SQLSTATE
codes in your client, it's more standardized.
# Kick, internaly delete and recreate the table
TRUNCATE TABLE table;
# Delete each rows, but doesn't reset auto increment
DELETE FROM table;
Truncate a table with referenced fields as foreign keys
DELETE FROM table;
-- will execute cascades
ALTER TABLE table AUTO_INCREMENT = 1;
Aka like
-- select URL contains url encoded space
SELECT 'http://example/David%20and%20Goliath' LIKE '\%20'
match the WHERE
clause
affected row inserted, deleted, changed
- Node.js MySQL clientchangedRows
vs affectedRows
CAST(date AS DATETIME)
MySQL :: MySQL 5.5 Reference Manual :: 12.7 Date and Time Functions - TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01')
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:
SELECT * FROM customers WHERE (status & 16) AND !(status & 2)
All of tree trimming AND pool service customers i.e. 32+4:
SELECT * FROM customers WHERE status & 36
SET SESSION group_concat_max_len = 1000000;#max 1MB
SELECT GROUP_CONCAT(k SEPARATOR ',') FROM (SELECT CONCAT(col1, ':', col2) AS k FROM sometable WHERE condition) AS tmp
9 = Not applicable