[翻译]ADO.NET Entity Framework Beta2(六)/快速入门(实体框架)(1)/创建学校演示数据库

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介:

In this task, you will create the schema for the School database and load data into the database. To do this, you use SQL Server Management Studio or SQL Server Management Studio Express to execute a Transact-SQL script. The resulting School database is used as the relational data store in the rest of the tasks in this quickstart.

在本任务中,你将创建学校数据库架构并填充数据。你要使用SQL Server 管理中心或 SQL Server Express管理中心来执行一个TSQL脚本。生成的数据库将在剩下的快速入门任务中作为关系型数据库被使用。

To create the School database and schema/创建数据库及架构

  1. On the File menu, point to New, and then click Database Engine Query. 在文件菜单,点击新建,然后单击数据库引擎查询

  2. In the Connect to Database Engine dialog box, type either localhost or the name of the local SQL Server instance, and then click Connect.在连接到数据库引擎对话框,输入Loccalhost或者本地SQL Server的实例名,然后单击连接

  3. Paste the following Transact-SQL script in the query window and then click Execute.粘贴以下TSQL脚本到查询窗口并单击工具栏上的执行按钮。



    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    USE [master];
    GO

    IF EXISTS (SELECT * FROM sys.databases WHERE name = 'School')
    DROP DATABASE School;
    GO

    -- Create the School database.
    CREATE DATABASE School;
    GO

    USE School;
    GO

    -- Create the Course table.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
    OBJECT_ID(N'[dbo].[Course]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
    BEGIN
    CREATE TABLE [dbo].[Course](
    [CourseID] [int] NOT NULL,
    [Title] [nvarchar](100) NOT NULL,
    [StartDate] [datetime] NULL,
    [EndDate] [datetime] NULL,
    [Credits] [int] NULL,
    CONSTRAINT [PK_School.Course] PRIMARY KEY CLUSTERED
    (
    [CourseID] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]
    END
    GO

    -- Create the Person table.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
    OBJECT_ID(N'[dbo].[Person]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
    BEGIN
    CREATE TABLE [dbo].[Person](
    [PersonID] [int] IDENTITY(1,1) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [HireDate] [datetime] NULL,
    [EnrollmentDate] [datetime] NULL,
    CONSTRAINT [PK_School.Student] PRIMARY KEY CLUSTERED
    (
    [PersonID] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]
    END
    GO

    -- Create the Enrollment table.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id =
    OBJECT_ID(N'[dbo].[Enrollment]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
    BEGIN
    CREATE TABLE [dbo].[Enrollment](
    [EnrollmentID] [int] IDENTITY(1,1) NOT NULL,
    [CourseID] [int] NOT NULL,
    [StudentPersonID] [int] NOT NULL,
    [Grade] [decimal](3, 2) NULL,
    CONSTRAINT [PK_Enrollment] PRIMARY KEY CLUSTERED
    (
    [EnrollmentID] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]
    END
    GO

    -- Create the CourseInstructor table.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[CourseInstructor]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
    BEGIN
    CREATE TABLE [dbo].[CourseInstructor](
    [CourseInstructorID] [int] IDENTITY(1,1) NOT NULL,
    [CourseID] [int] NOT NULL,
    [PersonID] [int] NOT NULL,
    CONSTRAINT [PK_CourseInstructor] PRIMARY KEY CLUSTERED
    (
    [CourseInstructorID] ASC
    ) ON [PRIMARY]
    ) ON [PRIMARY]
    END
    GO

    -- Define the relationship between Enrollment and Course.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_Enrollment_Course]') AND type = 'F')
    ALTER TABLE [dbo].[Enrollment] WITH CHECK ADD CONSTRAINT [FK_Enrollment_Course] FOREIGN KEY([CourseID])
    REFERENCES [dbo].[Course] ([CourseID])
    GO

    ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_Course]
    GO

    -- Define the relationship between Enrollment and Person.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_Enrollment_Student]') AND type = 'F')
    ALTER TABLE [dbo].[Enrollment] WITH CHECK ADD CONSTRAINT [FK_Enrollment_Student] FOREIGN KEY([StudentPersonID])
    REFERENCES [dbo].[Person] ([PersonID])
    GO

    ALTER TABLE [dbo].[Enrollment] CHECK CONSTRAINT [FK_Enrollment_Student]
    GO

    -- Define the relationship between CourseInstructor and Course.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_CourseInstructor_Course]') AND type = 'F')
    ALTER TABLE [dbo].[CourseInstructor] WITH CHECK ADD CONSTRAINT [FK_CourseInstructor_Course] FOREIGN KEY([CourseID])
    REFERENCES [dbo].[Course] ([CourseID])
    GO

    ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Course]
    GO

    -- Define the relationship between CourseInstructor and Person.
    IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[FK_CourseInstructor_Person]') AND type = 'F')
    ALTER TABLE [dbo].[CourseInstructor] WITH CHECK ADD CONSTRAINT [FK_CourseInstructor_Person] FOREIGN KEY([PersonID])
    REFERENCES [dbo].[Person] ([PersonID])
    GO

    ALTER TABLE [dbo].[CourseInstructor] CHECK CONSTRAINT [FK_CourseInstructor_Person]
    GO

    -- Insert data into the Person table.
    USE School
    GO
    SET IDENTITY_INSERT dbo.Person ON
    GO
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (1, 'Abercrombie', 'Kim', '1995-03-11', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (2, 'Barzdukas', 'Gytis', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (3, 'Justice', 'Peggy', null, '2001-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (4, 'Fakhouri', 'Fadi', '2002-08-06', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (5, 'Harui', 'Roger', '1998-07-01', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (6, 'Li', 'Yan', null, '2002-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (7, 'Norman', 'Laura', null, '2003-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (8, 'Olivotto', 'Nino', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (9, 'Tang', 'Wayne', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (10, 'Alonso', 'Meredith', null, '2002-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (11, 'Lopez', 'Sophia', null, '2004-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (12, 'Browning', 'Meredith', null, '2000-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (13, 'Anand', 'Arturo', null, '2003-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (14, 'Walker', 'Alexandra', null, '2000-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (15, 'Powell', 'Carson', null, '2004-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (16, 'Jai', 'Damien', null, '2001-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (17, 'Carlson', 'Robyn', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (18, 'Zheng', 'Roger', '2004-02-12', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (19, 'Bryant', 'Carson', null, '2001-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (20, 'Suarez', 'Robyn', null, '2004-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (21, 'Holt', 'Roger', null, '2004-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (22, 'Alexander', 'Carson', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (23, 'Morgan', 'Isaiah', null, '2001-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (24, 'Martin', 'Randall', null, '2005-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (25, 'Kapoor', 'Candace', '2001-01-15', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (26, 'Rogers', 'Cody', null, '2002-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (27, 'Serrano', 'Stacy', '1999-06-01', null);
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (28, 'White', 'Anthony', null, '2001-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (29, 'Griffin', 'Rachel', null, '2004-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (30, 'Shan', 'Alicia', null, '2003-09-01');
    INSERT INTO dbo.Person (PersonID, LastName, FirstName, HireDate, EnrollmentDate)
    VALUES (31, 'Stewart', 'Jasmine', '2000-02-12', null);
    GO
    SET IDENTITY_INSERT dbo.Person OFF
    GO

    -- Insert data into the Course table.
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (4050, 'Math', '2007-09-01', '2007-09-01', 4);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (4051, 'Science', '2007-09-01', '2007-09-01', 4);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (3030, 'Art', '2007-09-01', '2007-09-01', 3);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (2021, 'Woodshop', '2007-09-01', '2007-09-01', 2);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (2022, 'Cooking', '2007-09-01', '2007-09-01', 2);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (4041, 'History', '2007-09-01', '2007-09-01', 4);
    INSERT INTO dbo.Course (CourseID, Title, StartDate, EndDate, Credits)
    VALUES (4042, 'Literature', '2007-09-01', '2007-09-01', 4);
    GO

    -- Insert data into the CourseInstructor table.
    SET IDENTITY_INSERT dbo.CourseInstructor ON
    GO
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (1, 4051, 1);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (2, 2021, 31);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (3, 2022, 5);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (4, 3030, 4);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (5, 4041, 27);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (6, 4042, 25);
    INSERT INTO dbo.CourseInstructor(CourseInstructorID, CourseID, PersonID)
    VALUES (7, 4050, 18);
    GO
    SET IDENTITY_INSERT dbo.CourseInstructor OFF
    GO

    -- Insert data into the Enrollment table.
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2021, 2, 4);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2021, 3, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2022, 6, 2.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2022, 7, 3.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2022, 8, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 9, 3.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 10, null);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 11, 2.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 12, null);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 13, 4);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4042, 14, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 15, 2.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 16, 2);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 17, null);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 19, 3.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 20, 4);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 21, 2);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4050, 22, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4041, 22, 3.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (2022, 22, 2.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 22, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 23, 1.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 24, 4);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 26, 3.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 28, 2.5);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (4051, 29, 3);
    INSERT INTO dbo.Enrollment (CourseID, StudentPersonID, Grade)
    VALUES (3030, 30, 3.5);
    GO

Next Steps/下一H

You have successfully created the School database. Next, you will create the class scheduling application:

你已经成功创建了学校数据库,下面你将创建ClassScheduling用程序。

Creating the Class Schedule Application/创建ClassSchedule应用程序.

See Also/请参考

作者: 徐少侠
出处: http://www.cnblogs.com/Chinese-xu/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
如有问题,可以通过 Chinese_Xu@126.com 联系我,非常感谢。

分享家:Addthis中文版

本文转自徐少侠博客园博客,原文链接:http://www.cnblogs.com/Chinese-xu/archive/2007/09/21/901232.html,如需转载请自行联系原作者
相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
2月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:详细步骤与最佳实践指南ali01n.xinmi1009fan.com
随着Web开发技术的不断进步,ASP.NET已成为一种非常流行的Web应用程序开发框架。在ASP.NET项目中,我们经常需要与数据库进行交互,特别是SQL数据库。本文将详细介绍如何在ASP.NET项目中连接SQL数据库,并提供最佳实践指南以确保开发过程的稳定性和效率。一、准备工作在开始之前,请确保您
284 3
|
1月前
|
数据库 C# 开发者
ADO.NET连接到南大通用GBase 8s数据库
ADO.NET连接到南大通用GBase 8s数据库
|
1月前
|
存储 缓存 NoSQL
2款使用.NET开发的数据库系统
2款使用.NET开发的数据库系统
|
29天前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
|
29天前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(下)
本文接续前文,深入讲解了在Windows环境下使用C#和ADO.NET操作南大通用GBase 8s数据库的方法。通过Visual Studio 2022创建项目,添加GBase 8s的DLL引用,并提供了详细的C#代码示例,涵盖数据库连接、表的创建与修改、数据的增删查改等操作,旨在帮助开发者提高数据库管理效率。
|
2月前
|
存储 NoSQL API
.NET NoSQL 嵌入式数据库 LiteDB 使用教程
.NET NoSQL 嵌入式数据库 LiteDB 使用教程~
|
2月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:实现过程与关键细节解析an3.021-6232.com
随着互联网技术的快速发展,ASP.NET作为一种广泛使用的服务器端开发技术,其与数据库的交互操作成为了应用开发中的重要环节。本文将详细介绍在ASP.NET中如何连接SQL数据库,包括连接的基本概念、实现步骤、关键代码示例以及常见问题的解决方案。由于篇幅限制,本文不能保证达到完整的2000字,但会确保
|
2月前
|
数据库
Admin.Net根据域名自动选择数据库
Admin.Net根据域名自动选择数据库
22 0
|
3月前
|
SQL 存储 关系型数据库
C#一分钟浅谈:使用 ADO.NET 进行数据库访问
【9月更文挑战第3天】在.NET开发中,与数据库交互至关重要。ADO.NET是Microsoft提供的用于访问关系型数据库的类库,包含连接数据库、执行SQL命令等功能。本文从基础入手,介绍如何使用ADO.NET进行数据库访问,并提供示例代码,同时讨论常见问题及其解决方案,如连接字符串错误、SQL注入风险和资源泄露等,帮助开发者更好地利用ADO.NET提升应用的安全性和稳定性。
359 6
|
4天前
|
存储 Oracle 关系型数据库
数据库传奇:MySQL创世之父的两千金My、Maria
《数据库传奇:MySQL创世之父的两千金My、Maria》介绍了MySQL的发展历程及其分支MariaDB。MySQL由Michael Widenius等人于1994年创建,现归Oracle所有,广泛应用于阿里巴巴、腾讯等企业。2009年,Widenius因担心Oracle收购影响MySQL的开源性,创建了MariaDB,提供额外功能和改进。维基百科、Google等已逐步替换为MariaDB,以确保更好的性能和社区支持。掌握MariaDB作为备用方案,对未来发展至关重要。
18 3