首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ER图转换为sql中的表和关系

将ER图转换为sql中的表和关系
EN

Database Administration用户
提问于 2013-08-07 12:27:22
回答 1查看 7.5K关注 0票数 2

部门:1:m

分支:课程是1:m

分支:学生是1:m

分支机构:申请人为M:n

申请人:学生是1:1

这些是ER图中规定的条件。

这是ER图,为此必须在实现所有约束的SQL代码中创建表。我制作了表格并尝试通过外键实现所有的关系,我只想确认这些表是否正确。

1)部门表:

代码语言:javascript
复制
create table department(dpet_id number primary key, dept_name varchar2(15)
not null);

2)分支表:

代码语言:javascript
复制
create table branch(branch_id varchar2(5) primary key, electives varchar2(10),
dept_id number references department(dept_id));

3)课程表:

代码语言:javascript
复制
create table course(course_id number primary key, course_name varchar2(10)
not null,branch_id varchar2(5) references branch(branch_id));

4)学生桌:

代码语言:javascript
复制
create table student(stud_id number primary key, stud_name varchar2(30) not null,
branch_id varchar2(5) references branch(branch_id);

5)申请人表:

代码语言:javascript
复制
create table applicant(app_id number primary key, stud_id number constraint fk
references student(stud_id) constraint stu_unq unique);

6) Applicant_branch表:

代码语言:javascript
复制
create table applicant_branch(app_id number references applicant(app_id),
branch_id varchar2(5) references branch(branch_id));

这些表是否符合ER图?

我试着给出以上ER图的图,给出基数条件。现在请告诉我我的SQL代码中是否有错误。

EN

回答 1

Database Administration用户

回答已采纳

发布于 2013-08-07 12:44:59

Student - Applicant关系是1:1,您的实现是正确的。另一种方法是将app_id (或stud_id)从Applicant表中移除,并将(一个和唯一)列用作Primary KeyForeign Key约束(参见下面的代码)。

我从您的代码中假设可以选择学生作为申请者。如果情况正好相反(一些申请者被选为学生),则必须反转外键约束。

您还将几个列(在外键约束中使用)定义为NULL。不确定如何解释图表。我想这些应该是NOT NULL。否则,course.branch_id (例如为null )意味着您可能有一个与任何分支完全无关的课程。

我还会命名(FK)约束,因为这是作业,所以我会根据提供的图表命名它们。

NOT NULL添加到主键列也不错。它不会改变创建的表中的任何内容--直到您决定更改主键并忘记添加null约束:

代码语言:javascript
复制
create table department
 (dept_id number not null primary key, 
  dept_name varchar2(15) not null
 );

create table branch
 (branch_id varchar2(5) not null primary key, 
  electives varchar2(10),
  dept_id number not null,
  constraint Department_Has_Branches
    foreign key (dept_id)
    references department(dept_id)
 );

create table course
 (course_id number not null primary key,
  course_name varchar2(10) not null,
  branch_id varchar2(5) not null,
  constraint Branch_Offers_Courses
    foreign key (branch_id)
    references branch(branch_id)
 );

create table student
 (stud_id number not null primary key, 
  stud_name varchar2(30) not null,
  branch_id varchar2(5) not null,
  constraint Student_BelongsTo_Branch
    foreign key (branch_id)
    references branch(branch_id)
 );

create table applicant
 (app_id number not null primary key,      -- stud_id removed           
  constraint Student_SelectedAs_Applicant
    foreign key (app_id)                   -- app_id used in the FK to Student table
    references student(stud_id)
);

create table applicant_AppliesFor_branch
 (app_id number not null, 
  branch_id varchar2(5) not null,
  primary key (app_id, branch_id),
  constraint Student_AppliesFor_Branch
    foreign key (app_id)
    references applicant(app_id),
  constraint Branch_AppliedBy_Student
    foreign key (branch_id)
    references branch(branch_id)
);

关于Oracle,请参见:

在Oracle中显示表的所有约束名

票数 2
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/47707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档