Bagikan melalui


Mulai cepat: Menggunakan penjelajah dan perancang skema

Dalam panduan cepat ini, Anda mempelajari bagaimana GitHub Copilot membantu pengembang dalam merancang, memahami, dan mengembangkan skema database dengan saran yang disesuaikan dengan konteks. Baik Anda membangun dari awal atau rekayasa balik tabel yang ada, GitHub Copilot menyederhanakan proses di seluruh kerangka kerja SQL dan pemetaan relasional objek (ORM), membuat skema bekerja lebih cepat, lebih cerdas, dan lebih mudah dipertahankan.

Bagian ini mencakup pembuatan skema baru dari awal dan bekerja dengan database yang sudah ada. Anda dapat menggunakan GitHub Copilot untuk menghasilkan definisi skema code-first, memperbarui objek, atau reverse-engineer dan menjelajahi database yang ada.

Pembuatan skema

Membuat skema dasar

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.

Mengubah skema

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`.

Membuat skema dengan hubungan dan batasan

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.

Membuat skrip migrasi untuk menambahkan kolom

Generate a Prisma migration to add a column called `LastModified` (`datetime`) to the `Post` table.

Merekayasa balik database yang sudah ada

Reverse engineer the current database and generate `CREATE TABLE` statements for all tables in the `SalesLT` schema.

Meringkas struktur tabel

Summarize the structure of the `SalesLT.Product` table in natural language.

Membuat model Python

Generate a `models.py` (Django) file that reflects the structure of the `SalesLT.Customer` table.

Menghasilkan konteks dan model Entity Framework Core

Generate an Entity Framework Core DbContext and model classes for the `SalesLT` schema.

Membuat definisi dan asosiasi model

Create a Sequelize model definition for the `SalesLT.Product` and `SalesLT.Category` tables with appropriate associations.

Membuat entitas dari tabel

Generate a TypeORM entity for the `SalesLT.Customer` table, including primary key and indexed fields.

Membuat skrip migrasi untuk membuat tabel baru

Generate a `knex.js` migration script to create the `SalesLT.SalesOrderHeader` table with `OrderDate`, `CustomerID`, and `TotalDue` columns.

Menentukan hubungan

Menentukan skrip relasional dengan referensi kunci asing

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)`.

Menambahkan tabel ke skema dengan referensi kunci asing

Add a `Categories` table to the `blog` schema and update the `Posts` table to include a nullable foreign key referencing `Categories(CategoryId)`.

Memperbarui database untuk menambahkan tabel dan memperbarui kolom

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.

Mengidentifikasi hubungan kunci asing untuk tabel

Identify and describe all foreign key relationships that involve the `SalesLT.SalesOrderHeader` table.

Ganti kunci asing dengan sebuah hubungan banyak ke banyak

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.

Hasilkan pemetaan di antara dua tabel

Write Prisma relation mappings between `Customer`, `SalesOrderHeader`, and `SalesOrderDetail`.

Memperbarui model data

Update a Sequelize model to include a `hasMany` and `belongsTo` relationship between `Customer` and `Order`.

Skema validasi

Menyarankan batasan untuk data sensitif

Suggest constraints for a table storing user passwords (for example, special characters and length limits).

Memvalidasi batasan tipe data

Confirm that the `Name` column in `SalesLT.ProductCategory` doesn't use `nvarchar(max)` and has a reasonable maximum length constraint.

Memvalidasi batasan kunci primer

Check whether the `SalesLT.Address` table has a primary key and all required fields defined.

Memvalidasi rekaman audit untuk tabel

Generate a SQL script to validate that all tables in the `SalesLT` schema include a `CreatedDate` or `ModifiedDate` column.

Menentukan model dan menyertakan logika validasi

Define a SQLAlchemy model for the `Customer` table and include validation logic using Pydantic or custom Python validators before inserting into the database.

Menambahkan anotasi data untuk validasi format

Add data annotations in an Entity Framework model to ensure fields like `Email` and `PhoneNumber` follow specific formats.

Membagikan pengalaman Anda

Untuk membantu kami memperbaiki dan meningkatkan GitHub Copilot untuk ekstensi MSSQL, gunakan templat masalah GitHub berikut untuk mengirimkan umpan balik Anda: GitHub Copilot Feedback

Saat mengirimkan umpan balik, pertimbangkan untuk menyertakan:

  • Skenario yang diuji - Beri tahu kami area mana yang Anda fokuskan, misalnya, pembuatan skema, pembuatan kueri, keamanan, pelokalan.

  • Apa yang bekerja dengan baik - Menjelaskan pengalaman apa pun yang terasa lancar, membantu, atau melebihi harapan Anda.

  • Masalah atau bug - Sertakan masalah, inkonsistensi, atau perilaku yang membingungkan. Cuplikan layar atau rekaman layar sangat membantu.

  • Saran untuk perbaikan – Bagikan ide untuk meningkatkan kegunaan, memperluas cakupan, atau meningkatkan respons GitHub Copilot.