Difference between revisions of "SQL"

From Bitnami MediaWiki
Jump to navigation Jump to search
 
(11 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;  
−
CREATE DATABASE test
+
CREATE DATABASE test;
−
CHARACTER SET latin1  
+
CHARACTER SET latin1;
 
COLLATE latin1_bin;  
 
COLLATE latin1_bin;  
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
===Cheatsheet===
 
{| class="wikitable"  
 
{| class="wikitable"  
 
|-
 
|-
Line 78: Line 94:
 
| reset root passwd
 
| reset root passwd
 
| ALTER USER 'root'@'localhost IDENTIFIED BY 'newPassword';  
 
| 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===
 +
; 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 <code>CREATE TEMPORARY TABLE</code> statement. Disappears either post-transaction or at session end
 +
* View (virtual table): a query stored in the data dictionary. <code>CREATE VIEW view_name AS...</code>. No data stored, but can be queried against
 +
* Table Links: joined queries
 +
 +
;Taxonomy of SQL
 +
* DDL (data definition): <code>CREATE, ALTER, DROP, RENAME, TRUNCATE, COMMENT</code>
 +
* DQL (data query): <code>SELECT</code>
 +
* DML (data manipulation): <code>INSERT, UPDATE, DELETE, MERGE, CALL, EXPLAIN, LOCK TABLE </code>
 +
* DCL (data control): <code> GRANT, REVOKE </code>
 +
* TCL (transaction control): <code>COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION</code>
 +
 +
===Joins===
 +
* 3 different syntaxes
 +
; O.G.
 +
<syntaxhighlight lang='SQL'>SELECT c.fname, c.lname, a.address
 +
FROM customer c, address a
 +
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>

Latest revision as of 23:35, 10 March 2022

Resources[edit]


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 TABLE statement. 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