Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. # SQL COURSE
  2. ## What is a databse?
  3. - A collection of data
  4. - A structured set of computeerized data with an accessible interface
  5.  
  6. ### SQL vs. MySQL
  7. - SQL is the language used to talk to databases
  8. - Relational databases use SQL
  9. - Examples of relational database management systems: MySQL, SQLite, PostgreSQL
  10.  
  11. ### Entering MySQL in terminal
  12. - `mysql -u root -p`
  13.  
  14. ### Database creation/removal:
  15. `CREATE DATABASE <name>;` Creates a new databse
  16. `DROP DATABASE <name>;` Deletes a database
  17. `USE DATABASE <name>;` Selects a databse for use (where you'ree working)
  18. `SELECT database()` Shows which database we are using
  19.  
  20. ### Table Creation
  21. Creating a table:
  22. ```SQL
  23. CREATE TABLE tablename
  24. (
  25. name VARCHAR(100),
  26. age INT
  27. );
  28. ```
  29. Creating a table with a default value:
  30.  
  31. ```SQL
  32. CREATE TABLE tablename
  33. (
  34. name VARCHAR(100) DEFAULT 'unnamed',
  35. age INT DEFAULT 999
  36. );
  37. ```
  38. Creating a table without allowing for null:
  39.  
  40. ```SQL
  41. CREATE TABLE tablename
  42. (
  43. name VARCHAR(100) NOT NULL DEFAULT 'unnamed',
  44. age INT NOT NULL DEFAULT 999
  45. );
  46. ```
  47. Adding in AUTO_INCREMENT:
  48. ```SQL
  49. CREATE TABLE unique_cats2 (
  50. cat_id INT NOT NULL AUTO_INCREMENT,
  51. name VARCHAR(100),
  52. age INT,
  53. PRIMARY KEY (cat_id)
  54. );
  55. ```
  56. ### View Tables
  57. `SHOW TABLES`
  58. `DESC <tablename>`
  59. `SHOW COLUMNS FROM <tablename>`
  60.  
  61. ## INSERT
  62. ```SQL
  63. INSERT INTO table_name
  64. (column_name, column_name)
  65. VALUES(value, value),
  66. (value, value),
  67. (value, value);
  68. ```
  69. ## SQL QUERIES
  70.  
  71. ### SELECT *
  72.  
  73. Basic query:
  74. ```SQL
  75. SELECT *
  76. FROM customers;
  77. ```
  78.  
  79. Query with where:
  80. ```SQL
  81. SELECT *
  82. FROM customers
  83. WHERE customerID = 5;
  84. ```
  85.  
  86. Query with return modifier to return (ASC = ascending DESC = Descending):
  87. ```SQL
  88. SELECT *
  89. FROM customers
  90. ORDER BY customerID DESC;
  91. ```
  92.  
  93. ### Aliases
  94. ```SQL
  95. SELECT cat_id AS id, name FROM cats;
  96. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement