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

來源:互聯網
上載者:User

標籤: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主鍵、外鍵、自增

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.