主键:primary key一张表有且只有一个主键,键值可以唯一。可以设置主键为自增。

外键:表示了两个关系之间的相关联系。以另一个关系的外键作主关键字的表被称为主表,具有此外键的表被称为主表的从表。

自增:create table t1(id int primary key auto_increment,name char(10));

 

mysql 设置主键自增sql mysql主键自增语句_mysql 设置主键自增sql



create table class(cid int primary key auto_increment,caption char(20) not null);  #创建class表
  
insert into class(caption) values('三年二班'),('一年三班'),('三年一班');          #插入班级数据
  
create table student(sid int primary key auto_increment,sname char(20) not null,gender enum('男','女'),class_id int not null,constraint foreign key(class_id) references class(cid) on delete cascade on update cascade)        #创建学生表,班级id关联到class表的cid

insert into student(sname,gender,class_id) values('钢蛋','女',1),('钢锤','女',1),('山炮','男',2) #插入数据

create table teacher(tid int primary key auto_increment,tname char(20) not null);      

insert into teacher(tname) values('波多'),('苍井'),('饭岛爱');

create table course(cid int primary key auto_increment,cname char(20) not null,teach_id int not null,constraint foreign key(teach_id) references teacher(tid) on delete cascade on update cascade);

insert into course(cname,teach_id) values('生物',1),('体育',1),('物理',2);

create table score(sid int primary key auto_increment,student_id int not null,course_id int not null,number int(3) not null,constraint foreign key(student_id) references student(sid) on delete cascade on update cascade,constraint foreign key(course_id) references course(cid) on delete cascade on update cascade);

insert into course values(1,1,1,60),(2,1,2,59),(3,2,2,100);