Course课程表 Cpno先行课 Ccredit学分 Cno课号
Sc表(选课表)Sno学号 Cno课号 Grade成绩
Student表 Sno学号 Sname姓名 Ssex性别 Sage年龄 Sdept 所在系

主键(primary key)是表(也称关系)中的一个或多个字段(也称属性),它的值用于唯一的标识表中的某一条记录(行)。
外键:foreign key (F) reference S(K)
学生-课程数据库st
学生表:Student(Sno,Sname,Ssex,Sage,Sdept)
课程表:Course(Cno,Cname,Cpno,Ccredit)
学生选课表:SC(Sno,Cno,Grade)
mysql -uroot
create database st;
use st;
create table student
-> (
-> sno char(9) primary key,
-> sname char(20),
-> ssex char(1),
-> sage smallint,
-> sdept char(20)
-> );
create table course
-> (
-> cno char(4) primary key,
-> cname char(20),
-> cpno char(4),
-> ccredit smallint
-> );
create table sc
-> (
-> sno char(9),
-> cno char(4),
-> grade int,
-> primary key(sno,cno)
-> );
alter table sc add foreign key (sno) references student(sno);
alter table sc add foreign key (cno) references course(cno);
alter table course add foreign key (cpno) references course(cno);
show create table student \G
show create table course \G
show create table sc \G

实际操作
Microsoft Windows [版本 10.0.17763.316]
© 2018 Microsoft Corporation。保留所有权利。

C:\Users\yang>mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.48 MySQL Community Server (GPL)

Copyright © 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> create database st;
Query OK, 1 row affected (0.10 sec)

mysql> use st;
Database changed
mysql> create table student
-> (
-> sno char(9) primary key,
-> sname char(20),
-> ssex char(1),
-> sage smallint,
-> sdept char(20)
-> );
Query OK, 0 rows affected (0.27 sec)

mysql> create table course
-> (
-> cno char(4) primary key,
-> cname char(20),
-> cpno char(4),
-> ccredit smallint
-> );
Query OK, 0 rows affected (0.20 sec)

mysql> create table sc
-> (
-> sno char(9),
-> cno char(4),
-> grade int,
-> primary key(sno,cno)
-> );
Query OK, 0 rows affected (0.14 sec)

mysql> alter table sc add foreign key (sno) references student(sno);
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table sc add foreign key (cno) references coursse(cno);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> alter table course add foreign key (cno) references coursse(cno);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> alter table sc add foreign key (cno) references course(cno);
Query OK, 0 rows affected (0.44 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> alter table course add foreign key (cpno) references course(cno);
Query OK, 0 rows affected (0.36 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> show create table student \G
*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE student (
sno char(9) NOT NULL,
sname char(20) DEFAULT NULL,
ssex char(1) DEFAULT NULL,
sage smallint(6) DEFAULT NULL,
sdept char(20) DEFAULT NULL,
PRIMARY KEY (sno)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.02 sec)

mysql> show create table course \G
*************************** 1. row ***************************
Table: course
Create Table: CREATE TABLE course (
cno char(4) NOT NULL,
cname char(20) DEFAULT NULL,
cpno char(4) DEFAULT NULL,
ccredit smallint(6) DEFAULT NULL,
PRIMARY KEY (cno),
KEY cpno (cpno),
CONSTRAINT course_ibfk_1 FOREIGN KEY (cpno) REFERENCES course (cno)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

mysql> show create table sc \G
*************************** 1. row ***************************
Table: sc
Create Table: CREATE TABLE sc (
sno char(9) NOT NULL DEFAULT ‘’,
cno char(4) NOT NULL DEFAULT ‘’,
grade int(11) DEFAULT NULL,
PRIMARY KEY (sno,cno),
KEY cno (cno),
CONSTRAINT sc_ibfk_1 FOREIGN KEY (sno) REFERENCES student (sno),
CONSTRAINT sc_ibfk_2 FOREIGN KEY (cno) REFERENCES course (cno)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)


MySQL简单案例之创建学生表、课程表和选课表相关推荐

  1. MYSQL创建课程表course_MySQL简单案例之创建学生表、课程表和选课表

    Course课程表 Cpno先行课 Ccredit学分 Cno课号 Sc表(选课表)Sno学号 Cno课号 Grade成绩 Student表 Sno学号 Sname姓名 Ssex性别 Sage年龄 S ...

  2. SQL数据库创建学生、教师、选课表

    SQL数据库创建学生.教师.选课表 创建学生表 create table student (sno char(14) primary key,sname char (10) not null,ssex ...

  3. MySQL练习(学生表,课程表,选课表)超详解

    mysql练习 一.题目: 二.创建学生,课程,选课表并插入数据 三.习题答案 环境: win10系统,MySQL数据库 一.题目: 已知关系: S(Sno,Sname,Sage,Ssex,Sdept ...

  4. 数据库实验报告 创建学生关系数据表、课程表、选课表 SQL Kingbase

    一.实验题目 已有条件:Kingbase数据库软件包. 要求:请安装Kingbase数据库软件,在数据库软件中创建一个自己名字的模式,在该模式下创建学生关系数据表.课程表.选课表. 插入一些数据,尝试 ...

  5. mysql用sql语句将表中学生_用sql语句创建学生表如何做

    在数据库中使用SQL语句创建学生表代码如下:( 学号 char(12) primary key, 姓名 char(6) not null, 性别 char(2) check(性别 IN ('男','女 ...

  6. mysql 创建学生表

    – 如果存在名为school的数据库就删除它 drop database if exists school; – 创建名为school的数据库并设置默认字符集为utf8 create database ...

  7. mysql语句创建学生表_用sql语句创建学生表的方法是什么

    用sql语句创建学生表的方法是什么 发布时间:2020-08-25 11:53:33 来源:亿速云 阅读:123 作者:小新 小编给大家分享一下用sql语句创建学生表的方法是什么,希望大家阅读完这篇文 ...

  8. html 数据库 编写学生表,用sql语句创建学生表如何做

    在数据库中使用SQL语句创建学生表代码如下:( 学号 char(12) primary key, 姓名 char(6) not null, 性别 char(2) check(性别 IN ('男','女 ...

  9. 数据库实验-创建创建学生表

    (1)在表空间TableSpace_XXX下创建学生表(student).学生选课表(SC).课程表(course) 学生表:Student_XXX,属性为:(Sno 学号, Sname 姓名, Ss ...

最新文章

  1. PHP - 如何实现跨域
  2. 项目开发过程中的收获与思考
  3. Visual Studio 2017新版发布,极大提高开发效率丨附下载
  4. @NotNull-JSR-303验证
  5. mybatis的优缺点
  6. 英特尔CFO:裁员为加速转型 是艰难但正确的决定
  7. 优化SQL步骤——查看SQL执行频率 || 定位低效率执行SQL
  8. uni app 调用网络打印机_前端工程师 | 原生小程序坑点:uni-app到底好用在哪里?...
  9. 简述Linux虚拟内存管理
  10. 三路合并 —— Git 学习笔记 17
  11. 织梦cms仿知名吧站长导航网站模板
  12. softmax回归的从零开始实现
  13. angularJs解决跨域问题-最简单的完美实例
  14. innodb redo buffer的认识
  15. 【图解线性代数】第一章——线性代数的几何意义导读(思维导图)
  16. PSD是什么文件格式
  17. 2018版 主流SDR设备横向比较
  18. SuperData上线VR数据平台,做行业发展的“指明灯”
  19. 大话设计模式策略模式_多种方法实现商场促销
  20. jhipster快速入门指南

热门文章

  1. 计算机word应用模块三,计算机应用基础信息化教程模块三 Word 2010文字处理软件.pptx...
  2. 韩国首尔旅游攻略(组图)
  3. CSUST Online Judge4031 最喜欢的数
  4. 加密PDF转换为word的方法
  5. 最新掌上题库微信小程序源码下载,修复登录接口,支持在线考试,自定义导入考题
  6. Java中的字节流和字符流区别
  7. python中glob模块怎么下_如何在Python中使用glob.glob模块搜索子文件夹?
  8. c++中的字符串大小排序
  9. 未来5年热门的10大就业方向,你处在哪个行业?
  10. 详解SVM支持向量机算法(四:坐标上升和SMO算法)