zoukankan      html  css  js  c++  java
  • <MySQL>MySQL创建表及相关约束

     题目:

    解答:

        第一个表创建:
            create table class(
                cid int not null auto_increment primary key,
                caption char(20) not null
                )engine=innodb default charset=utf8;
        插入数据:
             insert into class(caption) values('三年二班');
             insert into class(caption) values('一年三班');
             insert into class(caption) values('三年一班');
        第二个表创建:
        create table student(
            sid int not null auto_increment primary key,
            sname char(20) not null,
            gender char(20) not null,
            class_id int
            )engine=innodb default charset=utf8; 
        增加约束(外键):
            alter table student add constraint foreign key student(class_id) references class(cid);
        插入数据:
            insert student(sname,gender,class_id) values('钢弹','女',1);
            insert student(sname,gender,class_id) values('铁锤','女',1);
            insert student(sname,gender,class_id) values('山炮','男',2);
       第三个表创建:
        create table teacher(
            tid int not null auto_increment primary key,
            tname char(20) not null
            )engine=innodb default charset=utf8;   
        插入数据:
            insert teacher(tname) values('波多');
            insert teacher(tname) values('苍空');
            insert teacher(tname) values('饭岛');
       第四个表创建:
        create table course(
            cid int not null auto_increment primary key,
            cname char(20) not null,
            tearch_id int
            )engine=innodb default charset=utf8; 
        增加约束:
            alter table course add constraint foreign key course(tearch_id) references teacher(tid);    
        插入数据:
            insert course(cname,tearch_id) values('生物',1);             
            insert course(cname,tearch_id) values('体育',1);             
            insert course(cname,tearch_id) values('物理',2);     
        第五个表创建:
        create table score(
            sid int not null auto_increment primary key,
            student_id int not null,
            corse_id int not null,
            number int not null
            )engine=innodb default charset=utf8;        
        增加约束:
             alter table score add constraint foreign key score(student_id) references student(sid); 
             alter table score add constraint foreign key (corse_id) references course(cid); 
             可能存在的问题:
             假设第二句写成:alter table score add constraint foreign key score(corse_id) references course(cid); 
             会报错:ERROR 1061 (42000): Duplicate key name 'score'
             原因是:外键名称重复,在key后面增加表名会默认作为外键名,因此如果写2条,就会出现外键名称重复
             解决办法:删除key后面的表名,mysql会默认增加索引
        插入数据:
            insert score(student_id,corse_id,number) values(1,1,60);
            insert score(student_id,corse_id,number) values(1,2,59);
            insert score(student_id,corse_id,number) values(2,2,100);

     

      

  • 相关阅读:
    剑指Offer(链表)-从尾到头打印链表
    Java数据结构与算法-链表
    剑指Offer(数组)-数组中重复的数字
    剑指Offer(数组)-二维数组的查找
    Java冒泡排序法实现
    springMVC全局异常配置
    CookieUtil工具类
    算法
    Java
    算法
  • 原文地址:https://www.cnblogs.com/shuimohei/p/10259777.html
Copyright © 2011-2022 走看看