MySQL語法100例是MySQL數據庫開發人員必備的基礎知識,本文將為您介紹100個MySQL語法實例。
1. 創建數據庫
CREATE DATABASE mydatabase;
2. 刪除數據庫
DROP DATABASE mydatabase;
3. 切換數據庫
USE mydatabase;
4. 創建數據表
CREATE TABLE mytable ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50), age INT, gender VARCHAR(10) );
5. 修改數據表結構
ALTER TABLE mytable ADD COLUMN birthday DATE; ALTER TABLE mytable MODIFY COLUMN age TINYINT; ALTER TABLE mytable DROP COLUMN gender;
6. 刪除數據表
DROP TABLE mytable;
7. 插入數據
INSERT INTO mytable (name, age, gender, birthday) VALUES ('Tom', 20, 'male', '1990-01-01');
8. 更新數據
UPDATE mytable SET name='Jerry', age=18 WHERE id=1;
9. 刪除數據
DELETE FROM mytable WHERE id=1;
10. 查詢數據
SELECT * FROM mytable; SELECT name, age FROM mytable WHERE gender='male' ORDER BY age DESC LIMIT 10;
11. 常用函數
SELECT COUNT(*) FROM mytable; SELECT AVG(age) FROM mytable; SELECT MAX(age) FROM mytable; SELECT MIN(age) FROM mytable; SELECT CONCAT(name, ' is ', age, ' years old') FROM mytable;
12. 連接查詢
SELECT mytable.name, mytable.age, myother.gender FROM mytable JOIN myother ON mytable.name = myother.name;
13. 子查詢
SELECT * FROM mytable WHERE age = (SELECT MAX(age) FROM mytable);
14. 索引
CREATE INDEX idx_name ON mytable (name); CREATE UNIQUE INDEX idx_email ON mytable (email); DROP INDEX idx_name ON mytable;
15. 事務
START TRANSACTION; INSERT INTO mytable (name, age) VALUES ('John', 25); INSERT INTO mytable (name, age) VALUES ('Jane', 30); COMMIT;
以上是MySQL語法100例的部分內容,希望對您學習MySQL有所幫助。