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

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笔记,mysql)