SQL规范化

发布于 2024-10-11 14:29:54 字数 275 浏览 2 评论 0原文

现在,我有一张表:

Id  -  CollegeName   -   CourseName

这张表没有标准化,所以我每 1 所大学都有很多课程
我需要将其规范化为两个表:

Colleges:  CollegeID  -  CollegeName  
Courses:   CourseID  - CollegeID  - CourseName 

有没有简单的方法可以做到这一点?
谢谢

right now, i have a table:

Id  -  CollegeName   -   CourseName

this table is not normalized so i have many Courses for every 1 College
I need to normalize this into two tables:

Colleges:  CollegeID  -  CollegeName  
Courses:   CourseID  - CollegeID  - CourseName 

Is there an easy way to do this?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

殊姿 2024-10-18 14:29:54
CREATE TABLE dbo.College
(
  CollegeId     int IDENTITY(1, 1) NOT NULL PRIMARY KEY,
  CollegeName   nvarchar(100) NOT NULL
)

CREATE TABLE dbo.Course
(
  CourseId      int IDENTITY(1, 1) NOT NULL PRIMARY KEY,
  CollegeId     int NOT NULL,
  CourseName    nvarchar(100) NOT NULL
)

ALTER TABLE dbo.Course
  ADD CONSTRAINT FK_Course_College FOREIGN KEY (CollegeId)
  REFERENCES dbo.College (CollegeId)

--- add colleges
INSERT INTO dbo.College (CollegeName)
SELECT DISTINCT CollegeName FROM SourceTable

--- add courses
INSERT INTO dbo.Course (CollegeId, CourseName)
SELECT
  College.CollegeId,
  SourceTable.CourseName
FROM
  SourceTable
INNER JOIN
  dbo.College ON SourceTable.CollegeName = College.CollegeName
CREATE TABLE dbo.College
(
  CollegeId     int IDENTITY(1, 1) NOT NULL PRIMARY KEY,
  CollegeName   nvarchar(100) NOT NULL
)

CREATE TABLE dbo.Course
(
  CourseId      int IDENTITY(1, 1) NOT NULL PRIMARY KEY,
  CollegeId     int NOT NULL,
  CourseName    nvarchar(100) NOT NULL
)

ALTER TABLE dbo.Course
  ADD CONSTRAINT FK_Course_College FOREIGN KEY (CollegeId)
  REFERENCES dbo.College (CollegeId)

--- add colleges
INSERT INTO dbo.College (CollegeName)
SELECT DISTINCT CollegeName FROM SourceTable

--- add courses
INSERT INTO dbo.Course (CollegeId, CourseName)
SELECT
  College.CollegeId,
  SourceTable.CourseName
FROM
  SourceTable
INNER JOIN
  dbo.College ON SourceTable.CollegeName = College.CollegeName
水溶 2024-10-18 14:29:54

如果您使用 Colleges.CollegeID 和 Courses.CourseID 作为自动编号字段创建 2 个新表,则可以使用:

INSERT INTO Colleges (CollegeName)
  SELECT DISTINCT CollegeName 
    FROM OLdTable ;

INSERT INTO Courses (CollegeID, CourseName)
  SELECT Colleges.CollegeID, OldTable.CourseName 
    FROM OldTable
      JOIN Colleges
        ON OldTable.CollegeName = Colleges.CollegeName ;

If you create the 2 new tables with Colleges.CollegeID and Courses.CourseID as auto numbered fields, you can go with :

INSERT INTO Colleges (CollegeName)
  SELECT DISTINCT CollegeName 
    FROM OLdTable ;

INSERT INTO Courses (CollegeID, CourseName)
  SELECT Colleges.CollegeID, OldTable.CourseName 
    FROM OldTable
      JOIN Colleges
        ON OldTable.CollegeName = Colleges.CollegeName ;
挽清梦 2024-10-18 14:29:54

我同意@Andomar 的第一条评论:删除看似多余的 Id 列,并且您的 CollegeName, CourseName 表已经在 5NF 中。

我怀疑您需要的是一个进一步的表格来给出课程属性,以便您可以模拟这样一个事实:例如,杜伦大学的学士学位。计算机科学学士学位与哈佛大学计算机科学学士学位相当(通过属性“计算专业”、“本科”、“国家=美国”、“国家=英国”等)。

I agreed with @Andomar's first comment: remove the seemingly redundant Id column and your CollegeName, CourseName table is already in 5NF.

What I suspect you need is a further table to give courses attributes so that you can model the fact that, say, Durham University's B.Sc. in Computing Science is comparable with Harvard's A.B. in Computer Science (via attributes 'computing major', 'undergraduate', 'country=US, 'country=UK', etc).

山色无中 2024-10-18 14:29:54

当然。

创建一个 College 表,其中包含一个 College_id(主键)列和一个用作唯一索引列的 College_name 列。

只需引用 Course 表中的 College_id 列,而不是 College_name 。

Sure.

Create a College table with a college_id (primary key) column, and a college_name column which is used as a unique index column.

Just refer to the college_id column, not college_name, in the Course table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文