Difference between revisions of "SQL"
Jump to navigation
Jump to search
| (2 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| + | ===Resources=== | ||
| + | * [https://sqlflow.gudusoft.com/#/ Query-to-diagram maker] | ||
| + | * [https://www.dpriver.com/pp/sqlformat.htm Auto-formatter] | ||
| + | * [https://towardsdatascience.com/10-best-practices-to-write-readable-and-maintainable-sql-code-427f6bb98208 Syntax suggestions] | ||
| + | * [https://learnsql.com/blog/24-rules-sql-code-formatting-standard/ More syntax suggestions] | ||
| + | * [https://www.freeformatter.com/sql-formatter.html Free online formatter] | ||
| + | * [https://extendsclass.com/sql-formatter.html Another online formatter] | ||
| + | * [https://codebeautify.org/sqlformatter And yet another...] | ||
| + | * [https://docs.telemetry.mozilla.org/concepts/sql_style.html Moziila guidance] | ||
| + | * [https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax Query syntax from Google] | ||
| + | * [https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql Google migration to SQL2011 from BigQuery] | ||
| + | * [https://docs.microsoft.com/en-us/sql/relational-databases/performance/subqueries?view=sql-server-ver15 Subquery guidance from MSFT] | ||
| + | |||
| + | |||
| + | |||
Create a new database: | Create a new database: | ||
<syntaxhighlight lang='SQL'>DROP DATABASE test; | <syntaxhighlight lang='SQL'>DROP DATABASE test; | ||
| Line 124: | Line 139: | ||
===Joins=== | ===Joins=== | ||
* 3 different syntaxes | * 3 different syntaxes | ||
| + | ; O.G. | ||
<syntaxhighlight lang='SQL'>SELECT c.fname, c.lname, a.address | <syntaxhighlight lang='SQL'>SELECT c.fname, c.lname, a.address | ||
FROM customer c, address a | FROM customer c, address a | ||
WHERE c.address_id = a.address_id | WHERE c.address_id = a.address_id | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ; Shortcut | ||
| + | <syntaxhighlight lang='SQL'>SELECT c.fname, c.lname, a.address | ||
| + | FROM customer c INNER JOIN address a | ||
| + | USING (address_id) | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | ; Proper SQL92 | ||
| + | <syntaxhighlight lang='SQL'>SELECT c.fname, c.lname, a.address | ||
| + | FROM customer c INNER JOIN address a | ||
| + | ON c.address_id = a.address_id | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 23:35, 10 March 2022
Resources[edit]
- Query-to-diagram maker
- Auto-formatter
- Syntax suggestions
- More syntax suggestions
- Free online formatter
- Another online formatter
- And yet another...
- Moziila guidance
- Query syntax from Google
- Google migration to SQL2011 from BigQuery
- Subquery guidance from MSFT
Create a new database:
DROP DATABASE test;
CREATE DATABASE test;
CHARACTER SET latin1;
COLLATE latin1_bin;
Cheatsheet[edit]
| Action | Command | Notes |
|---|---|---|
| select a db | USE database | no semicolon needed |
| primary key column | blank_id INT AUTO_INCREMENT PRIMARY KEY | |
| display table attributes | DESCRIBE mytable; | |
| add data to a table | INSERT INTO table (field1) VALUES ('value1'); | |
| create a db | CREATE DATABASE mydatabase; | |
| create a table | CREATE TABLE db.table (col1, col2, col3, ...); | |
| show how a table was made | SHOW CREATE TABLE table \G | |
| backup a table (not a db) | mysqldump -u user -p dbname table > tbl-bak.sql | |
| backup a full db | mysqldump -u user -p dbname > dbname-bak.sql | |
| restore a full db | mysql -u user -p dbname < dbname-bak.sql | |
| copy table's form to new db | CREATE TABLE dbnew.table LIKE table; | |
| populate new table | INSERT INTO newtable SELECT * FROM oldtable; | |
| add column | ALTER TABLE table ADD COLUMN col_id INT AFTER col2; | |
| drop column | ALTER TABLE table DROP COLUMN column; | |
| copy table | CREATE TABLE new_tbl SELECT * FROM old_tbl | |
| delete all rows | TRUNCATE TABLE tbl; | |
| reset root passwd | ALTER USER 'root'@'localhost IDENTIFIED BY 'newPassword'; | |
| change column name | ALTER TABLE tbl RENAME COLUMN old_col TO new_col; | |
| change column type | ALTER TABLE tbl MODIFY name CHAR(20) DEFAULT 'N/A'; | |
| add a column | ALTER TABLE tbl ADD new_col CHAR(20); | |
| delete a row | DELETE FROM tbl WHERE thing_id = 42; |
Concepts[edit]
- ACID
- Atomic
- Consistent
- Isolated
- Durable
- CAP Theorem
- Only two of consistency, availability and partition tolerance can be guaranteed simultaneously
- Table Types
- Subquery-generated: enclosed in parentheses; temporary to the query
- Temporary tables: generated by a
CREATE TEMPORARY TABLEstatement. Disappears either post-transaction or at session end - View (virtual table): a query stored in the data dictionary.
CREATE VIEW view_name AS.... No data stored, but can be queried against - Table Links: joined queries
- Taxonomy of SQL
- DDL (data definition):
CREATE, ALTER, DROP, RENAME, TRUNCATE, COMMENT - DQL (data query):
SELECT - DML (data manipulation):
INSERT, UPDATE, DELETE, MERGE, CALL, EXPLAIN, LOCK TABLE - DCL (data control):
GRANT, REVOKE - TCL (transaction control):
COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION
Joins[edit]
- 3 different syntaxes
- O.G.
SELECT c.fname, c.lname, a.address
FROM customer c, address a
WHERE c.address_id = a.address_id
- Shortcut
SELECT c.fname, c.lname, a.address
FROM customer c INNER JOIN address a
USING (address_id)
- Proper SQL92
SELECT c.fname, c.lname, a.address
FROM customer c INNER JOIN address a
ON c.address_id = a.address_id