|
所在平台: Udemy |
课程主页: https://www.udemy.com/course/sql-server-part5-advance-level/
课程评论:没有评论
SQL Server 进阶课程(第五部分)总结 本课程涵盖了 SQL Server 的高级主题,主要包括两个方面: **主题一:使用 SQL 从 Excel 导入数据** * **核心方法:** 主要利用 `OPENROWSET` 或 `OPENDATASOURCE` 函数,允许直接通过 SQL 查询来读取 Excel 文件。 * **关键依赖:** 成功导入需要安装相应的 OLE DB 提供程序,对于 `.xlsx` 文件,通常是 "Microsoft.ACE.OLEDB.12.0"。如果遇到“无法初始化 OLE DB 目标数据源对象”的错误,很可能是驱动程序缺失或配置问题。 * **驱动安装:** 需要从微软官方下载并安装 Access Database Engine (ACE OLEDB) 来解决驱动问题。 * **配置与权限:** 需要启用“Ad Hoc Distributed Queries”(通过 `sp_configure` 命令)来允许 SQL Server 执行非本地数据源的查询。 * **Excel 数据准备:** * **命名范围 (Named Ranges):** 在 Excel 中使用“名称管理器”为数据区域定义一个名称,可以使 SQL 查询更简洁。 * **Excel 表 (Excel Table):** 将数据转换为 Excel 表格并定义名称(例如 `SalesData`),也是一种推荐的准备方式。 * **SQL 查询示例:** `SELECT * FROM OPENROWSET(..., 'SELECT * FROM [SalesData$]')` 这是读取 Excel 数据的一种常见语法,其中 `[SalesData$]` 指的是 Excel 表或命名范围。 * **数据一致性:** 导入前需要确保 Excel 数据列名与目标 SQL Server 表的列名匹配,并且数据类型兼容,以避免导入错误。 **主题二:引用完整性与约束管理(高级 SQL 概念)** * **外键约束 (`FOREIGN KEY`):** * **`ON DELETE CASCADE`:** 当父表记录被删除时,所有相关的子表记录也会被自动删除。 * **`ON UPDATE CASCADE`:** 当父表的主键被更新时,子表中的外键也会自动更新。 * **无 `CASCADE` (默认):** 如果未指定 `CASCADE` 选项,则不允许删除或更新存在子表引用的父表记录,否则会报错。 * **临时禁用约束:** 使用 `ALTER TABLE [TableName] NOCHECK CONSTRAINT [ConstraintName]` 可以临时禁用指定的约束,这在批量数据导入时非常有用,可以避免因约束检查而降低导入效率。 * **重新启用约束:** 使用 `ALTER TABLE [TableName] CHECK CONSTRAINT [ConstraintName]` 可以在操作完成后重新启用被禁用的约束。 * **永久移除约束:** 使用 `ALTER TABLE [TableName] DROP CONSTRAINT [ConstraintName]` 可以完全移除约束,但这意味着关系完整性不再被强制执行。 * **约束的影响:** * **有约束时:** 保证了数据的完整性,例如,子表记录必须关联到存在的父表记录(插入时),且父表记录不能在存在子表引用时被删除(除非启用 `CASCADE`)。 * **无约束时:** 可能会导致“孤儿记录”(子表记录指向不存在的父表记录)或数据不一致的情况发生。 总而言之,本课程旨在提升学员在 SQL Server 中处理外部数据导入以及管理数据库关系和数据完整性的能力。
Topic1: Importing Data from Excel into SQL Server Using SQL: Key Points & ChallengesUse of OPENROWSET or OPENDATASOURCE: You can import Excel data using SQL queries like SELECT * FROM OPENROWSET(...), but it requires necessary drivers and access permissions.Common Driver Requirement: For.xlsx files, the "Microsoft.ACE.OLEDB.12.0" provider is typically required. It must be installed separately if missing.How to Install Drivers: Download and install Access Database Engine (ACE OLEDB) from Microsoft's official site to avoid provider errors.Typical Error - "Cannot initialize the data source object of OLE DB provider": This often means the driver is missing, SQL Server is running as a service, or the Excel file path is incorrect.Enable Ad Hoc Queries: sp_configure 'Ad Hoc Distributed Queries', 1 must be run (with RECONFIGURE) to allow ad hoc Excel file queries using OPENROWSET.Preparing Excel for Import: Use Named Ranges (via Name Manager) or convert data to an Excel Table with a defined name like SalesData for cleaner and safer import syntax.Accessing Table by Name: Use SQL like SELECT * FROM OPENROWSET(..., 'SELECT * FROM [SalesData$]') to read table-format Excel ranges.Ensure Column Matching: SQL Server expects consistent column names and data types between Excel and the target SQL table. Clean Excel data before import.Topic2: Referential Integrity & Constraint Management (Advanced SQL Concepts)Foreign Key with ON DELETE CASCADE: When enabled, deleting a record from the parent table automatically deletes related records in the child table.Foreign Key with ON UPDATE CASCADE: Updating a primary key in the parent table automatically updates the foreign key in the child table.No CASCADE (Default): If ON DELETE/UPDATE CASCADE is not specified, you'll get errors when trying to delete or update a parent record with child dependencies.Disabling Constraints Temporarily: Use ALTER TABLE NOCHECK CONSTRAINT to temporarily disable FK constraints (useful during bulk imports).Re-Enabling Constraints: Use ALTER TABLE CHECK CONSTRAINT to reenable constraints after your operation is complete.Permanent Constraint Removal: Use ALTER TABLE DROP CONSTRAINT when the relationship is no longer required, but beware-it removes enforcement entirely.Effect on Queries:Without constraints, accidental deletes or orphan inserts can occur.With constraints ON, child cannot exist without parent (INSERT fails if FK not matched), and parent cannot be deleted if children exist (unless CASCADE is enabled).