SQL语句大全:从基础到高级实例

需积分: 16 1 下载量 34 浏览量 更新于2024-09-21 1 收藏 91KB DOC 举报
本资源是一份全面的SQL语句指南,专为SQL初学者设计,包含了从基础操作到高级功能的详细讲解。首先,对于数据库管理,有创建(CREATE DATABASE)和删除(DROP DATABASE)数据库的操作,以及如何在SQL Server中备份数据,通过`sp_addumpdevice`函数指定设备和备份路径。创建新表是数据库设计的基础,包括两种方式:一是直接复制已有表结构(A),二是基于SELECT语句定义新的表结构(B)。 对于表的维护,有删除表(DROP TABLE)的操作,以及添加列(ALTER TABLE ADD COLUMN)的功能,但需要注意,一旦列被添加,无法删除或更改其数据类型。管理表的约束,如设置主键(ADD PRIMARY KEY)和删除主键(DROP PRIMARY KEY),同样被涵盖。创建索引(CREATE INDEX)和删除索引(DROP INDEX)用于优化查询性能,但索引一旦创建,其结构是固定的,修改时需重新构建。 此外,该文档还涉及创建视图(CREATE VIEW)和删除视图(DROP VIEW),这是一种对数据进行定制查询的方式。最后,提供了五个基本的SQL操作示例:SELECT(用于检索数据)、INSERT(插入数据)、DELETE(删除记录)、UPDATE(更新数据)和查找(查找特定记录)。 这份文档是学习SQL语言的重要参考资料,不仅涵盖了语法要点,还有实际操作的示例,有助于新手快速上手并理解SQL在数据管理和处理中的核心作用。无论是初学者还是有一定经验的开发者,都可以从中找到所需的工具和知识。
2019-03-21 上传
目 录 (01) SELECT .................................. ............... .......................................2 查找 SELECT "栏位名" FROM "表格名" (02) DISTINCT................................. ............... .....................................2 不同值 SELECT DISTINCT "栏位名" FROM "表格名" (03) WHERE...................................... ............... ....................................2 条件 SELECT "栏位名" FROM "表格名" WHERE "条件" (04) AND OR ...................................... ............... ..................................3 条件并和或 SELECT "栏位名" FROM "表格名" WHERE "简单条件" {[AND|OR] "简单条件"} (05) IN .............................................. ............... ..................................3 包含 SELECT "栏位名" FROM "表格名" WHERE "栏位名" IN ('值一', '值二 ', ...) (06) BETWEEN.............................. ............... ........................................4 范围包含 SELECT "栏位名" FROM " 表格名" WHERE "栏位名" BETWEEN '值一' AND '值二' (07) LIKE....................................... ............... .......................................4 通配符包含 SELECT "栏位名" FROM "表格名" WHERE "栏位名" LIKE {套 式} -- 支持通配符‘_’ 单个字符 '%' 任意字符 (08) ORDER BY............................... ............... ......................................5 排序 SELECT "栏位名" FROM "表格名" [WHERE "条件"] ORDER BY "栏位 名" [ASC, DESC] -- ASC 小到大 DESC 大到小 (09) 函数........................................ ............... ......................................5 函数 AVG (平均) COUNT (计数) MAX (最大值) MIN (最小值) SUM (总合) SELECT "函数名"("栏位名") FROM "表格名" (10) COUNT .................................... .............. ......................................6 计 数 SELECT COUNT(store_name) FROM Store_Information WHERE store_name is not NULL -- 统计非空 SELECT COUNT(DISTINCT store_name) FROM Store_Information -- 统计多 少个不同 (11) Group By .................................. .............. .....................................6 字段分组 SELECT "栏位 1", SUM("栏位 2") FROM "表格名" GROUP BY " 栏位 1" (12) HAVING...................................... .............. ....................................7 函数条件定位 SELECT "栏位 1", SUM("栏位 2") FROM "表格名" GROUP BY "栏位 1" HAVING (函数条件) (13) ALIAS........................................... .............. ..................................7 别名 SELECT "表格别名"."栏位 1" "栏位别名" FROM "表格名" "表格别名" (14) 连接................................................ ..............................................8 SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2 WHERE A1.store_name = A2.store_name GROUP BY A1.region_name (15) 外部连接........................................... ............................................9 SELECT A1.store_name, SUM(A2.Sales) SALES FROM Georgraphy A1, Store_Information A2 WHERE A1.store_name = A2.store_name (+) GROUP BY A1.store_name (16) Subquery .............................. .............. .........................................9 嵌套 SELECT "栏位 1" FROM "表格" WHERE "栏位 2" [比较运算素] (SELECT "栏位 1" FROM "表格" WHERE (17) UNION.................................... ............... ......................................10 合并不重复结果 [SQL 语句 1] UNION [SQL 语句 2] (18) UNION ALL....................................... .............. ............................ 11 合并所有结果 [SQL 语句 1] UNION ALL [SQL 语句 2] (19) INTERSECT..................................................... ............... ............. 11 查找相同值 [SQL 语句 1] INTERSECT [SQL 语句 2] (20) MINUS............................ ............... ..............................................12 显示第一个语句中不在第二个语句中的项 [SQL 语句 1] MINUS [SQL 语句 2] (21) Concatenate................................... ............... ...............................12 结果相加(串联) MySQL/Oracle: SELECT CONCAT(region_name,store_name) FROM Geography WHERE store_name = 'Boston'; SQL Server: SELECT region_name + ' ' + store_name FROM Geography WHERE store_name = 'Boston'; (22) Substring ...................................................... ............... ...............13 取字符 SUBSTR(str,pos) SUBSTR(str,pos,len) (23) TRIM ...... .............. .....................................................................14 去空 SELECT TRIM(' Sample '); TRIM()首尾, RTRIM()首, LTRIM()尾 (24) Create Table ........... .............. .....................................................14 建立表格 CREATE TABLE "表格名"("栏位 1" "栏位 1 资料种类","栏位 2" "栏位 2 资料种类",... ) (25) Create View............................. .............. ......................................15 建立表格视观表 CREATE VIEW "VIEW_NAME" AS "SQL 语句" (26) Create Index........................................... ............... ......................16 建立索引 CREATE INDEX "INDEX_NAME" ON "TABLE_NAME" (COLUMN_NAME) (27) Alter Table.. .............. ..................................................................16 修改表 ALTER TABLE "table_name"[改变方式] -- ADD 增加;DROP 删 除;CHANGE 更名;MODIFY 更改类型 (28) 主键.......................... ..................................................................18 ALTER TABLE Customer ADD PRIMARY KEY (SID) (29) 外来主键....................................... ............ ..................................18 CREATE TABLE ORDERS(Order_ID integer,Order_Date date,Customer_SID integer,Amount double,Primary Key (Order_ID),Foreign Key (Customer_SID) references CUSTOMER(SID)); (30) Drop Table................................................. ............... ...................19 删除表 DROP TABLE "表格名" (31) Truncate Table ................. ............... ............................................20 清除表内容 TRUNCATE TABLE "表格名" (32) Insert Into....................................... ............... .............................20 插入内容 INSERT INTO "表格名" ("栏位 1", "栏位 2", ...) VALUES ("值 1", "值 2", ...) (33) Update ........................ ................ ................................................20 修改内容 UPDATE "表格名" SET "栏位 1" = [新值] WHERE {条件} (34) Delete ......................................... .............. .................................21 DELETE FROM "表格名" WHERE {条件}