啟動mysql服務並進入mysql shell
建立乙個資料庫gradesystem
資料庫裡面放三張表create
database gradesystem;
第一張學生表student
:學生id(主鍵)、學生姓名、學生性別
sidsname
gender
1zhangsan
male
2lisi
male
3xiaohong
female
第二張課程表course
:課程id(主鍵)、課程名字
cidcname
1math
2physics
3chemistry
成績表mark
:成績 id(主鍵) 、學生 id 、課程 id 和分數
midsid
cidscore11
18022
18533
18641
27052
28663
29071
38082
38593
390接下來的一切操作都是在mysql shell裡面操作的
建立學生表student
建立課程表create
table student(
sid int(2) not
null,
sname char(20) not
null,
gender char(20) not
null
);
course
建立成績表create
table course(
cid int(2) not
null,
cname char(20) not
null
);
mark
新增主鍵,因為剛才建表的時候沒有新增主鍵create
table mark(
mid int(2) not
null,
sid int(2) not
null,
cid int(2) not
null,
score int(3)
);
新增鍵的語法給mark設定外來鍵新增主鍵約束 alter table 表名 add constraint 主鍵名 primary key 表名(主鍵欄位名)
新增外來鍵約束 alter table 表名 add constraint 外建名 foreign key (外來鍵欄位名)references 關聯表名 (關聯欄位名)
聯合主鍵的寫法:
primary key (欄位1,欄位2,……)
備註:在新增主鍵時,(主鍵欄位名)這個主鍵欄位名也可以寫多個 (欄位1,欄位2,……)。這就算是新增聯合主鍵的方法吧。
設定另外兩個也是一樣的
這裡寫外來鍵還行alter
table mark add
constraint pk_sid foreign
key (sid) references student(sid);
alter
table
addconstraint pk_cid foreign
key (cid) references course(cid);
建立表的時候是這麼新增的
例如
開始往create
table student(
sid int(2) not
null,
sname char(20) not
null,
gender char(20) not
null,
primary
key (sid)
);
student
表裡面插入資料
往insert
into student (sid, sname, gender) values (1, 'zhangsan', 'male');
insert
into student (sid, sname, gender) values (2, 'lisi', 'male');
insert
into student (sid, sname, gender) values (3, 'xiaohong', 'female');
course
中插入資料
往insert
into course (cid, cname) values (1, 'math');
insert
into course (cid, cname) values (2, 'physics');
insert
into course (cid, cname) values (3, 'chemistry');
mark
中插入資料
最後可通過insert
into mark (mid, sid, cid, score) values (1, 1, 1, 80);
insert
into mark (mid, sid, cid, score) values (1, 2, 1, 85);
insert
into mark (mid, sid, cid, score) values (1, 3, 1, 86);
insert
into mark (mid, sid, cid, score) values (1, 1, 2, 70);
insert
into mark (mid, sid, cid, score) values (1, 2, 2, 86);
insert
into mark (mid, sid, cid, score) values (1, 3, 2, 90);
insert
into mark (mid, sid, cid, score) values (1, 1, 3, 80);
insert
into mark (mid, sid, cid, score) values (1, 2, 3, 85);
insert
into mark (mid, sid, cid, score) values (1, 3, 3, 90);
select * from mark
來檢視成績表和同樣命令來檢視其它兩個表。 簡易學生成績管理系統Python
coding utf 8 import random 成績管理系統 score 錄入成績 score 語文 英語 物理 化學 生物 查詢 print 按照科目查詢 course input 請輸入科目 n print score course print 按照科目和姓名查詢 course input...
利用XML程式設計實踐簡易學生成績管理系統
這種模式就是典型的mvc模式,由下層的model domain 層提供資料物件一步一步地到dao層,再到ui層。exam.xml examid 123 idcard 111 cecilianame 北京location 90grade student examid 456 idcard 112 ma...
用c 模擬實現乙個學生成績管理系統
題目 用c 模擬實現乙個學生成績的資訊管理系統,要求能新增 刪除 修改 檢視和儲存學生的資訊等功能 源 如下 define crt secure no warnings includeusing namespace std include includeclass student 查詢學生資訊 vo...