zoukankan      html  css  js  c++  java
  • mysql 建立表之间关系 练习 2

    创建数据库db6

    create database db6 charset=utf8;

     user db6;

    # 创建班级表

    mysql> create table class(cid int primary key auto_increment,caption varchar(50) not null unique);
    Query OK, 0 rows affected (0.15 sec)
    
    mysql> select * from class;
    Empty set (0.00 sec)
    
    mysql> insert into class(caption) values('三年二班'),('一年三班'),('三年一班');
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from class;
    +-----+--------------+
    | cid | caption      |
    +-----+--------------+
    |   2 | 一年三班     |
    |   3 | 三年一班     |
    |   1 | 三年二班     |
    +-----+--------------+
    3 rows in set (0.00 sec)

    # 创建老师表

    mysql> create table teacher(tid int primary key auto_increment,tname varchar(50) not null);
    Query OK, 0 rows affected (0.11 sec)
    
    
    mysql> insert into teacher(tname) values('李老师'),('吴老师'),('陈老师');
    Query OK, 3 rows affected (0.12 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from teacher;
    +-----+-----------+
    | tid | tname     |
    +-----+-----------+
    |   1 | 李老师    |
    |   2 | 吴老师    |
    |   3 | 陈老师    |
    +-----+-----------+
    3 rows in set (0.00 sec)

    多对一关系建表

    # 学生表与班级表建立关系

     

    创建学生表

    mysql> create table student(
    sid int primary key auto_increment,
    sname varchar(20) not null,
    gender enum('','') not null,
    class_id int not null,
    foreign key(class_id) references class(cid) 
    on delete cascade 
    on update cascade);
    Query OK, 0 rows affected (0.06 sec)
    mysql> insert into student(sname,gender,class_id) values('小红','',1),('小李','',1),('小刚','',2);
    Query OK, 3 rows affected (0.36 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from student;
    +-----+--------+--------+----------+
    | sid | sname  | gender | class_id |
    +-----+--------+--------+----------+
    |   1 | 小红   ||        1 |
    |   2 | 小李   ||        1 |
    |   3 | 小刚   ||        2 |
    +-----+--------+--------+----------+
    3 rows in set (0.00 sec)

    # 老师表和课程表建立关系

    创建课程表

    mysql> create table course(
    cid int primary key auto_increment,
    cname varchar(20) not null,
    tearch_id int not null,
    foreign key(tearch_id) references teacher(tid)
    on delete cascade on update cascade); Query OK, 0 rows affected (0.12 sec

    插入记录 并查询

    mysql> insert into course(cname,tearch_id) values('生物',1),('体育',1),('物理',2);
    Query OK, 3 rows affected (0.11 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from course;
    +-----+--------+-----------+
    | cid | cname  | tearch_id |
    +-----+--------+-----------+
    |   1 | 生物   |         1 |
    |   2 | 体育   |         1 |
    |   3 | 物理   |         2 |
    +-----+--------+-----------+
    3 rows in set (0.00 sec)

    建立多对多关系

    # 建立一张score表 与 学生表 课程表建立关系

    mysql> create table score(
    sid int not null unique auto_increment,
    student_id int not null,
    corse_id int not null,
    number char(16) not null,
    foreign key(student_id) references student(sid) 
    on delete cascade 
    on update cascade,
    foreign key(corse_id) references course(cid) 
    on delete cascade 
    on update cascade,
    primary key(student_id,corse_id)
    );

    插入记录

    mysql> insert into score(student_id,corse_id,number) values(1,1,'60'),(1,2,'59'),(2,2,'100');
    Query OK, 3 rows affected (0.70 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from score;
    +-----+------------+----------+--------+
    | sid | student_id | corse_id | number |
    +-----+------------+----------+--------+
    |   1 |          1 |        1 | 60     |
    |   2 |          1 |        2 | 59     |
    |   3 |          2 |        2 | 100    |
    +-----+------------+----------+--------+
    3 rows in set (0.00 sec)
  • 相关阅读:
    微信小程序之 3d轮播(swiper来实现)
    微信小程序之canvas 文字断行和省略号显示
    iframe 自适应高度、父子页面传值、回调
    微信小程序之动态获取元素宽高
    微信小程序之自定义select下拉选项框组件
    使用text-align:justify,让内容两端对齐,兼容IE及主流浏览器的方法
    bind,apply,call,caller,callee还傻傻分不清楚?
    JVM从零学习(三)虚拟机栈
    JVM从零学习(二)PC Register程序计数器
    JVM从零学习(一)运行时数据区及线程
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/9868437.html
Copyright © 2011-2022 走看看