码迷,mamicode.com
首页 > 数据库 > 详细

【day2】mysql主键、外键、自增

时间:2017-09-06 20:45:10      阅读:286      评论:0      收藏:0      [点我收藏+]

标签: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表的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);

 

【day2】mysql主键、外键、自增

标签:values   div   自增   cap   number   teacher   bsp   char   cas   

原文地址:http://www.cnblogs.com/weiyumangmang/p/7486785.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!