Advertisement
OpataJoshua

Untitled

Dec 11th, 2022
1,613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 1.85 KB | None | 0 0
  1. -- Showing all databases
  2. SHOW DATABASES;
  3.  
  4. -- creating a database
  5. CREATE DATABASE comrades;
  6.  
  7. -- switching current database
  8. USE comrades;
  9.  
  10. -- showing all tables in the current database
  11. SHOW TABLES;
  12.  
  13. -- delete/drop a DATABASE
  14. DROP DATABASE comrades_x;
  15.  
  16. -- delete/drop a TABLE
  17. DROP TABLE members_x;
  18.  
  19. -- creating a table
  20. CREATE TABLE members (
  21.     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  22.     firstName VARCHAR(255) NOT NULL,
  23.     lastName VARCHAR(255) NOT NULL,
  24.     age INT,
  25.     gender VARCHAR(255) NOT NULL,
  26.     interestedCourseId INT
  27. )
  28.  
  29. -- creating a table
  30. CREATE TABLE courses (
  31.     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  32.     name VARCHAR(255) NOT NULL,
  33.     description VARCHAR(255)
  34. );
  35.  
  36. -- creating a table
  37. CREATE TABLE sessions (
  38.     id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  39.     name VARCHAR(255) NOT NULL,
  40.     day VARCHAR(255) NOT NULL
  41. );
  42.  
  43. -- adding a new record to a table
  44. INSERT INTO members ( firstName, age, lastName, gender)
  45. VALUES ("Elvis", 35, "Feda", "male");
  46.  
  47. -- adding multiple records to a table
  48. INSERT INTO members ( firstName, lastName, age, gender)
  49. VALUES
  50.     ("Francis", "Osei", 72, "male"),
  51.     ("Henry", "Antwi", 34, "male"),
  52.     ("Emefa", "Attikpo", 44, "female"),
  53.     ("Bernice", "Yevugah", 55, "male"),
  54.     ("Frank", "Oppong", 88, "male"),
  55.     ("Manuel", "Baafi", 23, "male");
  56.    
  57. -- selecting all records from a table
  58. SELECT * FROM members;
  59.  
  60. -- selecting all records but specifying what columns to be shown
  61. SELECT id, firstName FROM members;
  62.  
  63. -- updating a record and specifying conditions
  64. UPDATE members
  65. SET gender = "female"
  66. WHERE id=5 AND gender="male";
  67.  
  68. -- inserting another record
  69. INSERT INTO members ( firstName, age, lastName, gender)
  70. VALUES ("Kofi", 35, "Ghana", "male");
  71.  
  72. -- deleting a record
  73. DELETE FROM members
  74. WHERE id=8;
  75.  
  76. -- Joining tables
  77. SELECT * FROM members
  78. LEFT JOIN courses
  79. ON members.interestedCourseId=courses.id;
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement