MySQL是目前使用最广泛的关系型数据库管理系统,它常常被用于Web应用程序中。学习MySQL的时候,掌握常用的MySQL语句是至关重要的。
下面是常用MySQL语句的大全:
1. 创建数据库:
CREATE DATABASE databasename;
2. 删除数据库:
DROP DATABASE databasename;
3. 选择数据库:
USE databasename;
4. 创建数据表:
CREATE TABLE tablename (
column1 datatype,
column2 datatype,
column3 datatype,
.....
);
5. 删除数据表:
DROP TABLE tablename;
6. 插入数据:
INSERT INTO tablename (column1, column2, column3, ....)
VALUES (value1, value2, value3, ....);
7. 更新数据:
UPDATE tablename
SET column1 = value1, column2 = value2, ....
WHERE condition;
8. 删除数据:
DELETE FROM tablename WHERE condition;
9. 查询数据:
SELECT column1, column2, column3, ....
FROM tablename
WHERE condition;
10. 模糊查询:
SELECT column1, column2, ....
FROM tablename
WHERE column LIKE 'value%';
11. 求和:
SELECT SUM(column_name) FROM table_name;
12. 求平均值:
SELECT AVG(column_name) FROM table_name;
13. 求最大值:
SELECT MAX(column_name) FROM table_name;
14. 求最小值:
SELECT MIN(column_name) FROM table_name;
15. 连接:
SELECT column1, column2, ....
FROM tablename1
JOIN tablename2
ON tablename1.column = tablename2.column;
16. 排序:
SELECT column1, column2, ....
FROM tablename
ORDER BY column_name ASC|DESC;
17. 分组:
SELECT column1, COUNT(*)
FROM tablename
GROUP BY column_name;
18. 嵌套查询:
SELECT *
FROM tablename
WHERE column_name IN (SELECT column_name FROM tablename2 WHERE condition);
19. 修改表结构:
ALTER TABLE tablename ADD column_name datatype;
ALTER TABLE tablename DROP column_name;
ALTER TABLE tablename MODIFY column_name datatype;
ALTER TABLE tablename RENAME TO new_tablename;
20. 创建索引:
CREATE INDEX index_name ON tablename (column_name);
21. 删除索引:
DROP INDEX index_name ON tablename;
以上是常用MySQL语句的大全。掌握这些语句可以让你更加容易地进行SQL查询和操作数据库。