MySQL数据库在Navicat中的简单实例

        初次接触MySQL数据库时,我们往往会被大量的概念搞得晕头转向,在深入了解数据库底层架构之前,我们不妨先用一个简单的练习直观的感受一下数据库是如何对信息进行处理的。

        Navicat是一款能够图形化使用MySQL的工具,这会让我们更加直观地观察数据库中信息的变化,接下来我将用Navicat连接数据库进行实例展示。

        连接好数据库后,我们在Navicat中创建员工数据库test_db以及库内的员工信息表employee和部门表dept:

         这里不建议初学者直接用图形化界面操作,使用命令列界面用MySQL语句建库才能真正理解数据库是如何处理信息的。下面附上建库建表代码:

--建库
create database dest_db;

--建部门表并插入数据
use test_db;
drop table if exists `dept`;
create table `dept` (
  `id` int(255) not null,
  `name` varchar(20) default null,
  primary key (`id`)
) ENGINE=InnoDB default charset=utf8;

insert into `dept` values ('1', '开发部');
insert into `dept` values ('2', '市场部');
insert into `dept` values ('3', '财务部');

--建员工信息表并插入数据
use test_db;
drop table if exists `employee`;
create table `employee` (
  `id` int(11) not null,
  `name` varchar(20) default null,
  `gender` char(1) default null,
  `salary` double(10,2) default null,
  `join_date` date default null,
  `dept_id` int(11) default null,
  primary key (`id`)
) ENGINE=InnoDB default charset=utf8;

insert into `employee` values ('1', '关羽', '男', '7200.00', '2013-02-24', '1');
insert into `employee` values ('2', '张飞', '男', '4500.00', '2010-12-02', '1');
insert into `employee` values ('3', '刘备', '男', '9000.00', '2008-08-08', '2');
insert into `employee` values ('4', '孙尚香', '女', '5000.00', '2015-10-07', '3');
insert into `employee` values ('5', '赵云', '男', '6000.00', '2011-03-14', '1');
insert into `employee` values ('6', '关平', '男', '2000.00', '2020-04-24', '1');
insert into `employee` values ('7', '诸葛亮', '男', '8800.00', '2012-10-06', '2');
insert into `employee` values ('8', '马超', '男', '4500.00', '2015-11-26', '1');
insert into `employee` values ('9', '黄月英', '女', '5000.00', '2013-09-13', '3');

         数据库及库内的表已经建立完成,接下来就可以对表进行一些简单的操作了,这里列举18个简单操作:

1.查询所有员工信息

select * from employee;

 

2.查询员工工资并去重

select distinct salary from employee;

 3.查询开发部所有员工信息

select * from employee where dept_id=1;

 

 4.查询工资在4000~6000的的所有员工信息

select * from employee where salary>4000 and salary<6000;

 5.查询姓关的所有员工信息

select * from employee where `name` like '关%';

 6.查询工资最高的员工是谁

select name from (select max(salary) maxs from employee)e,employee where employee.salary=e.maxs;

 7.按工资对员工进行信息排序(降序)

select * from employee order by salary;

 8.查询女员工的数量

select count(*) from employee where gender='女';

 9.查询最高工资和最低工资

select max(salary),min(salary) from employee;

 10.按性别给员工分组,统计每个分组员工的人数

select gender,count(gender) from employee group by gender;

11.查询工资小于平均工资的员工有哪些 

select name from employee,(select avg(salary) avgs from employee)e where employee.salary < e.avgs;

 12.查询工资大于5000的员工来自于哪些部门,输出部门的名字

select employee.name,dept.name from employee,dept where employee.dept_id=dept.id and salary >5000;

 13.查询开发部与财务部所有的员工信息,分别使用子查询和表连接实现

--子查询
select * from employee 
where employee.dept_id in (
	select id 
	from dept
	where dept.`name`='开发部' or dept.`name`='财务部');

--表连接
select employee.name,gender,salary,join_date,dept.`name`
from employee,dept
where dept_id=dept.id and (dept.name='开发部' or dept.name = '财务部');

14.查询2011年以后入职的员工信息,分别使用子查询和表连接实现

--子查询
select x.id,x.`name`,x.salary,x.join_date,y.`name`
from employee x,dept y 
where x.`name` in(
	select `name`
	from employee
	where join_date >= "2011.01.01") and dept_id=y.id;

--表连接
select x.id,x.`name`,x.salary,x.join_date,y.`name`
from employee x,dept y 
where x.dept_id = y.id and join_date >="2011.01.01";

 15.开发部所有员工加薪1000,市场部所有员工加薪2000,财务部所有员工降薪500,并查出薪资变更后的所有员工信息和部门信息

--开发部加薪
update employee,dept
set salary = salary+1000
where employee.dept_id=dept.id and dept.`name`='开发部';

--市场部加薪
update employee,dept
set salary = salary+2000
where employee.dept_id=dept.id and dept.`name`='市场部';

--财务部加薪	
update employee,dept
set salary = salary-500
where employee.dept_id=dept.id and dept.`name`='财务部';

select * from employee;

16.开除工资最低的员工

delete 
from employee 
where salary in (
    select minsalary from(
        select min(salary) minsalary from employee)a);

17.给员工表中工资字段创建索引,然后删除员工表中工资字段的索引

--创建索引
create index salary_index on employee(salary);

--删除索引
drop index salary_index;

 18.删除员工表

drop table employee;

         以上就是我用Navicat连接MySQL数据库展示的简单实例,有问题的同学可以评论区提问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

交交的土拨鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值