番外篇:MySQL简单操作之【增删改查】

番外篇:MySQL简单操作之【增删改查】)

  1. 安装数据库的步骤这里就不作介绍了,如果想要看这个的朋友可以百度其他博客了解mysql的安装步骤。
  2. 本篇主要介绍一些简单的数据库mysql的基本操作。

登录数据库

点击登录mysql的命令框

在这里插入图片描述

输入默认密码:123456

在这里插入图片描述

已知操作的数据信息和查询题目

数据列表

在这里插入图片描述

查询问题

在这里插入图片描述

测试mysql代码

// 创建数据库
create database mysql_test
// 使用数据库(要在mysql_test这个数据库导入数据表一定要使用它)
use mysql_test
// 创建数据表
create table `Gz`(
	`职工号` int(10),
	`姓名` varchar(20) not null,
	`性别` varchar(10) not null,
	`工资` int(10) not null,
	`科室号` int(20) not null,
	primary key (`职工号`)
);

create table `Ks`(
	`科室号` int(20) not null,
	`科室名` varchar(20) not null,
	`负责人` varchar(10) not null,
	primary key (`科室号`)
);

// 插入对应的数据
insert into `Gz`(`职工号` ,`姓名` ,`性别` ,`工资`,`科室号`) values( 101, '李红', '女', 3780, 111);
insert into `Gz`(`职工号` ,`姓名` ,`性别` ,`工资`,`科室号`) values( 102, '张力', '男', 3200, 112);
insert into `Gz`(`职工号` ,`姓名` ,`性别` ,`工资`,`科室号`) values( 103, '王兰', '女', 3460, 112);
insert into `Gz`(`职工号` ,`姓名` ,`性别` ,`工资`,`科室号`) values( 104, '张三', '男', 4350, 113);
insert into `Gz`(`职工号` ,`姓名` ,`性别` ,`工资`,`科室号`) values( 105, '方飞', '女', 3788, 114);

insert into `Ks`(`科室号` ,`科室名` ,`负责人` ) values( 111, '办公室', '李红');
insert into `Ks`(`科室号` ,`科室名` ,`负责人` ) values( 112, '销售科', '王兰');
insert into `Ks`(`科室号` ,`科室名` ,`负责人` ) values( 113, '技术科', '张三');

// 第一题:查询数据.
select `姓名`,`科室名`,`性别`,`工资` from `Gz`, `Ks` where `Gz`.`科室号` = `Ks`.`科室号` and `工资`>3200;

// ps: 创建视图的语句(测试)
create view test_view as select * from `Gz`;
select * from test_view;

// 第三题:创建对应函数.
delimiter $
drop function if exists mysql_test$
create function mysql_test(score int(10))
returns varchar(255)
begin
return(select `姓名` from `Gz` where `工资`>score);
end $
delimiter ;
* 执行对应的自定义函数.
select mysql_test(3200);

///
(正确的)
delimiter $
drop function if exists mysql_test$
create function mysql_test(score int(10))
returns varchar(255)
begin
return(select group_concat(`姓名`,'/',`工资`) from `Gz` where `工资`>score);
end $
delimiter ;

select mysql_test(3200);

///
(错误的) --- 反斜杠错了
delimiter $
drop function if exists mysql_test$
create function mysql_test(score int(10))
returns varchar(255)
begin
return(select group_concat(`姓名`,'\',`工资`) from `Gz` where `工资`>score);
end $
delimiter ;

执行效果

第一题:
在这里插入图片描述
视图:
在这里插入图片描述
第三题:
在这里插入图片描述

注:转载注明出处

https://blog.csdn.net/qq_49710945/article/details/109598617

猜你喜欢

转载自blog.csdn.net/qq_49710945/article/details/109598617
今日推荐