【學習筆記】使用SQLyog連接MySQL數據庫
阿新 • • 發佈:2017-07-14
comm 丟失 school turn 復合主鍵 price not email pre
一、使用SQLyog創建數據庫用來管理學生信息
1 #創建數據庫student 2 DROP DATABASE IF EXISTS Myschool; 3 CREATE DATABASE Myschool; 4 5 #在數據庫中新建四張數據表 6 USE Myschool; 7 8 #創建年級表 9 CREATE TABLE grade( 10 gradeID INT(4) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘年級編號‘, 11 gradeName VARCHAR(50) NOT NULL COMMENT ‘年級名稱‘ 12 )COMMENT="年級表"; 13 14 #創建學生信息表 15 CREATE TABLE student( 16 studentNo INT(4) NOT NULL PRIMARY KEY COMMENT ‘學號‘, 17 loginPwd VARCHAR(20) NOT NULL DEFAULT ‘123‘ COMMENT ‘密碼‘, 18 studentName VARCHAR(50) NOT NULL COMMENT ‘姓名‘, 19 sex CHAR(2) NOT NULL DEFAULT‘男‘ COMMENT ‘性別‘, 20 gradeID INT(4) UNSIGNED COMMENT ‘年級編號‘, 21 phone VARCHAR(50) COMMENT ‘電話‘, 22 address VARCHAR(255) DEFAULT ‘地址不詳‘ COMMENT ‘地址‘, 23 bornDate DATETIME DEFAULT NOW() COMMENT ‘出生日期‘, 24 email VARCHAR(50) DEFAULT ‘[email protected]‘ COMMENT ‘郵件賬號‘, 25 identityCard VARCHAR(18) COMMENT ‘身份證號‘ 26 )COMMENT="學生信息表"; 27 28 #創建科目表 29 CREATE TABLE `subject`( 30 subjectNo INT(4) NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘課程編號‘, 31 subjectName VARCHAR(50) COMMENT ‘課程名稱‘, 32 classHour INT(4) COMMENT ‘學時‘, 33 gradeID INT(4) UNSIGNED COMMENT ‘年級編號‘ 34 )COMMENT="科目表"; 35 36 #創建成績表 37 CREATE TABLE result( 38 studentNo INT(4) NOT NULL COMMENT ‘學號‘, 39 subjectNo INT(4) NOT NULL COMMENT ‘課程編號‘, 40 examDate DATETIME DEFAULT NOW() NOT NULL COMMENT ‘考試日期‘, 41 studentResult INT(4) NOT NULL COMMENT ‘考試成績‘ 42 )COMMENT="成績表"; 43 44 #為表添加外鍵(關系) 45 46 #在student表中添加gradeID外鍵 47 ALTER TABLE student 48 ADD CONSTRAINT fk_student_grade FOREIGN KEY(gradeID) 49 REFERENCES grade(gradeID); 50 51 #在subject表中添加gradeID外鍵 52 ALTER TABLE `subject` 53 ADD CONSTRAINT fk_subject_grade FOREIGN KEY(gradeID) 54 REFERENCES grade(gradeID); 55 56 #在result表中添加studentNo,subjectNo外鍵 57 ALTER TABLE result 58 ADD CONSTRAINT fk_result_student FOREIGN KEY(studentNo) 59 REFERENCES student(studentNo); 60 61 ALTER TABLE result 62 ADD CONSTRAINT fk_result_subject FOREIGN KEY(subjectNo) 63 REFERENCES `subject`(subjectNo); 64 65 #為表添加初始數據 66 67 #年級表grade的數據 68 INSERT INTO grade(gradeID,gradeName) 69 VALUES(1,‘一年級‘),(2,‘二年級‘),(3,‘三年級‘),(4,‘四年級‘),(5,‘五年級‘); 70 71 #科目表subject的數據 72 INSERT INTO `subject`(subjectNo,subjectName,classHour,gradeID) 73 VALUES(1,‘Logic Java‘,220,1),(2,‘HTML‘,160,1),(3,‘Java OOP‘,230,2); 74 75 #學生信息表student的數據 76 INSERT INTO student(studentNo,studentName,sex,gradeID,phone,address,bornDate) 77 VALUES(10000,‘郭靖‘,‘男‘,1,13645667783,‘天津市河西區‘,‘1990-09-08‘), 78 (10001,‘李文才‘,‘男‘,1,13645667890,‘地址不詳‘,‘1994-04-12‘), 79 (10002,‘李斯文‘,‘男‘,1,13645556793,‘河南洛陽‘,‘1993-07-23‘), 80 (10003,‘張萍‘,‘女‘,1,13642345112,‘地址不詳‘,‘1995-06-10‘), 81 (10004,‘韓秋潔‘,‘女‘,1,13812344566,‘北京市海澱區‘,‘1995-07-15‘), 82 (10005,‘張秋麗‘,‘女‘,1,13567893246,‘北京市東城區‘,‘1994-01-17‘), 83 (10006,‘肖梅‘,‘女‘,1,13563456721,‘河北省石家莊市‘,‘1991-02-17‘), 84 (10007,‘秦洋‘,‘男‘,1,13056434411,‘上海市盧灣區‘,‘1992-04-18‘), 85 (10008,‘何睛睛‘,‘女‘,1,13053445221,‘廣州市天河區‘,‘1997-07-23‘), 86 (20000,‘王寶寶‘,‘男‘,2,15076552323,‘地址不詳‘,‘1996-06-05‘), 87 (20010,‘何小華‘,‘女‘,2,13318877954,‘地址不詳‘,‘1995-09-10‘), 88 (30011,‘陳誌強‘,‘女‘,3,13689965430,‘地址不詳‘,‘1994-09-27‘), 89 (30012,‘李露露‘,‘女‘,3,13685678854,‘地址不詳‘,‘1992-09-27‘); 90 91 #成績表result的數據 92 INSERT INTO result(studentNo,subjectNo,examDate,studentResult) 93 VALUES(10000,1,‘2016-02-15‘,71), 94 (10000,1,‘2016-02-17‘,60), 95 (10001,1,‘2016-02-17‘,46), 96 (10002,1,‘2016-02-17‘,83), 97 (10003,1,‘2016-02-17‘,65), 98 (10004,1,‘2016-02-17‘,70), 99 (10005,1,‘2016-02-17‘,95), 100 (10006,1,‘2016-02-17‘,93), 101 (10007,1,‘2016-02-17‘,23), 102 (20000,3,‘2016-07-17‘,68), 103 (20010,3,‘2016-07-17‘,90), 104 (20000,2,‘2016-07-17‘,88), 105 (20010,2,‘2016-07-17‘,78); 106 107 ##學生信息數據庫備份##
二、使用SQLyog創建數據庫制作圖書館管理系統
1 #建立一個圖書館管理系統的數據庫來存放圖書館的相關信息,包括圖書的基本信息、圖書借閱的信息和讀者的信息 2 3 #創建數據庫Library 4 CREATE DATABASE IF NOT EXISTS Library; 5 USE Library; 6 #創建實體表 7 #圖書信息表book 8 CREATE TABLE book( 9 bookId VARCHAR(50) PRIMARY KEY NOT NULL COMMENT ‘圖書編號‘, 10 bookName VARCHAR(255) NOT NULL COMMENT ‘圖書名稱‘, 11 author VARCHAR(50) COMMENT ‘作者姓名‘, 12 pubComp VARCHAR(50) COMMENT ‘出版社‘, 13 pubDate DATE COMMENT ‘出版日期‘, 14 bookCount INT(4) COMMENT ‘現存數量‘, 15 price DOUBLE COMMENT ‘單價‘ 16 )COMMENT="圖書信息表"; 17 18 #讀者信息表reader 19 CREATE TABLE reader( 20 readerId VARCHAR(50) PRIMARY KEY NOT NULL COMMENT ‘讀者編號‘, 21 readerName VARCHAR(50) NOT NULL COMMENT ‘讀者姓名‘, 22 lendNum INT(4) COMMENT ‘已借書數量‘, 23 readerAddress VARCHAR(255) COMMENT ‘聯系地址‘ 24 )COMMENT="讀者信息表"; 25 26 #圖書借閱表borrow 27 CREATE TABLE borrow( 28 readerId VARCHAR(50) NOT NULL COMMENT ‘讀者編號‘, 29 bookId VARCHAR(50) NOT NULL COMMENT ‘圖書編號‘, 30 lendDate DATETIME DEFAULT NOW() NOT NULL COMMENT ‘借閱日期‘, 31 willDate DATE COMMENT ‘應歸還日期‘, 32 returnDate DATE COMMENT ‘實際歸還日期‘ 33 )COMMENT="圖書借閱表"; 34 35 #罰款記錄表penalty 36 CREATE TABLE penalty( 37 readerId VARCHAR(50) NOT NULL COMMENT ‘讀者編號‘, 38 bookId VARCHAR(50) NOT NULL COMMENT ‘圖書編號‘, 39 pDate DATE NOT NULL COMMENT ‘罰款日期‘, 40 pType INT COMMENT ‘罰款類型:1-延期,2-損壞,3-丟失‘, 41 amount DOUBLE COMMENT ‘罰款金額‘ 42 )COMMENT="罰款記錄表"; 43 44 #刪除信息表 45 USE Library; 46 DROP TABLE IF EXISTS book; 47 DROP TABLE IF EXISTS reader; 48 DROP TABLE IF EXISTS borrow; 49 DROP TABLE IF EXISTS penalty; 50 51 #給borrow添加復合主鍵 52 ALTER TABLE borrow 53 ADD CONSTRAINT pk_borrow PRIMARY KEY(readerId,bookId,lendDate); 54 55 #給penalty添加復合主鍵 56 ALTER TABLE penalty 57 ADD CONSTRAINT pk_penalty PRIMARY KEY(readerId,bookId,pDate); 58 59 ##刪除borrow和penalty的主鍵 60 ALTER TABLE borrow 61 DROP PRIMARY KEY; 62 ALTER TABLE penalty 63 DROP PRIMARY KEY; 64 ##刪除borrow和penalty的外鍵 65 ALTER TABLE borrow 66 DROP FOREIGN KEY fk_borrow_book; 67 ALTER TABLE borrow 68 DROP FOREIGN KEY fk_borrow_reader; 69 ALTER TABLE penalty 70 DROP FOREIGN KEY fk_penalty_book; 71 ALTER TABLE penalty 72 DROP FOREIGN KEY fk_penalty_reader; 73 74 #給borrow添加外鍵 75 ALTER TABLE borrow 76 ADD CONSTRAINT fk_borrow_book FOREIGN KEY(bookId) 77 REFERENCES book(bookId); 78 ALTER TABLE borrow 79 ADD CONSTRAINT fk_borrow_reader FOREIGN KEY(readerId) 80 REFERENCES reader(readerId); 81 82 #給penalty添加外鍵 83 ALTER TABLE penalty 84 ADD CONSTRAINT fk_penalty_book FOREIGN KEY(bookId) 85 REFERENCES book(bookId); 86 ALTER TABLE penalty 87 ADD CONSTRAINT fk_penalty_reader FOREIGN KEY(readerId) 88 REFERENCES reader(readerId);
【學習筆記】使用SQLyog連接MySQL數據庫