数据的定义与数据更新的插入
#创建st库
create database st
use st
#建student
CREATE TABLE student
(
Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
#建Course表
CREATE TABLE Course(
Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40) NOT NULL,
Cpno CHAR(4),
Ccredit SMALLINT,
FOREIGN KEY(Cpno) REFERENCES Course(cno)
);
#建sc表
CREATE TABLE sc
(
Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY(Sno,Cno),
FOREIGN KEY(Sno) REFERENCES student(Sno),
FOREIGN KEY(Cno) REFERENCES course(Cno)
)
#向student表中插入数据
insert into student(sno,sname,ssex,sage,sdept)
values(‘201215121’,‘李勇’,‘男’,20,‘CS’);
insert into student(sno,sname,ssex,sage,sdept)
values(‘201215122’,‘刘晨’,‘女’,19,‘CS’);
insert into student(sno,sname,ssex,sage,sdept)
values(‘201215125’,‘王敏’,‘女’,18,‘A’);
insert into student(sno,sname,ssex,sage,sdept)
values(‘201215124’,‘张立’,‘男’,19,‘IS’);
#向course表中插入数据
insert into course(cno,cname,cpno,ccredit)
values(‘2’,‘数学’,null,2);
insert into course(cno,cname,cpno,ccredit)
values(‘6’,‘数据处理’,null,2);
insert into course(cno,cname,cpno,ccredit)
values(‘4’,‘操作系统’,‘6’,3);
insert into course(cno,cname,cpno,ccredit)
values(‘7’,‘PASCAL语言’,‘6’,4);
insert into course(cno,cname,cpno,ccredit)
values(‘5’,‘数据结构’,‘7’,4);
insert into course(cno,cname,cpno,ccredit)
values(‘1’,‘高等数学’,‘5’,4);
insert into course(cno,cname,cpno,ccredit)
values(‘3’,‘信息系统’,‘1’,4);
#向sc表中插入数据
insert into sc(sno,cno,grade)
values(‘201215121’,‘1’,92);
insert into sc(sno,cno,grade)
values(‘201215121’,‘2’,85);
insert into sc(sno,cno,grade)
values(‘201215121’,‘3’,88);
insert into sc(sno,cno,grade)
values(‘201215122’,‘2’,90);
insert into sc(sno,cno,grade)
values(‘201215122’,‘3’,80);
#显示student表
mysql> select * from student;
±----------±-------±-----±-----±------+
| Sno | Sname | Ssex | Sage | Sdept |
±----------±-------±-----±-----±------+
| 201215121 | 李勇 | 男 | 20 | CS |
| 201215122 | 刘晨 | 女 | 19 | CS |
| 201215123 | 王小敏 | 女 | 18 | MA |
| 201215124 | 张立 | 男 | 19 | IS |
| 201215125 | 王敏 | 女 | 18 | A |
±----------±-------±-----±-----±------+
5 rows in set (0.00 sec)