Guest User

Untitled

a guest
Feb 8th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.21 KB | None | 0 0
  1. 1. Log into mysql:
  2. mysql -u [username] -p;(will prompt for password)
  3.  
  4. data types (INTEGER,FLOAT, DECIMAL(i, j), CHAR(n), VARCHAR(n))
  5. DATE:
  6. • Made up of year-month-day in the format yyyy-mm-dd
  7. TIME:
  8. • Made up of hour:minute:second in the format hh:mm:ss
  9. TIME(i):
  10. • Made up of hour:minute:second plus i additional digits specifying fractions of a second
  11. • format is hh:mm:ss:ii...i
  12. TIMESTAMP:
  13. • Has both DATE and TIME components
  14.  
  15. 2. Show all databases:
  16. SHOW DATABASES;
  17.  
  18. 3. Access database:
  19. mysql -u [username] -p [database](will prompt for password)
  20.  
  21. 4. Create new database:
  22. CREATE DATABASE [database_name];
  23. An error occurs if database exists, Check if the database is existing
  24. DROP DATABASE IF EXISTS [database_name];
  25. CREATE DATABASE [database_name];
  26.  
  27. CREATE DATABASE IF NOT EXISTS Department;
  28.  
  29. 5. Select database:
  30. USE [database_name];
  31.  
  32. 6. Determine what database is in use:
  33. SELECT DATABASE();
  34.  
  35. 7. Show all tables:
  36. SHOW TABLES;
  37.  
  38. 8. Show table structure:
  39. DESCRIBE [table_name];
  40.  
  41. 9. List all indexes on a table:
  42. show index from [table];
  43.  
  44. 10. Create new table with columns:
  45. CREATE TABLE DEPARTMENT
  46. ( DNAME VARCHAR(10) NOT NULL,
  47. DNUMBER INTEGER NOT NULL,
  48. MGRSSN CHAR(9),
  49. MGRSTARTDATE CHAR(9) );
  50.  
  51. 11. Adding a column:
  52. ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);
  53. The new attribute will have NULLs in all the tuples of the relation right after the command is executed; hence, the
  54. NOT NULL constraint is not allowed for such an attribute
  55.  
  56. 12. Adding a column with an unique, auto-incrementing ID:
  57. ALTER TABLE [table] ADD COLUMN [column] int NOT NULL AUTO_INCREMENT PRIMARY KEY;
  58.  
  59. 13. Inserting a record:
  60. (Insert a tuple for a new EMPLOYEE for whom we only know the FNAME, LNAME, and SSN attributes)
  61. INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
  62. VALUES ('Richard', 'Marini', '653298653')
  63.  
  64. (Attribute values should be listed in the same order)
  65. INSERT INTO EMPLOYEE
  66. VALUES ('Richard','K','Marini', '653298653','30-DEC-92', '98 Oak Forest, Katy,TX', 'M',37000,'987654321', 4);
  67.  
  68. 14. MySQL function for datetime input:
  69. NOW()
  70.  
  71. 15. Selecting records:
  72. SELECT * FROM [table_name];
  73.  
  74. SELECT <attribute list>
  75. FROM <table list>
  76. WHERE <condition>
  77.  
  78. SELECT BDATE, ADDRESS
  79. FROM EMPLOYEE
  80. WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith'
  81.  
  82. 16. Explain records:
  83. EXPLAIN SELECT * FROM [table];
  84.  
  85. 17. Selecting parts of records:
  86. SELECT [column], [another-column] FROM [table];
  87.  
  88. 18. Counting records:
  89. SELECT COUNT([column]) FROM [table];
  90.  
  91. 19. Counting and selecting grouped records:
  92. SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];
  93.  
  94. 20. Selecting specific records:
  95. SELECT * FROM [table] WHERE [column] = [value]; (Selectors: <, >, !=; combine multiple selectors with AND, OR)
  96.  
  97. 21. Select records containing [value]:
  98. SELECT * FROM [table] WHERE [column] LIKE '%[value]%';
  99.  
  100. 22. Select records starting with [value]:
  101. SELECT * FROM [table] WHERE [column] LIKE '[value]%';
  102.  
  103. 23. Select records starting with val and ending with ue:
  104. SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';
  105.  
  106. 24. Select a range:
  107. SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];
  108.  
  109. 25. Select with custom order and only limit:
  110. SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value]; (Order: DESC, ASC)
  111.  
  112. 26. Updating records:
  113. UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];
  114.  
  115. UPDATE PROJECT
  116. SET PLOCATION = ‘Galle', DNUM = 5
  117. WHERE PNUMBER=10
  118.  
  119. 27. Deleting records:
  120. DELETE FROM [table] WHERE [column] = [value];
  121.  
  122. 28. Delete all records from a table (without dropping the table itself):
  123. DELETE FROM [table]; (This also resets the incrementing counter for auto generated columns like an id column.)
  124.  
  125. 29. Delete all records without deleting schema in a table:
  126. truncate table [table];
  127.  
  128. 30. Removing table columns:
  129. ALTER TABLE [table] DROP COLUMN [column];
  130.  
  131. 31. Deleting tables:
  132. DROP TABLE [table];
  133.  
  134. 32. Deleting databases:
  135. DROP DATABASE [database];
  136.  
  137. 33. Custom column output names:
  138. SELECT [column] AS [custom-column] FROM [table];
  139.  
  140. 34. Export a database dump :
  141. mysqldump -u [username] -p [database] > db_backup.sql
  142.  
  143. 35. Use --lock-tables=false option for locked tables.
  144.  
  145. 36. Import a database dump:
  146. mysql -u [username] -p -h localhost [database] < db_backup.sql
  147.  
  148. 37. Logout: exit;
  149.  
  150. 38. To eliminate duplicate tuples in a query result
  151. SELECT DISTINCT SALARY
  152. FROM EMPLOYEE
  153. --------------------------------------------------------------------------------------------------
  154. Aggregate functions
  155.  
  156. 1. Select but without duplicates:
  157. SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00
  158.  
  159. 2. Calculate total number of records:
  160. SELECT SUM([column]) FROM [table];
  161.  
  162. 3. Count total number of [column] and group by [category-column]:
  163. SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];
  164.  
  165. 4. Get largest value in [column]:
  166. SELECT MAX([column]) FROM [table];
  167.  
  168. 5. Get smallest value:
  169. SELECT MIN([column]) FROM [table];
  170.  
  171. 6. Get average value:
  172. SELECT AVG([column]) FROM [table];
  173.  
  174. 7. Get rounded average value and group by [category-column]:
  175. SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];
  176.  
  177. -----------------------------------------------------------------------------------------------
  178. Users functions
  179.  
  180. 1. List all users:
  181. SELECT User,Host FROM mysql.user;
  182.  
  183. 2. Create new user:
  184. CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
  185.  
  186. 3. Grant ALL access to user for * tables:
  187. GRANT ALL ON database.* TO 'user'@'localhost';
  188.  
  189. -----------------------------------------------------------------------------------------------
  190. MySQL Mathematical Functions
  191.  
  192. 1. Count rows per group COUNT(column | *)
  193.  
  194. 2. Average value of group AVG(column)
  195.  
  196. 3. Minumum value of group MIN(column)
  197.  
  198. 4. Maximum value of group MAX(column)
  199.  
  200. 5. Sum values in a group SUM(column)
  201.  
  202. 6. Absolute value abs(number)
  203.  
  204. 7. Rounding numbers round(number)
  205.  
  206. 8. Largest integer not greater floor(number)
  207.  
  208. 9. Smallest integer not smaller ceiling(number)
  209.  
  210. 10. Square root sqrt(number)
  211.  
  212. 11. nth power pow(base,exponent)
  213.  
  214. 12. random number n, 0<n < 1 rand()
  215.  
  216. 13. sin (similar cos, etc.) sin(number)
  217.  
  218. ------------------------------------------------------------------------------------
  219. MySQL String Functions
  220.  
  221. 1. Compare strings strcmp(string1,string2)
  222.  
  223. 2. Convert to lower case lower(string)
  224.  
  225. 3. Convert to upper case upper(string)
  226.  
  227. 4. Left-trim whitespace (similar right) ltrim(string)
  228.  
  229. 5. Substring of string substring(string,index1,index2)
  230.  
  231. 6. Encrypt password password(string)
  232.  
  233. 7. Encode string encode(string,key)
  234.  
  235. 8. Decode string decode(string,key)
  236.  
  237. 9. Get date curdate()
  238.  
  239. 10. Get time curtime()
  240.  
  241. 11. Extract day name from date string dayname(string)
  242.  
  243. 12. Extract day number from date string dayofweek(string)
  244.  
  245. 13.Extract month from date string monthname(string)
  246.  
  247. ----------------------------------------------------------------------------------------
  248.  
  249. #reset root password
  250.  
  251. sudo /etc/init.d/mysql stop
  252. sudo mysqld_safe --skip-grant-tables &
  253. mysql -u root
  254. mysql> use mysql;
  255. mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
  256. mysql> flush privileges;
  257. mysql> quit
  258.  
  259. sudo /etc/init.d/mysql stop
  260. sudo /etc/init.d/mysql start
Add Comment
Please, Sign In to add comment