SQL语句全攻略:从基础到高级操作

需积分: 50 0 下载量 5 浏览量 更新于2024-07-25 收藏 326KB PDF 举报
该资源是一个全面的SQL语句教程,涵盖了从基础到进阶的各种SQL操作,包括查询、数据过滤、排序、聚合函数、连接、子查询以及数据库的创建、修改和删除等。 在SQL(Structured Query Language)中,`SELECT`语句是最基本也是最重要的查询工具,用于从数据库中提取所需的数据。它允许你指定想要选择的列("栏位名")以及从哪个表("表格名")中选取。例如,基本的`SELECT`语句结构如下: ```sql SELECT 列1, 列2, ... FROM 表格名; ``` 在这个例子中,`列1, 列2`是你想从`表格名`中选取的特定列。如果想选择所有列,可以使用通配符`*`,表示选取所有列: ```sql SELECT * FROM 表格名; ``` `DISTINCT`关键字用于去除查询结果中的重复行,确保返回唯一的记录。例如: ```sql SELECT DISTINCT 列名 FROM 表格名; ``` `WHERE`子句用于指定筛选条件,只返回满足条件的行。例如,如果我们想找出`sales`大于1000的商店信息: ```sql SELECT * FROM Store_Information WHERE sales > 1000; ``` `AND`和`OR`用于组合多个条件。例如: ```sql SELECT * FROM Store_Information WHERE sales > 1000 AND date >= '2022-01-01'; ``` `IN`用于指定一个值列表,匹配其中任何一个值。例如: ```sql SELECT * FROM Store_Information WHERE store_name IN ('LosAnge', 'NewYork'); ``` `BETWEEN`用于指定一个范围,匹配在该范围内的值: ```sql SELECT * FROM Store_Information WHERE sales BETWEEN 1000 AND 2000; ``` `LIKE`用于模式匹配,通常与通配符`%`和`_`一起使用: ```sql SELECT * FROM Store_Information WHERE store_name LIKE 'L%'; ``` `ORDER BY`用于对结果进行排序,`ASC`表示升序,`DESC`表示降序: ```sql SELECT * FROM Store_Information ORDER BY sales DESC; ``` `GROUP BY`用于对数据进行分组,常与聚合函数如`COUNT`, `SUM`, `AVG`, `MAX`, `MIN`一起使用: ```sql SELECT store_name, COUNT(*) FROM Store_Information GROUP BY store_name; ``` `HAVING`在`GROUP BY`后使用,用于对分组后的数据设置条件: ```sql SELECT store_name, COUNT(*) FROM Store_Information GROUP BY store_name HAVING COUNT(*) > 1; ``` `ALIAS`用于给表或列设置别名,简化查询语句: ```sql SELECT s.store_name AS Store_Name, s.sales AS Sales FROM Store_Information s; ``` `JOIN`用于连接两个或多个表,`LEFT JOIN`, `RIGHT JOIN`表示包含左表或右表的所有记录。`INNER JOIN`返回两个表中匹配的记录: ```sql SELECT t1.column, t2.column FROM Table1 t1 INNER JOIN Table2 t2 ON t1.key = t2.key; ``` `SUBQUERY`(子查询)嵌套在另一个查询中,用于执行复杂查询: ```sql SELECT * FROM Store_Information WHERE sales > (SELECT AVG(sales) FROM Store_Information); ``` `UNION`, `UNION ALL`, `INTERSECT`, `MINUS`用于合并多个查询的结果集: ```sql SELECT column FROM Table1 UNION SELECT column FROM Table2; ``` `CONCATENATE`用于连接两个或多个字符串: ```sql SELECT CONCAT(store_name, ' - ', city) AS StoreInfo FROM Store_Information; ``` `SUBSTRING`, `TRIM`用于处理字符串,分别用于截取子串和去除两侧的空白: ```sql SELECT SUBSTRING(store_name, 1, 5) FROM Store_Information; SELECT TRIM(store_name) FROM Store_Information; ``` `CREATE TABLE`, `CREATE VIEW`, `CREATE INDEX`用于创建新的表、视图和索引: ```sql CREATE TABLE NewTable (column1 datatype, column2 datatype); CREATE VIEW ViewName AS SELECT * FROM TableName WHERE condition; CREATE INDEX idx_sales ON Store_Information (sales); ``` `ALTER TABLE`用于修改已存在的表结构: ```sql ALTER TABLE Store_Information ADD COLUMN city VARCHAR(50); ``` `PRIMARY KEY`定义表中的唯一标识,`FOREIGN KEY`用于建立表间关联: ```sql CREATE TABLE Orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)); ``` `DROP TABLE`, `TRUNCATE TABLE`用于删除和清空表: ```sql DROP TABLE IF EXISTS OldTable; TRUNCATE TABLE Store_Information; ``` `INSERT INTO`用于向表中插入新数据: ```sql INSERT INTO Store_Information (store_name, sales, date) VALUES ('NewStore', 5000, '2023-03-01'); ``` `UPDATE`用于更新现有数据: ```sql UPDATE Store_Information SET sales = sales + 100 WHERE store_name = 'LosAnge'; ``` `DELETE`用于删除数据,`DELETE FROM`会删除所有行,`DELETE ... WHERE`则根据条件删除: ```sql DELETE FROM Store_Information WHERE sales < 1000; ``` 这个SQL教程覆盖了数据库管理、查询和维护的各个方面,适合初学者和有一定经验的开发者参考学习。通过深入理解和实践这些语句,可以有效地操作和管理数据库中的数据。