標籤:values div 自增 cap number teacher bsp char cas
主鍵:primary key一張表有且只有一個主鍵,索引值可以唯一。可以設定主鍵為自增。
外鍵:表示了兩個關係之間的相關聯絡。以另一個關係的外鍵作主關鍵字的表被稱為主表,具有此外鍵的表被稱為主表的從表。
自增:create table t1(id int primary key auto_increment,name char(10));
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表的cidinsert 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);
【day2】mysql主鍵、外鍵、自增