metalx1000

Basic MySQL Shell commands

Mar 2nd, 2016
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.17 KB | None | 0 0
  1. #Mysql Shell notes
  2.  
  3. #restart service if needed
  4. sudo /etc/init.d/mysql restart
  5.  
  6. #login with password prompt
  7. mysql -u root -p
  8.  
  9. #list databases
  10. show databases;
  11.  
  12. #create database
  13. CREATE DATABASE MyDatabase;
  14.  
  15. #choose database to use
  16. USE MyDatabase;
  17.  
  18. #list tables
  19. show tables;
  20.  
  21. #Create a table with auto increments and some fields
  22. CREATE TABLE MyTable ( id INT NOT NULL AUTO_INCREMENT , name TEXT NOT NULL , address TEXT NOT NULL , phone TEXT NOT NULL , PRIMARY KEY (id)) ENGINE = InnoDB;
  23.  
  24. #enter data into table
  25. INSERT INTO MyTable (id, name, address, phone) VALUES (NULL, 'John Smith', '123 Elm Street', '(555)555-5555');
  26.  
  27. #show entire table
  28. select * from MyTable;
  29.  
  30. #delete row
  31. DELETE FROM MyTable WHERE id = '2';
  32.  
  33. #query (Not case sensitive)
  34. SELECT * FROM MyTable WHERE name = 'betty friend';
  35. SELECT * FROM MyTable WHERE name LIKE 'betty%';
  36.  
  37. #modify row
  38. UPDATE MyTable SET address="124 Elm Street", phone="(555)555-3333" WHERE id=4;
  39.  
  40. #Backup and restore Database
  41. mysqldump -u root -p --all-databases > dump.sql
  42. mysql -u root -p <dump.sql
  43.  
  44. #or by database
  45. mysqldump -u root -p MyDatabase > dumpfilename.sql
  46. mysql -u root -p MyDatabase < dumpfilename.sql
Add Comment
Please, Sign In to add comment