MySQL开发入门(仅用cmd编程)

今天,我为大家带来MySQL开发入门介绍,希望对数据库初学者有所帮助。

MySQL是一个关系型数据库管理系统。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。由于其性能卓越,搭配PHP和Apache可组成良好的开发环境。

首先,读者可通过搜索下载mysql-5.6.17-winx64.zip并安装,建议读者不下载5.7+的版本,因为下载到的内容可能有缺少,功能不稳定。

下载到的mysql-5.6.17-winx64.zip解压后的完整目录如下:


MySQL开发入门(仅用cmd编程)_第1张图片

MySQL安装过程请参考MySQL安装,安装过程值得注意的是,在输入mysqld install指令和net start mysql指令时,务必先切换到如上图所示的bin所在目录下,否则会出现安装失败、缺少文件、服务无法启动等问题(即使你配置了环境变量)。

下面笔者将以“图书数据库系统”为导向,来为大家介绍MySQL的基础指令:

首先是概念内容设计(笔者截取下自己的笔记):

MySQL开发入门(仅用cmd编程)_第2张图片

接着根据上图转换成逻辑设计

BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)
primary key : Book_Number
foreign key : PerNumber refer to PERSON(Person_Number)
PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)
primary key : Person_Number
PUNISH(Punish_Number,PerNumber,Person_Name,Person_Type,Punish_Time,Punish_Reason,Punish_Content,Is_Punish_Finish)
primary key : Punish_Number
foreign key : PerNumber refer to PERSON(Person_Number)

下面进行实现

(1)建表

mysql> create table BOOK(
              Book_Number int,
              PerNumber int,    
              Book_Name char(30),
              Is_Borrow bool,
              Person_Name char(30),
              Borrow_Time date,
              Return_Time date,
              Primary Key(Book_Number)
);
mysql> create table PERSON(
              Person_Number int,
              Person_Name char(30),
              Person_Type char(30),
              Book_Name char(30),
              Is_Punish bool,
              Primary Key(Person_Number)
);
mysql> create table PUNISH(
              Punish_Number int,
              PerNumber int,
              Person_Name char(30),
              Person_Type char(30),
              Punish_Time date,
              Punish_Reason char(30),
              Punish_Content char(30),
              Is_Punish_Finish bool,
              Primary Key(Punish_Number)
);

*注意:此处定义为char(30),如果仅仅定义为char,默认长度为1,则只能显示第一个字符。如果一开始定义错误,要修改表结构,可用
mysql> alter table PERSON modify Person_Name char(30);

建表效果图如下:


MySQL开发入门(仅用cmd编程)_第3张图片
Paste_Image.png

(2)添加外键约束

mysql> alter table BOOK add constraint FK_BOOK Foreign Key(PerNumber) references PERSON(Person_Number);
mysql> alter table PUNISH add constraint FK_PUNISH Foreign Key(PerNumber) references PERSON(Person_Number);

添加外键约束效果图如下:

MySQL开发入门(仅用cmd编程)_第4张图片
Paste_Image.png

(3)插入测试数据

-------------------------------PERSON---------------------------

mysql> insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(1,'A1','adminstrator',NULL,false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(2,'A2','adminstrator','语文',false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(3,'A3','adminstrator',NULL,true);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(4,'B4','borrower',NULL,true);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(5,'B5','borrower','数学',false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(6,'B6','borrower','英语',false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(7,'B7','borrower','物理',false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(8,'B8','borrower','化学',false);

mysql>insert into PERSON(Person_Number,Person_Name,Person_Type,Book_Name,Is_Punish)values(9,'B9','borrower','生物',false);

插入PERSON表数据效果图如下:

MySQL开发入门(仅用cmd编程)_第5张图片
Paste_Image.png

-------------------------------PUNISH----------------------------

mysql>insert into PUNISH(Punish_Number,PerNumber,Person_Name,Person_Type,Punish_Time,Punish_Reason,Punish_Content,Is_Punish_Finish)values(1,(select Person_Number from PERSON where Person_Number = 3),'A3','adminstrator','2016-01-24','违规操作','罚款20元',true);

mysql>insert into PUNISH(Punish_Number,PerNumber,Person_Name,Person_Type,Punish_Time,Punish_Reason,Punish_Content,Is_Punish_Finish)values(2,(select Person_Number from PERSON where Person_Number = 4),'B4','borrower','2016-01-24','书过期未还','罚款10元',false);

插入PUNISH表数据效果图如下:

MySQL开发入门(仅用cmd编程)_第6张图片
Paste_Image.png

--------------------------------BOOK----------------------------

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(1,(select Person_Number from PERSON where Person_Number = 2),'语文',true,'A2','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(2,(select Person_Number from PERSON where Person_Number = 5),'数学',true,'B5','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(3,(select Person_Number from PERSON where Person_Number = 6),'英语',true,'B6','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(4,(select Person_Number from PERSON where Person_Number = 7),'物理',true,'B7','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(5,(select Person_Number from PERSON where Person_Number = 8),'化学',true,'B8','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(6,(select Person_Number from PERSON where Person_Number = 9),'生物',true,'B9','2016-01-24','2016-04-24');

mysql>insert into BOOK(Book_Number,Book_Name,Is_Borrow)values(7,'历史',false);

mysql>insert into BOOK(Book_Number,Book_Name,Is_Borrow)values(8,'地理',false);

mysql>insert into BOOK(Book_Number,Book_Name,Is_Borrow)values(9,'政治',false);

插入BOOK表数据效果图如下:

MySQL开发入门(仅用cmd编程)_第7张图片
Paste_Image.png
MySQL开发入门(仅用cmd编程)_第8张图片
Paste_Image.png

(4)发现插入数据考虑欠缺,对表进行删改

mysql>update PERSON set Book_Name = '历史' where Person_Number =4;

mysql>update PERSON set Is_Punish = false where Person_Number = 3;

mysql> delete from BOOK where Book_Number = 7;

mysql>insert into BOOK(Book_Number,PerNumber,Book_Name,Is_Borrow,Person_Name,Borrow_Time,Return_Time)values(7,(select Person_Number from PERSON where Person_Number = 4),'历史',true,'B4','2015-10-23','2016-01-23');

对表删改的效果图如下:

MySQL开发入门(仅用cmd编程)_第9张图片
Paste_Image.png
MySQL开发入门(仅用cmd编程)_第10张图片
Paste_Image.png

(5)查询测试

mysql> select BOOK.Book_Name,Borrow_Time,Return_Time from BOOK inner join PERSON on BOOK.PerNumber = PERSON.Person_Number where Date(Borrow_Time) >'2015-10-23';

mysql> select * from PUNISH where (Is_Punish_Finish = false and Punish_Reason = '书过期未还') or (Person_Type = 'adminstrator');

查询效果图如下:

MySQL开发入门(仅用cmd编程)_第11张图片
Paste_Image.png

(6)创建视图测试

mysql> create or replace view Punish_View as select Punish.Person_Name,Punish_Time,Punish_Reason,Punish_Content from PUNISH where Is_Punish_Finish = false;

mysql> select * from Book_View;

mysql> select * from Punish_View;

创建视图效果图如下:

MySQL开发入门(仅用cmd编程)_第12张图片
Paste_Image.png

(7)创建存储过程或函数测试

mysql> 
delimiter $
create procedure Borrow_Count(OUT 总共借出的书本数目为: int)
begin
select count(*) into 总共借出的书本数目为: from BOOK where Is_Borrow = true;
select 总共借出的书本数目为:;
end;$

mysql> 
delimiter ;
set @总共借出的书本数目为:=0;
call Borrow_Count(@总共借出的书本数目为:);

mysql> select * from book;

创建存储过程或函数测试效果图如下:

MySQL开发入门(仅用cmd编程)_第13张图片
Paste_Image.png

(8)创建触发器测试

mysql> 
delimiter $
create trigger Punish_Finish_Update
after update on PERSON
for each row
begin
if new.Is_Punish = false
then
update PUNISH set PUNISH.Is_Punish_Finish = true where PUNISH.PerNumber = new.Person_Number;
end if;
end;$

mysql> select * from punish;$

mysql> select * from person;$

mysql> update PERSON set Is_Punish = false where Person_Number = 4;$

mysql> select * from punish;$

mysql> select * from person;$

创建触发器效果图如下:

MySQL开发入门(仅用cmd编程)_第14张图片
Paste_Image.png
MySQL开发入门(仅用cmd编程)_第15张图片
Paste_Image.png

由此可见,触发器Punish_Finish_Update成功作用,同时修改两表

*注意:使用trigger时,delimiter 把指令的结束标志变成了,所以上述查询中,每句指令都要用$作为结束

重新定义指令结束标志为:

mysql> delimiter ;
mysql> select * from person;
mysql> select * from punish;
MySQL开发入门(仅用cmd编程)_第16张图片
Paste_Image.png

读者若通过以上的训练,基础的MySQL开发入门便已掌握。这里顺便提醒一下,用cmd开发时,每句代码后要用分号";"结束,否则你可能会感觉cmd崩了。经过笔者检验,cmd不会崩,出问题了需要从自身出发寻找问题出现的原因。

感谢您的关注!谢谢!

你可能感兴趣的:(MySQL开发入门(仅用cmd编程))