MySQL是一種關系型數據庫管理系統,它支持SQL語言。SQL是結構化查詢語言的簡稱,用于在關系型數據庫中查詢、插入、更新和刪除數據。以下是MySQL中常見的基本語句及其例子:
1. 創建數據庫 CREATE DATABASE example; 2. 切換數據庫 USE example; 3. 創建表 CREATE TABLE customers ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); 4. 插入數據 INSERT INTO customers (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com'); 5. 查詢數據 SELECT * FROM customers; 6. 更新數據 UPDATE customers SET email='john.doe@example.com' WHERE id=1; 7. 刪除數據 DELETE FROM customers WHERE id=1; 8. 建立索引 CREATE INDEX idx_lastname ON customers (lastname); 9. 聯合查詢 SELECT a.firstname, a.lastname, b.order_date FROM customers a INNER JOIN orders b ON a.id = b.customer_id; 10. 使用子查詢 SELECT firstname, lastname FROM customers WHERE id IN (SELECT customer_id FROM orders);
上一篇c json編碼轉換