1. -- DDL
    2. USE db1;
    3. SHOW TABLES;
    4. # 添加字段
    5. ALTER TABLE students ADD brithday DATETIME DEFAULT "2020-07-21";
    6. ALTER TABLE students ADD id_2 INT;
    7. # 修改字段
    8. ALTER TABLE students MODIFY brithday DATE DEFAULT "2020-07-21";
    9. # 修改字段名
    10. ALTER TABLE students CHANGE brithday birth DATE DEFAULT "2020-07-21";
    11. # 删除字段
    12. ALTER TABLE students DROP id_2;
    13. -- DML 表数据操作
    14. # 添加整行数据
    15. INSERT INTO students VALUE(10, "lily", 20, "2000-1-1");
    16. # 同时添加多行数据
    17. INSERT INTO students(NAME, age, birth, gender) VALUES("lucy", 21, "1999-01-01", "male"),
    18. ("jerry", 25, "1995-01-01", "male"),
    19. ("ermao", 10, "2010-01-01", "female");
    20. # 添加name,gender两个字段的数据
    21. ALTER TABLE students ADD gender ENUM("male","female");
    22. # 将姓名为jacky全部修改为jack
    23. UPDATE students SET NAME="jack" WHERE NAME="jacky";
    24. # 将性别为女的名字修改为rose
    25. UPDATE students SET NAME="rose" WHERE gender="female";
    26. # 将姓名为jack的数据 物理删除
    27. DELETE FROM students WHERE NAME="jack";
    28. -- 查询语句
    29. # 查询Student表所有数据
    30. SELECT * FROM students;
    31. # 询Student表中name与gender字段的数据
    32. SELECT NAME, gender FROM students;
    33. # 查询Student表中name字段的数据并且去重
    34. SELECT DISTINCT NAME FROM students;
    35. -- 比较运算: = > >= < <= <> !=
    36. # 查询id大于11的数据
    37. SELECT * FROM students WHERE id>11;
    38. # 查询年龄大于18岁的信息
    39. SELECT * FROM students WHERE age>18;
    40. # 查询姓名不是lily的数据
    41. SELECT * FROM students WHERE NAME!="lily";
    42. -- 逻辑运算: and or not
    43. # 查询18~22之间的所有学生信息
    44. SELECT * FROM students WHERE age>=18 AND age<=22;
    45. # 查询id大于3的女同学
    46. SELECT * FROM students WHERE id>3 AND gender="female";
    47. # 查询id小于12或者id大于13的学生信息
    48. SELECT * FROM students WHERE id<12 OR id>13
    49. # 查询年龄不是18的女同学
    50. SELECT * FROM students WHERE (NOT age=18) AND gender="female";
    51. -- like % _
    52. # 查询名字以l开始的学生信息
    53. SELECT * FROM students WHERE NAME LIKE("l%");
    54. # 查询名字含有i的学生信息
    55. SELECT * FROM students WHERE NAME LIKE("%i%");
    56. # 查询名字仅有2个字符的学生信息
    57. INSERT INTO students(NAME, age) VALUES("wb", 11);
    58. SELECT * FROM students WHERE NAME LIKE("__");
    59. # 查询名字至少有2个字符的学生信息
    60. INSERT INTO students(NAME, age) VALUE("o", 111);
    61. SELECT * FROM students WHERE NAME LIKE("__%");
    62. -- in between and
    63. # 查询id是1或者11或者13的学生信息
    64. SELECT * FROM students WHERE id IN(1,11,13);
    65. # 查询年龄不是18,20的学生信息
    66. SELECT * FROM students WHERE age NOT IN(18,20);
    67. # 查询id是2至4的学生信息
    68. SELECT * FROM students WHERE id BETWEEN 2 AND 4;
    69. # 查询id是3-5的男同学信息
    70. SELECT * FROM students WHERE (id BETWEEN 10 AND 15) AND gender="male";
    71. # 查询年龄不在18至20之间的学生信息
    72. SELECT * FROM students WHERE age NOT BETWEEN 18 AND 20;
    73. -- is null is not null
    74. # 查询没有填写性别的学生
    75. SELECT * FROM students WHERE gender IS NULL;
    76. SELECT * FROM students WHERE gender IS NOT NULL;
    77. -- 聚合函数
    78. # 查询最大的年龄
    79. SELECT MAX(age) AS "最大年龄" FROM students;
    80. # 女性最大的id
    81. SELECT MAX(id) FROM students WHERE gender="female";
    82. # 查询未删除的学生的最小编号
    83. ALTER TABLE students DROP is_delete;
    84. ALTER TABLE students ADD is_delete ENUM ("0","1") DEFAULT "1";
    85. SELECT MIN(id) FROM students WHERE is_delete="0";
    86. # 查询男生年龄和
    87. SELECT SUM(age) FROM students WHERE gender="male";
    88. # 查询未删除女生的年龄的平均值 保留一位小数
    89. SELECT ROUND(AVG(age), 1) FROM students WHERE gender="female" AND is_delete="1";
    90. # 计算男性的平均年龄,保留2位小数
    91. SELECT ROUND(AVG(age), 2) FROM students WHERE gender="female";