MYsql建立学生成绩表
生活随笔
收集整理的這篇文章主要介紹了
MYsql建立学生成绩表
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
mysql> show databases;mysql> create database gradesystem;mysql> use gradesystem;mysql> create table tb_class-> (-> clid int not null comment '班級(jí)編號(hào)',-> clname varchar(20) not null comment '班級(jí)名稱(chēng)',-> primary key(clid)-> );mysql> show tables;
+-----------------------+
| Tables_in_gradesystem |
+-----------------------+
| tb_class |
+-----------------------+mysql> create table tb_student-> (-> stuid int not null,-> stuname varchar(4) not null,-> clid int not null comment '班級(jí)編號(hào)',-> primary key(stuid)-> );mysql> show tables;
+-----------------------+
| Tables_in_gradesystem |
+-----------------------+
| tb_class |
| tb_student |
+-----------------------+mysql> create table tb_course-> (-> cid int not null comment '課程編號(hào)',-> cname varchar(20) not null comment '課程名稱(chēng)',-> primary key (cid)-> );mysql> show tables;
+-----------------------+
| Tables_in_gradesystem |
+-----------------------+
| tb_class |
| tb_course |
| tb_student |
+-----------------------+mysql> create table tb_mark(-> mid int not null,-> clid int not null comment '班級(jí)編號(hào)',-> stuid int not null,-> cid int not null comment '課程編號(hào)',-> score decimal(4,1) comment '成績(jī)',-> primary key(mid)-> );mysql> show tables;
+-----------------------+
| Tables_in_gradesystem |
+-----------------------+
| tb_class |
| tb_course |
| tb_mark |
| tb_student |
+-----------------------+//ALTER TABLE <數(shù)據(jù)表名> add constraint FK_主表_從表 foreign key (外鍵字段) references 主表(主表主鍵字段);
//ALTER TABLE <數(shù)據(jù)表名> ADD CONSTRAINT <唯一約束名> UNIQUE(<列名>);mysql> alter table tb_student add constraint uni_id unique(stuid);mysql> alter table tb_student add constraint fk_student_clid foreign key(clid) references tb_class(clid);mysql> alter table tb_mark add constraint fk_mark_clid foreign key(clid) referen
ces tb_class(clid);mysql> alter table tb_mark add constraint fk_mark_stuid foreign key(stuid) refer
ences tb_student(stuid);mysql> alter table tb_mark add constraint fk_mark_cid foreign key(cid) reference
s tb_course(cid);mysql> insert into tb_course(cid,cname)values-> (1,'C++程序設(shè)計(jì)'),-> (2,'多媒體技術(shù)'),-> (3,'大學(xué)英語(yǔ)'),-> (4,'高等數(shù)學(xué)'),-> (5,'大學(xué)體育'),-> (6,'馬克思主義政治經(jīng)濟(jì)學(xué)');mysql> insert into tb_class(clid,clname)values-> (1,'一班'),-> (2,'二班'),-> (3,'三班'),-> (4,'四班'),-> (5,'五班'),-> (6,'六班'),-> (7,'七班'),-> (8,'八班'),-> (9,'九班'),-> (10,'十班');mysql> insert into tb_student(stuid,stuname,clid)values-> (001,'張三',1),-> (002,'李四',1),-> (003,'王二',1);//如果該字段不是主鍵,需要先設(shè)置該字段為主鍵://alter table 表名 add primary key(字段名);//修改字段為自動(dòng)增長(zhǎng)//alter table 表名 change 字段名 字段名 字段類(lèi)型 auto_increment;mysql> alter table tb_mark change mid mid int not null auto_increment;mysql> insert into tb_mark(clid,stuid,cid,score)values-> (1,001,1,80),-> (1,001,2,88),-> (1,001,3,71),-> (1,001,4,60),-> (1,001,5,66),-> (1,001,6,91),-> (1,002,1,77),-> (1,002,2,73),-> (1,002,3,84),-> (1,002,4,93),-> (1,002,5,64),-> (1,002,6,91),-> (1,003,1,97),-> (1,003,2,89),-> (1,003,3,81),-> (1,003,4,79),-> (1,003,5,93),-> (1,003,6,88);mysql> show tables;
+-----------------------+
| Tables_in_gradesystem |
+-----------------------+
| tb_class |
| tb_course |
| tb_mark |
| tb_student |
+-----------------------+mysql> select * from tb_class;
+------+--------+
| clid | clname |
+------+--------+
| 1 | 一班 |
| 2 | 二班 |
| 3 | 三班 |
| 4 | 四班 |
| 5 | 五班 |
| 6 | 六班 |
| 7 | 七班 |
| 8 | 八班 |
| 9 | 九班 |
| 10 | 十班 |
+------+--------+mysql> select * from tb_course;
+-----+--------------------------------+
| cid | cname |
+-----+--------------------------------+
| 1 | C++程序設(shè)計(jì) |
| 2 | 多媒體技術(shù) |
| 3 | 大學(xué)英語(yǔ) |
| 4 | 高等數(shù)學(xué) |
| 5 | 大學(xué)體育 |
| 6 | 馬克思主義政治經(jīng)濟(jì)學(xué) |
+-----+--------------------------------+mysql> select *from tb_mark;
+-----+------+-------+-----+-------+
| mid | clid | stuid | cid | score |
+-----+------+-------+-----+-------+
| 2 | 1 | 1 | 1 | 80.0 |
| 3 | 1 | 1 | 2 | 88.0 |
| 4 | 1 | 1 | 3 | 71.0 |
| 5 | 1 | 1 | 4 | 60.0 |
| 6 | 1 | 1 | 5 | 66.0 |
| 7 | 1 | 1 | 6 | 91.0 |
| 8 | 1 | 2 | 1 | 77.0 |
| 9 | 1 | 2 | 2 | 73.0 |
| 10 | 1 | 2 | 3 | 84.0 |
| 11 | 1 | 2 | 4 | 93.0 |
| 12 | 1 | 2 | 5 | 64.0 |
| 13 | 1 | 2 | 6 | 91.0 |
| 14 | 1 | 3 | 1 | 97.0 |
| 15 | 1 | 3 | 2 | 89.0 |
| 16 | 1 | 3 | 3 | 81.0 |
| 17 | 1 | 3 | 4 | 79.0 |
| 18 | 1 | 3 | 5 | 93.0 |
| 19 | 1 | 3 | 6 | 88.0 |
+-----+------+-------+-----+-------+mysql> select * from tb_student;
+-------+---------+------+
| stuid | stuname | clid |
+-------+---------+------+
| 1 | 張三 | 1 |
| 2 | 李四 | 1 |
| 3 | 王二 | 1 |
+-------+---------+------+mysql> select tb_student.stuname,tb_mark.score,tb_mark.cid from tb_student,tb_mark where tb_student.stuid=tb_mark.stuid;
+---------+-------+-----+
| stuname | score | cid |
+---------+-------+-----+
| 張三 | 80.0 | 1 |
| 張三 | 88.0 | 2 |
| 張三 | 71.0 | 3 |
| 張三 | 60.0 | 4 |
| 張三 | 66.0 | 5 |
| 張三 | 91.0 | 6 |
| 李四 | 77.0 | 1 |
| 李四 | 73.0 | 2 |
| 李四 | 84.0 | 3 |
| 李四 | 93.0 | 4 |
| 李四 | 64.0 | 5 |
| 李四 | 91.0 | 6 |
| 王二 | 97.0 | 1 |
| 王二 | 89.0 | 2 |
| 王二 | 81.0 | 3 |
| 王二 | 79.0 | 4 |
| 王二 | 93.0 | 5 |
| 王二 | 88.0 | 6 |
+---------+-------+-----+
總結(jié)
以上是生活随笔為你收集整理的MYsql建立学生成绩表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ATSHA204芯片手册阅读笔记
- 下一篇: 博士真的很多了吗?