学习笔记使用SQLyog连接MySQL数据库
Posted 滕秋宇
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习笔记使用SQLyog连接MySQL数据库相关的知识,希望对你有一定的参考价值。
一、使用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 \'xx@163.com\' 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数据库的主要内容,如果未能解决你的问题,请参考以下文章
安装了sqlyog,没有mysql文件夹和服务,sqlyog也连接不了怎么办?