StudentDB

简单的三个实体 学生、老师、班级,如下图

 use StudentDB

create table Classes (
   ClassesId            int                  identity,
   ClassesName          nvarchar(25)         null,
   CreateTime           date                 null,
   constraint PK_CLASSES primary key (ClassesId)
)
go

/*==============================================================*/
/* Table: Students                                              */
/*==============================================================*/
create table Students (
   StudentId            int                  identity,
   ClassesId            int                  null,
   StudentName          nvarchar(25)         null,
   Age                  int                  null,
   Email                nvarchar(25)         null,
   Sex                  bit                  null,
   RegistTime           date                 null,
   Isleft               bit                  null,
   constraint PK_STUDENTS primary key (StudentId)
)
go

/*==============================================================*/
/* Table: Teachers                                              */
/*==============================================================*/
create table Teachers (
   TeacherId            int                  identity,
   TeacherName          nvarchar(25)         null,
   Age                  int                  null,
   constraint PK_TEACHERS primary key (TeacherId)
)
go

/*==============================================================*/
/* Table: Teaching                                              */
/*==============================================================*/
create table Teaching (
   StudentId            int                  not null,
   TeacherId            int                  not null,
   constraint PK_TEACHING primary key (StudentId, TeacherId)
)
go

alter table Students
   add constraint FK_STUDENTS_REFERENCE_CLASSES foreign key (ClassesId)
      references Classes (ClassesId)
go

alter table Teaching
   add constraint FK_TEACHING_REFERENCE_STUDENTS foreign key (StudentId)
      references Students (StudentId)
go

alter table Teaching
   add constraint FK_TEACHING_REFERENCE_TEACHERS foreign key (TeacherId)
      references Teachers (TeacherId)
go

 

 

 

阅读更多
个人分类: DataBase
想对作者说点什么? 我来说一句

建立基本表和数据完整性

2011年12月29日 84KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭