AdventureWorks LT 数据库:模式、表和 SQL 练习
AdventureWorks LT 是 Microsoft SQL Server 的示例数据库,描述一家自行车制造商:客户、产品、产品类别和销售订单。 在 SQLtest.online 上,你可以直接在浏览器中查询它:做自动判题的练习,或在练习场中运行自己的查询,无需安装任何软件。
- 10 张主要表
- 847 位客户
- 295 种产品
- 34 道 SQL 练习
什么是 AdventureWorks
AdventureWorks 是 Microsoft 为 SQL Server 和 Azure SQL 提供的示例数据库。它描述了一家虚构的公司 Adventure Works Cycles,这家公司生产和销售自行车、零件及配件。
本站使用的是轻量版 AdventureWorks LT:同样的业务只用大约十张表,而不是几十张。它很适合练习 T-SQL,包括 TOP、类别树上的自连接以及多对多关系。
ER 图
该图展示了 AdventureWorks 的表以及它们之间的外键关系。点击可查看完整尺寸。
数据库包含什么
这些表分为三组。
客户
Customer、Address 以及关联表 CustomerAddress,后者还保存地址类型。
产品
Product、ProductCategory(树形结构:每个类别可以有父类别)、ProductModel 以及多种语言的描述。
销售
SalesOrderHeader 保存订单,SalesOrderDetail 保存订单明细。
这个版本中的 32 个订单日期都是 2008 年 6 月 1 日。产品描述通过 ProductModelProductDescription 与型号关联,该表还保存每条描述的语言(culture)。服务表 BuildVersion、ErrorLog 和 sysdiagrams 不在练习中使用。
各表的数据量:
| 表 | 行数 | 内容 |
|---|---|---|
| Customer | 847 | 客户 |
| CustomerAddress | 417 | 客户与地址的关联 |
| Address | 450 | 地址 |
| SalesOrderHeader | 32 | 订单 |
| SalesOrderDetail | 542 | 订单明细 |
| Product | 295 | 产品 |
| ProductCategory | 41 | 产品类别 |
| ProductModel | 128 | 产品型号 |
| ProductDescription | 762 | 产品描述 |
| ProductModelProductDescription | 762 | 按语言的型号与描述关联 |
表结构
点击表名可查看它的列、示例行和键。
表列表
- AddressID每个地址的唯一标识符 (PK)
- AddressLine1地址的第一行
- AddressLine2地址的第二行
- City城市
- StateProvince州或省
- CountryRegion国家
- PostalCode邮政编码
- rowguidguid
- ModifiedDate行创建或最后更新的时间戳
- 主键,btree (AddressID)
| AddressID | AddressLine1 | AddressLine2 | City | StateProvince | CountryRegion | PostalCode | rowguid | ModifiedDate |
|---|---|---|---|---|---|---|---|---|
| 9 | 8713 Yosemite Ct. | null | Bothell | Washington | United States | 98011 | 268AF621-76D7-4C78-9441-144FD139821A | 2006-07-01 00:00:00.000 |
- CustomerID每个客户的唯一标识符 (PK)
- NameStyle0 = FirstName 和 LastName 的数据以西方风格(名,姓)顺序存储。1 = 东方风格(姓,名)顺序。默认:0
- Title称谓
- FirstName名字
- MiddleName中间名
- LastName姓
- Suffix后缀
- CompanyName公司名称
- SalesPerson销售人员
- EmailAddress电子邮件
- Phone电话号码
- PasswordHash密码哈希
- PasswordSalt盐
- rowguidrowguid
- ModifiedDate行创建或最后更新的时间戳
- 主键,btree (CustomerID)
| CustomerID | NameStyle | Title | FirstName | MiddleName | LastName | Suffix | CompanyName | SalesPerson | EmailAddress | Phone | PasswordHash | PasswordSalt | rowguid | ModifiedDate |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 先生 | Orlando | N. | Gee | [null] | A Bike Store | adventure-works\pamela0 | orlando0@adventure-works.com | 245-555-0173 | L/Rlwxzp4w7RWmEgXX+/A7cXaePEPcp+KwQhl2fJL7w= | 1KjXYs4= | 3F5AE95E-B87D-4AED-95B4-C3797AFCB74F | 2005-08-01 00:00:00.000 |
- CustomerID客户在 Customer 表中的标识符
- AddressID地址在 Address 表中的标识符
- AddressType地址类型
- rowguidguid
- ModifiedDate行创建或最后更新的时间戳
- 主键,btree (CustomerID, AddressID)
- 外键 (CustomerID) 参考 Customer(CustomerID)
- 外键 (AddressID) 参考 Address(AddressID)
| CustomerID | AddressID | AddressType | rowguid | ModifiedDate |
|---|---|---|---|---|
| 29485 | 1086 | 主办公室 | 16765338-DBE4-4421-B5E9-3836B9278E63 | 2007-09-01 00:00:00.000 |
- ProductID每个产品的唯一标识符 (PK)
- Name产品名称
- ProductNumber商品编号
- Color产品颜色
- StandardCost产品价格
- ListPrice产品在目录中的价格
- Size产品尺寸
- Weight产品重量
- ProductCategoryID指向 ProductCategory 表的外键
- ProductModelID指向 ProductModel 表的外键
- SellStartDate销售开始日期的时间戳
- SellEndDate销售结束日期的时间戳
- DiscontinuedDate停止销售日期的时间戳
- ThumbNailPhoto产品缩略图
- ThumbnailPhotoFileName
缩略图文件名 - rowguidguid
- ModifiedDate行创建或最后更新的时间戳
- 主键,btree (ProductID, ProductCategoryID, ProductModelID)
- 外键 (ProductCategoryID) 参考 ProductCategory(ProductCategoryID)
- 外键 (ProductModelID) 参考 ProductModel(ProductModelID)
| ProductID | Name | ProductNumber | Color | StandardCost | ListPrice | Size | Weight | ProductCategoryID | ProductModelID | SellStartDate | SellEndDate | DiscontinuedDate | ThumbNailPhoto | ThumbnailPhotoFileName | rowguid | ModifiedDate |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 680 | HL Road Frame - Black, 58 | FR-R92B-58 | 黑色 | 1059.3100 | 1431.5000 | 58 | 1016.04 | 18 | 6 | 2002-06-01 00:00:00.000 | [null] | [null] | [binary] | no_image_available_small.gif | 43DD68D6-14A4-461F-9069-55309D90EA7E | 2008-03-11 10:01:36.827 |
- ProductCategoryID每个产品类别的唯一标识符 (PK)
- ParentProductCategoryID父产品类别的 ID
- Name产品类别名称
- rowguidguid
- ModifiedDate行创建或最后更新的时间戳
- 主键,btree (ProductCategoryID)
- 外键 (ParentProductCategoryID) 参考 ProductCategory(ProductCategoryID)
| ProductCategoryID | ParentProductCategoryID | Name | rowguid | ModifiedDate |
|---|---|---|---|---|
| 1 | [null] | 自行车 | CFBDA25C-DF71-47A7-B81B-64EE161AA37C | 2002-06-01 00:00:00.000 |
- ProductDescriptionID记录的唯一 ID (PK)
- Description产品描述
- rowguidguid
- ModifiedDate行创建或最后更新的时间戳
示例查询
这些查询展示了数据之间是如何关联的。复制任意一条,在练习场中运行即可。
客户及其地址:通过 CustomerAddress 实现的多对多关系。
SELECT TOP 3 c.FirstName, c.LastName, ca.AddressType, a.City
FROM Customer c
JOIN CustomerAddress ca ON ca.CustomerID = c.CustomerID
JOIN Address a ON a.AddressID = ca.AddressID
ORDER BY c.CustomerID;
| FirstName | LastName | AddressType | City |
|---|---|---|---|
| Catherine | Abel | Main Office | Van Nuys |
| Kim | Abercrombie | Main Office | Branch |
| Frances | Adams | Main Office | Modesto |
订单及其明细:从订单头到产品。
SELECT TOP 3 h.SalesOrderID, h.OrderDate, p.Name, d.OrderQty, d.UnitPrice
FROM SalesOrderHeader h
JOIN SalesOrderDetail d ON d.SalesOrderID = h.SalesOrderID
JOIN Product p ON p.ProductID = d.ProductID
ORDER BY h.SalesOrderID, d.SalesOrderDetailID;
| SalesOrderID | OrderDate | Name | OrderQty | UnitPrice |
|---|---|---|---|---|
| 71774 | 2008-06-01 00:00:00.000 | ML Road Frame-W - Yellow, 48 | 1 | 356.8980 |
| 71774 | 2008-06-01 00:00:00.000 | ML Road Frame-W - Yellow, 38 | 1 | 356.8980 |
| 71776 | 2008-06-01 00:00:00.000 | Rear Brakes | 1 | 63.9000 |
按主题分类的 SQL 练习
基于 AdventureWorks 数据库共有 34 道练习,从简单筛选到订单和产品分析。答案会在真实的 SQL Server 上自动检查。 右侧数字是该主题的练习数量,彩色圆点表示难度范围。
- 分析查询 7
从哪里开始
AdventureWorks 数据库的前几道练习:
常见问题
使用 AdventureWorks 需要安装 SQL Server 吗?
不需要。SQLtest.online 的练习和练习场都在我们的服务器上执行查询,有浏览器就够了。在练习场中,AdventureWorks 可在 SQL Server 2022 上使用。
AdventureWorks LT 与完整版 AdventureWorks 有什么区别?
完整版数据库在多个模式(Sales、Production、Person 等)中有几十张表。LT 版只保留业务核心——客户、产品和订单——大约十张表,更便于学习。
使用哪种 SQL 方言?
T-SQL,即 SQL Server 的方言。例如,使用 TOP 或 OFFSET … FETCH 代替 LIMIT。