2018-08-31【数据库】PowerDesigner建表

这个东西的好处,是可以清晰的看出图表之间的关系。
https://blog.csdn.net/idomyway/article/details/78658931

image.png
/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2018/8/31 18:10:58                           */
/*==============================================================*/


drop table if exists class_info;

drop table if exists student_info;

drop table if exists student_teacher_r;

drop table if exists teacher_info;

/*==============================================================*/
/* Table: class_info                                            */
/*==============================================================*/
create table class_info
(
   class_id             int not null auto_increment,
   class_name varchar(20) not null,
   class_content int,
   class_desc varchar(100),
   primary key(class_id)
);

/*==============================================================*/
/* Table: student_info                                          */
/*==============================================================*/
create table student_info
(
   stu_id               int not null auto_increment,
   class_id             int,
   stu_name varchar(20) not null,
   stu_sex bool,
   stu_school varchar(20),
   primary key(stu_id)
);

/*==============================================================*/
/* Table: student_teacher_r                                     */
/*==============================================================*/
create table student_teacher_r
(
   student_teacher_r    int not null auto_increment,
   stu_id               int,
   teacher_id           int,
   primary key (student_teacher_r)
);

/*==============================================================*/
/* Table: teacher_info                                          */
/*==============================================================*/
create table teacher_info
(
   teacher_id           int not null,
   teacher_name varchar(20) not null,
   teacher_sex bool,
   primary key(teacher_id)
);

alter table student_info add constraint FK_class_student_r foreign key(class_id)
      references class_info(class_id) on delete restrict on update restrict;

alter table student_teacher_r add constraint FK_ref1 foreign key(stu_id)
      references student_info(stu_id) on delete restrict on update restrict;

alter table student_teacher_r add constraint FK_ref2 foreign key(teacher_id)
      references teacher_info(teacher_id) on delete restrict on update restrict;

你可能感兴趣的:(2018-08-31【数据库】PowerDesigner建表)