快速入门:使用架构浏览器和设计器

在本快速入门中,你将了解 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.

生成 Entity Framework Core 上下文和模型

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 响应的想法。