在本快速入門中,您將瞭解 GitHub Copilot 如何協助開發人員使用內容感知建議來設計、瞭解及演進資料庫架構。 無論你是從零開始建置,還是逆向工程現有資料表,GitHub Copilot 都能簡化跨 SQL 與物件關聯映射(ORM)框架的流程,讓結構運作更快、更聰明且更容易維護。
本節涵蓋從頭開始建立新的架構,以及使用現有的資料庫。 您可以使用 GitHub Copilot 來產生以程式碼優先的方式進行架構定義、更新物件,或進行逆向工程並探索現有的資料庫。
模式建立
建立基本架構
Write a SQL script to create a new schema named `blog` for a blog application. The schema should include three tables: `Posts`, `Comments`, and `Users`. Each table must have appropriate primary keys, and the necessary foreign key relationships and constraints should be defined.
修改結構
Add a new column named `LastModified` of type `datetime` to the `Posts` table in the `blog` schema. Generate the updated SQL script reflecting this change, including the full definition of the modified schema.
It isn't needed to create the schema, but it would be great if you could use the script generated and run it to validate the accuracy of the generated code. The following section continues using this new schema called `blog`.
建立包含關係與約束的架構
Generate a Prisma schema for a blog application using my current database. The schema should define a new database schema named `blog` and include tables for `posts`, `authors`, and `comments`, with appropriate relationships and constraints.
建立遷移腳本來新增欄位
Generate a Prisma migration to add a column called `LastModified` (`datetime`) to the `Post` table.
逆向工程現有資料庫
Reverse engineer the current database and generate `CREATE TABLE` statements for all tables in the `SalesLT` schema.
概述表格架構
Summarize the structure of the `SalesLT.Product` table in natural language.
產生一個 Python 模型
Generate a `models.py` (Django) file that reflects the structure of the `SalesLT.Customer` table.
產生實體框架核心上下文與模型
Generate an Entity Framework Core DbContext and model classes for the `SalesLT` schema.
建立模型定義與關聯
Create a Sequelize model definition for the `SalesLT.Product` and `SalesLT.Category` tables with appropriate associations.
從資料表產生一個實體
Generate a TypeORM entity for the `SalesLT.Customer` table, including primary key and indexed fields.
產生遷移腳本來建立新的資料表
Generate a `knex.js` migration script to create the `SalesLT.SalesOrderHeader` table with `OrderDate`, `CustomerID`, and `TotalDue` columns.
定義關聯性
定義一個帶有外鍵參考的關聯腳本
Write SQL to define a one-to-many relationship between `Users` and `Posts` in the `blog` schema. Ensure the foreign key in `Posts` references `Users(UserId)`.
在模式中加入一個包含外鍵參照的資料表
Add a `Categories` table to the `blog` schema and update the `Posts` table to include a nullable foreign key referencing `Categories(CategoryId)`.
更新資料庫以新增資料表並更新欄位
Write SQL to update the `Users` table to include a `RoleId` column and create a new `Roles` table. Define a foreign key relationship and enforce that every user must have a role.
識別資料表的外鍵關係
Identify and describe all foreign key relationships that involve the `SalesLT.SalesOrderHeader` table.
用多對多關係取代外金鑰
Write a SQL script that removes a foreign key between `Posts` and `Categories` in the `blog` schema and replaces it with a many-to-many relationship using a new join table.
產生兩個資料表間的映射
Write Prisma relation mappings between `Customer`, `SalesOrderHeader`, and `SalesOrderDetail`.
更新資料模型
Update a Sequelize model to include a `hasMany` and `belongsTo` relationship between `Customer` and `Order`.
結構描述驗證
建議敏感資料的限制
Suggest constraints for a table storing user passwords (for example, special characters and length limits).
驗證資料型別限制
Confirm that the `Name` column in `SalesLT.ProductCategory` doesn't use `nvarchar(max)` and has a reasonable maximum length constraint.
驗證主鍵限制
Check whether the `SalesLT.Address` table has a primary key and all required fields defined.
驗證資料表的稽核紀錄
Generate a SQL script to validate that all tables in the `SalesLT` schema include a `CreatedDate` or `ModifiedDate` column.
定義模型並包含驗證邏輯
Define a SQLAlchemy model for the `Customer` table and include validation logic using Pydantic or custom Python validators before inserting into the database.
新增資料註解以驗證格式
Add data annotations in an Entity Framework model to ensure fields like `Email` and `PhoneNumber` follow specific formats.
分享您的體驗
若要協助我們精簡及改善 MSSQL 延伸模組的 GitHub Copilot,請使用下列 GitHub 問題範本來提交您的意見反應: GitHub Copilot 意見反應
提交意見反應時,請考慮包括:
測試的案例 – 讓我們知道您專注於哪些領域,例如架構建立、查詢產生、安全性、當地語系化。
表現良好之處 – 描述任何感覺順暢、有幫助或超乎您期望的體驗。
問題或錯誤 – 包含任何問題、不一致或混淆的行為。 螢幕快照或螢幕錄製特別有用。
改進建議 – 分享改善可用性、擴大涵蓋範圍或增強 GitHub Copilot 回應的想法。