MySQL是一个开放源码的关系型数据库管理系统,使用SQL作为其查询语言。它是Web开发中常用的数据库管理系统之一。MySQL的语法十分丰富,可以执行各种数据库操作,包括创建、修改、删除和查询等。
MySQL的基础语句可以分为以下几类:
- 数据库操作语句:
- 创建数据库:CREATE DATABASE database_name;
- 删除数据库:DROP DATABASE database_name;
- 使用数据库:USE database_name;
- 显示所有数据库:SHOW DATABASES;
- 显示某个数据库的表:SHOW TABLES;
- 表操作语句:
- 创建表:CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
- 删除表:DROP TABLE table_name;
- 修改表:ALTER TABLE table_name ADD column datatype;
- 修改表名:ALTER TABLE table_name RENAME TO new_table_name;
- 数据操作语句:
- 插入数据:INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- 更新数据:UPDATE table_name SET column_name1 = value1, column_name2 = value2 WHERE condition;
- 删除数据:DELETE FROM table_name WHERE condition;
- 查询数据:SELECT column1, column2, ... FROM table_name WHERE condition;
- 数据查询语句:
- 查询所有列:SELECT * FROM table_name;
- 按条件查询:SELECT * FROM table_name WHERE condition;
- 指定查询列:SELECT column1, column2, ... FROM table_name;
- 重命名列名:SELECT column1 AS alias FROM table_name;
- 按照某列排序:SELECT * FROM table_name ORDER BY column_name ASC|DESC;
- 分页查询:SELECT * FROM table_name LIMIT start, count;
- 数据过滤语句:
- 模糊查询:SELECT * FROM table_name WHERE column_name LIKE 'value%';
- 正则表达式查询:SELECT * FROM table_name WHERE column_name REGEXP 'pattern';
- 范围查询:SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;
- 空值判断:SELECT * FROM table_name WHERE column_name IS NULL|NOT NULL;
- 数据聚合语句:
- 求和:SELECT SUM(column_name) FROM table_name;
- 平均值:SELECT AVG(column_name) FROM table_name;
- 计数:SELECT COUNT(column_name) FROM table_name;
- 最大值:SELECT MAX(column_name) FROM table_name;
- 最小值:SELECT MIN(column_name) FROM table_name;
- 分组聚合:SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
- 数据联接语句:
- 内连接:SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
- 左连接:SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
- 右连接:SELECT * FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
以上是MySQL的基础语句大全,涵盖了数据库的创建、删除、修改以及数据的插入、更新、删除和查询等常见操作。在实际开发中,我们需要根据具体需求来选择合适的语句进行操作。通过熟练掌握这些语句,可以有效地操作和管理MySQL数据库。