Advertisement
LshySVK

SQL Cheat sheet

Jun 19th, 2024 (edited)
1,481
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.96 KB | None | 1 0
  1. # MySQL Cheat Sheet
  2. https://gist.github.com/bradtraversy/c831baaad44343cc945e76c2e30927b3
  3.  
  4. > Help with SQL commands to interact with a MySQL database
  5.  
  6. ## MySQL Locations
  7. * Mac             */usr/local/mysql/bin*
  8. * Windows         */Program Files/MySQL/MySQL _version_/bin*
  9. * Xampp           */xampp/mysql/bin*
  10.  
  11. ## Add mysql to your PATH
  12. # Current Session
  13. export PATH=${PATH}:/usr/local/mysql/bin
  14. # Permanantly
  15. echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> ~/.bash_profile
  16.  
  17. On Windows - https://www.qualitestgroup.com/resources/knowledge-center/how-to-guide/add-mysql-path-windows/
  18.  
  19. ## Login
  20. mysql -u root -p
  21.  
  22. ## Show Users
  23. SELECT User, Host FROM mysql.user;
  24.  
  25. ## Create User
  26. CREATE USER 'someuser'@'localhost' IDENTIFIED BY 'somepassword';
  27.  
  28. ## Grant All Priveleges On All Databases
  29. GRANT ALL PRIVILEGES ON * . * TO 'someuser'@'localhost';
  30. FLUSH PRIVILEGES;
  31.  
  32. ## Show Grants
  33. SHOW GRANTS FOR 'someuser'@'localhost';
  34.  
  35. ## Remove Grants
  36. REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'someuser'@'localhost';
  37.  
  38. ## Delete User
  39. DROP USER 'someuser'@'localhost';
  40.  
  41. ## Exit
  42. exit;
  43.  
  44. ## Show Databases
  45. SHOW DATABASES
  46.  
  47. ## Create Database
  48. CREATE DATABASE acme;
  49.  
  50. ## Delete Database
  51. DROP DATABASE acme;
  52.  
  53. ## Select Database
  54. USE acme;
  55.  
  56. ## Create Table
  57. CREATE TABLE users(
  58. id INT AUTO_INCREMENT,
  59.    first_name VARCHAR(100),
  60.    last_name VARCHAR(100),
  61.    email VARCHAR(50),
  62.    password VARCHAR(20),
  63.    location VARCHAR(100),
  64.    dept VARCHAR(100),
  65.    is_admin TINYINT(1),
  66.    register_date DATETIME,
  67.    PRIMARY KEY(id)
  68. );
  69.  
  70. ## Delete / Drop Table
  71. DROP TABLE tablename;
  72.  
  73. ## Show Tables
  74. SHOW TABLES;
  75.  
  76. ## Insert Row / Record
  77. INSERT INTO users (first_name, last_name, email, password, location, dept, is_admin, register_date) values ('Brad', 'Traversy', 'brad@gmail.com', '123456','Massachusetts', 'development', 1, now());
  78.  
  79. ## Insert Multiple Rows
  80. INSERT INTO users (first_name, last_name, email, password, location, dept,  is_admin, register_date) values ('Fred', 'Smith', 'fred@gmail.com', '123456', 'New York', 'design', 0, now()), ('Sara', 'Watson', 'sara@gmail.com', '123456', 'New York', 'design', 0, now()),('Will', 'Jackson', 'will@yahoo.com', '123456', 'Rhode Island', 'development', 1, now()),('Paula', 'Johnson', 'paula@yahoo.com', '123456', 'Massachusetts', 'sales', 0, now()),('Tom', 'Spears', 'tom@yahoo.com', '123456', 'Massachusetts', 'sales', 0, now());
  81.  
  82. ## Select
  83. SELECT * FROM users;
  84. SELECT first_name, last_name FROM users;
  85.  
  86. ## Where Clause
  87. SELECT * FROM users WHERE location='Massachusetts';
  88. SELECT * FROM users WHERE location='Massachusetts' AND dept='sales';
  89. SELECT * FROM users WHERE is_admin = 1;
  90. SELECT * FROM users WHERE is_admin > 0;
  91.  
  92. ## Delete Row
  93. DELETE FROM users WHERE id = 6;
  94.  
  95. ## Update Row
  96. UPDATE users SET email = 'freddy@gmail.com' WHERE id = 2;
  97.  
  98. ## Add New Column
  99. ALTER TABLE users ADD age VARCHAR(3);
  100.  
  101. ## Modify Column
  102. ALTER TABLE users MODIFY COLUMN age INT(3);
  103.  
  104. ## Order By (Sort)
  105. SELECT * FROM users ORDER BY last_name ASC;
  106. SELECT * FROM users ORDER BY last_name DESC;
  107.  
  108. ## Concatenate Columns
  109. SELECT CONCAT(first_name, ' ', last_name) AS 'Name', dept FROM users;
  110.  
  111. ## Select Distinct Rows
  112. SELECT DISTINCT location FROM users;
  113.  
  114. ## Between (Select Range)
  115. SELECT * FROM users WHERE age BETWEEN 20 AND 25;
  116.  
  117. ## Like (Searching)
  118. SELECT * FROM users WHERE dept LIKE 'd%';
  119. SELECT * FROM users WHERE dept LIKE 'dev%';
  120. SELECT * FROM users WHERE dept LIKE '%t';
  121. SELECT * FROM users WHERE dept LIKE '%e%';
  122.  
  123. ## Not Like
  124. SELECT * FROM users WHERE dept NOT LIKE 'd%';
  125.  
  126. ## IN
  127. SELECT * FROM users WHERE dept IN ('design', 'sales');
  128.  
  129. ## Create & Remove Index
  130. CREATE INDEX LIndex On users(location);
  131. DROP INDEX LIndex ON users;
  132.  
  133. ## New Table With Foreign Key (Posts)
  134. CREATE TABLE posts(
  135. id INT AUTO_INCREMENT,
  136.    user_id INT,
  137.    title VARCHAR(100),
  138.    body TEXT,
  139.    publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
  140.    PRIMARY KEY(id),
  141.    FOREIGN KEY (user_id) REFERENCES users(id)
  142. );
  143.  
  144. ## Add Data to Posts Table
  145. INSERT INTO posts(user_id, title, body) VALUES (1, 'Post One', 'This is post one'),(3, 'Post Two', 'This is post two'),(1, 'Post Three', 'This is post three'),(2, 'Post Four', 'This is post four'),(5, 'Post Five', 'This is post five'),(4, 'Post Six', 'This is post six'),(2, 'Post Seven', 'This is post seven'),(1, 'Post Eight', 'This is post eight'),(3, 'Post Nine', 'This is post none'),(4, 'Post Ten', 'This is post ten');
  146.  
  147. ## INNER JOIN
  148. SELECT
  149.   users.first_name,
  150.   users.last_name,
  151.   posts.title,
  152.   posts.publish_date
  153. FROM users
  154. INNER JOIN posts
  155. ON users.id = posts.user_id
  156. ORDER BY posts.title;
  157.  
  158. ## New Table With 2 Foriegn Keys
  159. CREATE TABLE comments(
  160.     id INT AUTO_INCREMENT,
  161.     post_id INT,
  162.     user_id INT,
  163.     body TEXT,
  164.     publish_date DATETIME DEFAULT CURRENT_TIMESTAMP,
  165.     PRIMARY KEY(id),
  166.     FOREIGN KEY(user_id) references users(id),
  167.     FOREIGN KEY(post_id) references posts(id)
  168. );
  169.  
  170. ## Add Data to Comments Table
  171. INSERT INTO comments(post_id, user_id, body) VALUES (1, 3, 'This is comment one'),(2, 1, 'This is comment two'),(5, 3, 'This is comment three'),(2, 4, 'This is comment four'),(1, 2, 'This is comment five'),(3, 1, 'This is comment six'),(3, 2, 'This is comment six'),(5, 4, 'This is comment seven'),(2, 3, 'This is comment seven');
  172.  
  173. ## Left Join
  174. SELECT
  175. comments.body,
  176. posts.title
  177. FROM comments
  178. LEFT JOIN posts ON posts.id = comments.post_id
  179. ORDER BY posts.title;
  180.  
  181. ## Join Multiple Tables
  182. SELECT
  183. comments.body,
  184. posts.title,
  185. users.first_name,
  186. users.last_name
  187. FROM comments
  188. INNER JOIN posts on posts.id = comments.post_id
  189. INNER JOIN users on users.id = comments.user_id
  190. ORDER BY posts.title;
  191.  
  192. ## Aggregate Functions
  193. SELECT COUNT(id) FROM users;
  194. SELECT MAX(age) FROM users;
  195. SELECT MIN(age) FROM users;
  196. SELECT SUM(age) FROM users;
  197. SELECT UCASE(first_name), LCASE(last_name) FROM users;
  198.  
  199. ## Group By
  200. SELECT age, COUNT(age) FROM users GROUP BY age;
  201. SELECT age, COUNT(age) FROM users WHERE age > 20 GROUP BY age;
  202. SELECT age, COUNT(age) FROM users GROUP BY age HAVING count(age) >=2;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement