Advertisement
metalx1000

Basic MySQL Examples

Feb 4th, 2024 (edited)
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.86 KB | None | 0 0
  1. sudo apt install mysql-server
  2.  
  3. sudo mysql
  4.  
  5. #Create DataBase and
  6. SHOW DATABASES;
  7. CREATE DATABASE office;
  8. SHOW DATABASES;
  9. USE office;
  10. CREATE TABLE employees ( id INT NOT NULL AUTO_INCREMENT , fname char(255) NOT NULL, lname char(255) NOT NULL, address char(255) NOT NULL, age int(100), phone char(20), PRIMARY KEY (id));
  11. DESCRIBE employees;
  12.  
  13. # Enter Data
  14. INSERT INTO employees VALUES(null,'John','Smith','123 Pine St',35,'555-555-5555');
  15. SELECT * FROM employees;
  16. INSERT INTO employees VALUES(null,'Jake','Simpson','123 Oak Lane',26,'555-555-5551');
  17. INSERT INTO employees VALUES(null,'John','Peterson','564 1st Ave',45,'555-555-5531');
  18. INSERT INTO employees VALUES(null,'Sally','Smith','123 Oak Lane',29,'555-555-5871');
  19. INSERT INTO employees VALUES(null,'Jake','Ericsson','543 Oak Lane',29,NULL);
  20. INSERT INTO employees VALUES(null,'Jim','Johnson','423 Pine Lane',38,'555-555-5341');
  21. INSERT INTO employees VALUES(null,'Tom','Ericsson','543 Oak Lane',29,'555-555-2351');
  22.  
  23. # Search Data
  24. SELECT * FROM employees;
  25. SELECT * FROM employees WHERE lname = 'Smith';
  26. SELECT * FROM employees WHERE address LIKE '%Oak%';
  27. SELECT * FROM employees WHERE age > 30;
  28. SELECT * FROM employees WHERE age < 30 ORDER BY age;
  29. SELECT * FROM employees WHERE age < 30 ORDER BY age DESC;
  30. SELECT * FROM employees WHERE age < 30 ORDER BY age LIMIT 2;
  31. SELECT * FROM employees WHERE age < 30 ORDER BY age DESC LIMIT 2;
  32.  
  33. SELECT fname FROM employees WHERE lname = 'Smith';
  34. SELECT fname,lname FROM employees WHERE address LIKE '%Oak%';
  35.  
  36. #update entry
  37. SELECT * FROM employees WHERE phone is NULL;
  38. UPDATE employees SET phone="555-555-3333" WHERE id=5;
  39. SELECT * FROM employees;
  40.  
  41. # Delete Entry
  42. DELETE FROM employees WHERE id = '2';
  43. SELECT * FROM employees;
  44.  
  45. # Delete Table
  46. DROP TABLE employees;
  47. SELECT * FROM employees;
  48.  
  49. # Delete DATABASE
  50. DROP DATABASE office;
  51. SHOW DATABASES;
  52.  
  53. # EXIT
  54. quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement