MySQL是一種非常流行的關系型數據庫系統,在Web應用程序開發中被廣泛使用。本文介紹了MySQL數據庫的基本50個語句,這些語句可以幫助您建立數據庫、表格、插入數據、查詢數據、更新、刪除表等等。下面是MySQL的50個基本語句:
1. 創建數據庫: CREATE DATABASE database_name; 2. 刪除數據庫: DROP DATABASE database_name; 3. 進入數據庫: USE database_name; 4. 顯示所有數據庫: SHOW DATABASES; 5. 顯示數據庫表格: SHOW TABLES; 6. 創建表格: CREATE TABLE table_name(col1 data_type, col2 data_type, ...); 7. 修改表格: ALTER TABLE table_name ADD COLUMN column_name data_type; 8. 刪除表格: DROP TABLE table_name; 9. 插入數據: INSERT INTO table_name(col1, col2, ...) VALUES (value1, value2, ...); 10. 查詢語句: SELECT * FROM table_name; 11. 查詢指定列: SELECT col_name1, col_name2, ... FROM table_name; 12. 查詢帶條件的數據: SELECT * FROM table_name WHERE col_name = "value"; 13. 多條件查詢: SELECT * FROM table_name WHERE col_name1 = "value1" AND col_name2 = "value2"; 14. 排序: SELECT * FROM table_name ORDER BY col_name DESC; 15. 分組: SELECT col_name, COUNT(*) FROM table_name GROUP BY col_name; 16. 查詢最大值: SELECT MAX(col_name) FROM table_name WHERE ... 17. 查詢最小值: SELECT MIN(col_name) FROM table_name WHERE ... 18. 查詢平均值: SELECT AVG(col_name) FROM table_name WHERE ... 19. 查詢總和: SELECT SUM(col_name) FROM table_name WHERE ... 20. 列別名: SELECT col_name AS another_name FROM table_name; 21. 統計查詢結果: SELECT COUNT(*) FROM table_name; 22. 使用LIKE查詢: SELECT * FROM table_name WHERE col_name LIKE "value%"; 23. 使用IS NULL查詢: SELECT * FROM table_name WHERE col_name IS NULL; 24. 使用IS NOT NULL查詢: SELECT * FROM table_name WHERE col_name IS NOT NULL; 25. 使用IN查詢: SELECT * FROM table_name WHERE col_name IN ("value1", "value2", ...); 26. 使用BETWEEN查詢: SELECT * FROM table_name WHERE col_name BETWEEN 'value1' AND 'value2'; 27. 查詢不重復的結果: SELECT DISTINCT col_name FROM table_name WHERE ...; 28. 使用LIMIT限制結果數量: SELECT * FROM table_name WHERE ... LIMIT 10; 29. 更新數據: UPDATE table_name SET col_name = "new_value" WHERE ...; 30. 更新多個列值: UPDATE table_name SET col_name1 = "new_value1", col_name2 = "new_value2", ... WHERE ...; 31. 刪除單行數據: DELETE FROM table_name WHERE col_name = "value"; 32. 刪除多行數據: DELETE FROM table_name WHERE col_name1 = "value1" AND col_name2 = "value2"; 33. 刪除表格數據: TRUNCATE TABLE table_name; 34. 更改表格名稱: RENAME TABLE table_name1 TO table_name2; 35. 視圖創建: CREATE VIEW view_name AS SELECT col_name1, col_name2 FROM table_name WHERE ...; 36. 視圖查詢: SELECT * FROM view_name; 37. 視圖刪除: DROP VIEW view_name; 38. 使用函數查詢: SELECT UPPER(col_name) FROM table_name WHERE ...; 39. 使用JOIN查詢: SELECT * FROM tableA JOIN tableB ON tableA.col_name = tableB.col_name; 40. 使用INNER JOIN查詢: SELECT * FROM tableA INNER JOIN tableB ON tableA.col_name = tableB.col_name; 41. 使用LEFT JOIN查詢: SELECT * FROM tableA LEFT JOIN tableB ON tableA.col_name = tableB.col_name; 42. 使用RIGHT JOIN查詢: SELECT * FROM tableA RIGHT JOIN tableB ON tableA.col_name = tableB.col_name; 43. 使用FULL OUTER JOIN查詢: SELECT * FROM tableA LEFT JOIN tableB ON tableA.col_name = tableB.col_name UNION SELECT * FROM tableA RIGHT JOIN tableB ON tableA.col_name = tableB.col_name; 44. 添加外鍵: ALTER TABLE table_name ADD FOREIGN KEY (col_name) REFERENCES table_name2 (col_name); 45. 刪除外鍵: ALTER TABLE table_name DROP FOREIGN KEY foreign_key_name; 46. 創建索引: CREATE INDEX index_name ON table_name (col_name); 47. 刪除索引: DROP INDEX index_name; 48. 使用IF條件語句: SELECT col_name, IF(col_name = "value", "yes", "no") FROM table_name WHERE ...; 49. 使用CASE條件語句: SELECT col_name, CASE WHEN col_name = "value1" THEN "option1" WHEN col_name = "value2" THEN "option2" WHEN col_name = "value3" THEN "option3" ELSE "option4" END FROM table_name WHERE ...; 50. 使用LIMIT和OFFSET分頁查詢: SELECT * FROM table_name LIMIT 10 OFFSET 20;
下一篇mysql 清除