没有合适的资源?快使用搜索试试~ 我知道了~
SQL教程
试读
64页
需积分: 0 1 下载量 114 浏览量
更新于2015-04-03
收藏 1.59MB DOC 举报
SQL教程是学习数据库管理和数据查询不可或缺的资源,尤其适合初学者。这个教程设计得简单易用,旨在帮助用户快速掌握SQL的基本概念和操作。
SQL,全称Structured Query Language(结构化查询语言),是一种用于管理关系数据库的标准编程语言。它允许用户创建、修改、查询和管理数据库,包括插入、更新、删除数据,以及定义数据库结构。SQL不仅用于检索数据,还用于执行数据更新操作,是数据科学、数据分析和软件开发等领域的重要工具。
SQL能做的事情非常广泛,包括但不限于:
1. **创建数据库和表**:你可以使用SQL来定义数据库的结构,创建包含各种数据类型的表格。
2. **插入数据**:向数据库表中添加新的记录或行。
3. **查询数据**:通过SELECT语句从一个或多个表中提取特定的信息,可以根据各种条件进行筛选。
4. **更新数据**:修改已经存在于数据库中的记录。
5. **删除数据**:从表中移除不再需要的记录。
6. **联接表**:合并来自不同表的数据,以获取更复杂的信息。
7. **聚合数据**:使用SUM、AVG、COUNT等函数对数据进行统计分析。
8. **创建视图**:定义虚拟表,基于一个或多个表的查询结果,方便后续查询。
9. **权限管理**:控制用户对数据库的访问权限,确保数据安全。
SQL是一种标准,尽管不同数据库管理系统(RDBMS,如MySQL、Oracle、SQL Server、PostgreSQL等)可能有自己的语法扩展,但基本的SQL语句和概念是通用的。这使得掌握SQL成为一项可移植的技能,无论你使用哪个RDBMS,基础操作大同小异。
在网站中使用SQL,可以高效地存储和处理大量用户数据,提高网站性能,支持动态内容生成和个性化用户体验。例如,电子商务网站会用SQL来管理产品目录、客户订单和交易信息。
SQL的语法是其核心,分为两大类别:
- **DML(Data Manipulation Language)**:用于数据操作,包括INSERT、UPDATE和DELETE语句。
- **DDL(Data Definition Language)**:用于数据定义,涉及CREATE、ALTER和DROP等语句,用于创建和修改数据库对象。
在SQL语句中,分号(;)通常作为语句的结束符,但在某些系统中不是必须的。正确使用分号可以帮助解析器区分不同的语句,尤其是在同一行中写多条命令时。
SQL SELECT语句是查询数据的基础,它允许你从一个或多个表中选择特定列,并根据条件过滤数据。SELECT语句可以包含多种元素,如选择的列、表名、连接条件、分组(GROUP BY)、排序(ORDER BY)和限制(LIMIT)。通过熟练运用这些元素,你可以构建出复杂的查询,满足各种数据分析需求。
这个SQL教程将引导你逐步掌握SQL的关键概念和实际应用,为你的数据工作打下坚实的基础。无论是入门学习还是进阶提升,都能从中受益。
SQL 教程...................................................................................................................................................7
SQL 简介...............................................................................................................................................7
什么是
SQL
?
...................................................................................................................................7
SQL
能做什么?
...............................................................................................................................7
SQL
是一种标准
-
但是
....................................................................................................................7
在您的网站中使用
SQL...................................................................................................................7
RDBMS..............................................................................................................................................7
SQL 语法...............................................................................................................................................9
数据库表
...........................................................................................................................................9
SQL
语句
...........................................................................................................................................9
重要事项
...........................................................................................................................................9
SQL
语句后面的分号?
...................................................................................................................9
SQL DML
和
DDL.............................................................................................................................9
SQL SELECT 语句..............................................................................................................................11
SQL SELECT
语句
...........................................................................................................................11
SQL SELECT
实例
...........................................................................................................................11
SQL SELECT *
实例
........................................................................................................................11
在结果集(
result-set
)中导航
.......................................................................................................12
SQL SELECT DISTINCT 语句..........................................................................................................13
SQL SELECT DISTINCT
语句
........................................................................................................13
使用
DISTINCT
关键词
..................................................................................................................13
SQL WHERE 子句..............................................................................................................................15
WHERE
子句
...................................................................................................................................15
使用
WHERE
子句
..........................................................................................................................15
引号的使用
.....................................................................................................................................16
SQL AND & OR 运算符.....................................................................................................................17
AND
和
OR
运算符
.........................................................................................................................17
原始的表
(
用在例子中的
)
:
.........................................................................................................17
AND
运算符实例
............................................................................................................................17
OR
运算符实例
...............................................................................................................................17
结合
AND
和
OR
运算符
................................................................................................................17
SQL ORDER BY 子句........................................................................................................................19
ORDER BY
语句
..............................................................................................................................19
原始的表
(
用在例子中的
)
:
.........................................................................................................19
实例
1..............................................................................................................................................19
实例
2..............................................................................................................................................19
实例
3..............................................................................................................................................20
实例
4..............................................................................................................................................20
SQL INSERT INTO 语句....................................................................................................................22
INSERT INTO
语句
.........................................................................................................................22
插入新的行
.....................................................................................................................................22
在指定的列中插入数据
.................................................................................................................22
SQL UPDATE 语句.............................................................................................................................24
Update
语句
.....................................................................................................................................24
更新某一行中的一个列
.................................................................................................................24
更新某一行中的若干列
.................................................................................................................24
SQL DELETE 语句.............................................................................................................................25
DELETE
语句
..................................................................................................................................25
Person:.............................................................................................................................................25
删除某行
.........................................................................................................................................25
删除所有行
.....................................................................................................................................25
SQL TOP 子句.....................................................................................................................................26
TOP
子句
.........................................................................................................................................26
MySQL
和
Oracle
中的
SQL SELECT TOP
是等价的
..................................................................26
原始的表
(
用在例子中的
)
:
.........................................................................................................26
SQL TOP
实例
.................................................................................................................................27
SQL TOP PERCENT
实例
...............................................................................................................27
SQL LIKE 操作符...............................................................................................................................28
LIKE
操作符
....................................................................................................................................28
原始的表
(
用在例子中的
)
:
.........................................................................................................28
LIKE
操作符实例
............................................................................................................................28
SQL 通配符.........................................................................................................................................30
SQL
通配符
.....................................................................................................................................30
原始的表
(
用在例子中的
)
:
.........................................................................................................30
使用
%
通配符
................................................................................................................................30
使用
_
通配符
.................................................................................................................................31
使用
[charlist]
通配符
....................................................................................................................32
SQL IN 操作符....................................................................................................................................33
IN
操作符
........................................................................................................................................33
原始的表
(
在实例中使用:
).........................................................................................................33
IN
操作符实例
................................................................................................................................33
SQL BETWEEN 操作符.....................................................................................................................34
BETWEEN
操作符
..........................................................................................................................34
原始的表
(
在实例中使用:
).........................................................................................................34
BETWEEN
操作符实例
..................................................................................................................34
实例
2..............................................................................................................................................35
SQL ALIAS(别名)...........................................................................................................................36
SQL Alias.........................................................................................................................................36
Alias
实例
:
使用表名称别名
..........................................................................................................36
Alias
实例
:
使用一个列名别名
......................................................................................................36
SQL JOIN............................................................................................................................................38
Join
和
Key......................................................................................................................................38
引用两个表
.....................................................................................................................................38
SQL JOIN -
使用
Join.....................................................................................................................39
不同的
SQL JOIN...........................................................................................................................39
SQL INNER JOIN 关键字..................................................................................................................40
SQL INNER JOIN
关键字
...............................................................................................................40
原始的表
(
用在例子中的
)
:
.........................................................................................................40
内连接(
INNER JOIN
)实例
........................................................................................................40
SQL LEFT JOIN 关键字.....................................................................................................................42
SQL LEFT JOIN
关键字
.................................................................................................................42
原始的表
(
用在例子中的
)
:
.........................................................................................................42
左连接(
LEFT JOIN
)实例
..........................................................................................................42
SQL RIGHT JOIN 关键字..................................................................................................................44
SQL RIGHT JOIN
关键字
...............................................................................................................44
原始的表
(
用在例子中的
)
:
.........................................................................................................44
右连接(
RIGHT JOIN
)实例
........................................................................................................44
SQL FULL JOIN 关键字.....................................................................................................................46
SQL FULL JOIN
关键字
.................................................................................................................46
原始的表
(
用在例子中的
)
:
.........................................................................................................46
全连接(
FULL JOIN
)实例
..........................................................................................................46
SQL UNION 和 UNION ALL 操作符................................................................................................48
SQL UNION
操作符
........................................................................................................................48
下面的例子中使用的原始表:
.....................................................................................................48
使用
UNION
命令
...........................................................................................................................49
UNION ALL.....................................................................................................................................49
使用
UNION ALL
命令
...................................................................................................................49
SQL SELECT INTO 语句...................................................................................................................51
SELECT INTO
语句
........................................................................................................................51
SQL SELECT INTO
实例
-
制作备份复件
.....................................................................................51
SQL SELECT INTO
实例
-
带有
WHERE
子句
.............................................................................51
SQL SELECT INTO
实例
-
被连接的表
.........................................................................................52
SQL CREATE DATABASE 语句.......................................................................................................53
CREATE DATABASE
语句
..............................................................................................................53
SQL CREATE DATABASE
实例
......................................................................................................53
SQL CREATE TABLE 语句................................................................................................................54
CREATE TABLE
语句
.....................................................................................................................54
SQL CREATE TABLE
实例
.............................................................................................................54
SQL 约束 (CONSTRAINTS)...................................................................................................................56
SQL
约束
.........................................................................................................................................56
SQL NOT NULL 约束........................................................................................................................57
SQL NOT NULL
约束
......................................................................................................................57
SQL UNIQUE 约束.............................................................................................................................58
SQL UNIQUE
约束
.........................................................................................................................58
SQL UNIQUE Constraint on CREATE TABLE...............................................................................58
撤销
UNIQUE
约束
........................................................................................................................59
SQL PRIMARY KEY 约束.................................................................................................................60
SQL PRIMARY KEY
约束
...............................................................................................................60
SQL PRIMARY KEY Constraint on CREATE TABLE.....................................................................60
SQL PRIMARY KEY Constraint on ALTER TABLE........................................................................61
撤销
PRIMARY KEY
约束
..............................................................................................................61
SQL FOREIGN KEY 约束..................................................................................................................62
SQL FOREIGN KEY
约束
...............................................................................................................62
SQL FOREIGN KEY Constraint on CREATE TABLE.....................................................................62
SQL FOREIGN KEY Constraint on ALTER TABLE........................................................................63
撤销
FOREIGN KEY
约束
..............................................................................................................64
SQL CHECK 约束...............................................................................................................................65
SQL CHECK
约束
...........................................................................................................................65
SQL CHECK Constraint on CREATE TABLE.................................................................................65
SQL CHECK Constraint on ALTER TABLE....................................................................................66
撤销
CHECK
约束
..........................................................................................................................66
SQL DEFAULT 约束...........................................................................................................................67
SQL DEFAULT
约束
.......................................................................................................................67
SQL DEFAULT Constraint on CREATE TABLE.............................................................................67
SQL DEFAULT Constraint on ALTER TABLE................................................................................67
撤销
DEFAULT
约束
......................................................................................................................68
SQL CREATE INDEX 语句................................................................................................................69
索引
.................................................................................................................................................69
CREATE INDEX
实例
.....................................................................................................................69
SQL 撤销索引、表以及数据库.........................................................................................................70
SQL DROP INDEX
语句
.................................................................................................................70
SQL DROP TABLE
语句
.................................................................................................................70
SQL DROP DATABASE
语句
..........................................................................................................70
SQL TRUNCATE TABLE
语句
........................................................................................................70
SQL ALTER TABLE 语句..................................................................................................................71
ALTER TABLE
语句
........................................................................................................................71
原始的表
(
用在例子中的
)
:
.........................................................................................................71
SQL ALTER TABLE
实例
................................................................................................................71
改变数据类型实例
.........................................................................................................................72
DROP COLUMN
实例
....................................................................................................................72
SQL AUTO INCREMENT 字段.........................................................................................................73
AUTO INCREMENT
字段
...............................................................................................................73
用于
MySQL
的语法
.......................................................................................................................73
用于
SQL Server
的语法
.................................................................................................................73
用于
Access
的语法
........................................................................................................................74
用于
Oracle
的语法
........................................................................................................................75
SQL VIEW(视图)...........................................................................................................................76
SQL CREATE VIEW
语句
...............................................................................................................76
SQL CREATE VIEW
实例
...............................................................................................................76
SQL
更新视图
.................................................................................................................................77
SQL
撤销视图
.................................................................................................................................78
SQL DATE 函数...................................................................................................................................79
SQL
日期
.........................................................................................................................................79
MySQL Date
函数
...........................................................................................................................79
SQL Server Date
函数
.....................................................................................................................79
SQL Date
数据类型
.........................................................................................................................79
SQL
日期处理
.................................................................................................................................80
SQL NULL 值......................................................................................................................................82
SQL NULL
值
..................................................................................................................................82
SQL
的
NULL
值处理
.....................................................................................................................82
SQL IS NULL...................................................................................................................................82
SQL IS NOT NULL..........................................................................................................................83
SQL NULL 函数..................................................................................................................................84
SQL ISNULL()
、
NVL()
、
IFNULL()
和
COALESCE()
函数
.........................................................84
SQL 数据类型.....................................................................................................................................86
Microsoft Access
数据类型
.............................................................................................................86
MySQL
数据类型
............................................................................................................................86
SQL Server
数据类型
......................................................................................................................88
SQL 服务器 – RDBMS.......................................................................................................................91
DBMS -
数据库管理系统(
Database Management System
)
......................................................91
RDBMS -
关系数据库管理系统(
Relational Database Management System
)
..........................91
SQL 函数.............................................................................................................................................92
函数的语法
.....................................................................................................................................92
函数的类型
.....................................................................................................................................92
合计函数(
Aggregate functions
)
.................................................................................................92
Scalar
函数
......................................................................................................................................94
SQL AVG 函数....................................................................................................................................95
定义和用法
.....................................................................................................................................95
SQL AVG()
实例
..............................................................................................................................95
SQL COUNT() 函数............................................................................................................................97
SQL COUNT()
语法
........................................................................................................................97
SQL COUNT(column_name)
实例
..................................................................................................97
SQL COUNT(DISTINCT column_name)
实例
................................................................................98
SQL FIRST() 函数...............................................................................................................................99
FIRST()
函数
...................................................................................................................................99
SQL FIRST()
实例
...........................................................................................................................99
SQL LAST() 函数.............................................................................................................................100
LAST()
函数
...................................................................................................................................100
SQL LAST()
实例
...........................................................................................................................100
SQL MAX() 函数..............................................................................................................................101
MAX()
函数
...................................................................................................................................101
SQL MAX()
实例
...........................................................................................................................101
SQL MIN() 函数................................................................................................................................102
MIN()
函数
....................................................................................................................................102
SQL MIN()
实例
............................................................................................................................102
SQL SUM() 函数...............................................................................................................................103
SUM()
函数
...................................................................................................................................103
SQL SUM()
实例
...........................................................................................................................103
SQL GROUP BY 语句......................................................................................................................104
GROUP BY
语句
...........................................................................................................................104
SQL GROUP BY
实例
...................................................................................................................104
GROUP BY
一个以上的列
...........................................................................................................105
SQL HAVING 子句...........................................................................................................................106
HAVING
子句
................................................................................................................................106
SQL HAVING
实例
........................................................................................................................106
SQL UCASE() 函数..........................................................................................................................108
UCASE()
函数
...............................................................................................................................108
SQL UCASE()
实例
.......................................................................................................................108
SQL LCASE() 函数...........................................................................................................................109
LCASE()
函数
................................................................................................................................109
SQL LCASE()
实例
........................................................................................................................109
SQL MID() 函数................................................................................................................................110
MID()
函数
....................................................................................................................................110
SQL MID()
实例
............................................................................................................................110
SQL LEN() 函数................................................................................................................................111
LEN()
函数
.....................................................................................................................................111
SQL LEN()
实例
.............................................................................................................................111
SQL ROUND() 函数.........................................................................................................................112
ROUND()
函数
..............................................................................................................................112
剩余63页未读,继续阅读
资源推荐
资源评论
2012-08-14 上传
178 浏览量
196 浏览量
2017-08-07 上传
174 浏览量
5星 · 资源好评率100%
180 浏览量
2010-02-03 上传
5星 · 资源好评率100%
2019-01-17 上传
124 浏览量
5星 · 资源好评率100%
154 浏览量
5星 · 资源好评率100%
2009-03-21 上传
152 浏览量
资源评论
qq_20616209
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 数据分析-46-JD某商品比价分析
- 营销广告人年终总结:年度工作总结汇报实例+如何写好年终总结汇报指导(PPT底稿,可编辑)
- 「数据结构课程设计」二叉排序树与文件操作
- 数据分析-58-谷歌应用市场的APP分析
- Tesseract-OCR集成java的docker镜相包
- 第九章:函数-下(例程代码)
- 用STM32设计19264的屏程序 包括点、线、圆、矩形、反显、任意位置的读写、汉字字符
- 深度学习基础-TensorFlow入门与使用指南-tensorflow-basics-and-use-cases.md
- 搜寻excel文件匹配并自动设置IP网络信息
- 一款超级好用的U盘格式化工具(解压直接使用)
- 华豫佰佳组合促销视图.sql
- estdtyfygighuiyhihihihji
- 汽车电子芯片市场调研报告:2023年市场规模已达约820.8亿元
- 圣诞树网页设计示例 + HTML & CSS基础学习
- stc51单片机资料datasheet
- 图书管理系统-数据库设计报告.docx
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功