MySql数据库实验知识点:实验⼀创建以下数据库和表,并查看是否成功创建
⽬的与要求
(1) 了解 mysql 数据库的存储引擎分类;
(2) 了解表的结构特点;
(3) 了解 mysql 的基本数据类型;
(4) 了解空值概念;
(5) 学会使⽤ sql 语句创建数据库和表。
实验内容
1.实验题⽬
创建⽤于企业管理的员⼯数据库,数据库名为 yggl,包含员⼯信息,部门信
息及员⼯薪⽔信息。数据库 yggl 包含 3 个表:
1) Employees: 员⼯信息表;
2) Departments: 部门信息表;
3) Salary: 员⼯薪⽔情况表。
创建语句如下:
(1)创建数据库
create database yggl;
use yggl;
(2)创建表 departments
create table Departments
(
DepartmentID char(3) NOT null,
DepartmentName char(20) not null,
Note text(16) ,
primary key (DepartmentID)
)engine=innodb;
(3)创建表 employees
create table employees
(employeeid char(6) not null,
name char(10) not null,
education char(4),
birthday date not null,
sex char(2) not null,
workyear tinyint(1) ,
address varchar(20) ,
phonenumber char(12),
departmentid char(3) not null,
primary key (employeeid)
)engine=innodb;
mysql面试题基础知识(4)创建表 salary create table salary
(
employeeid char(6),
income float(8),
outcome float(8),
primary key (employeeid)
)engine=innodb;
该实验进⾏前,⾸先要明确,⽤户必须是系统管理员,或是被授权使⽤ create database 语句的⽤户。其次,确定数据库包含哪些表,以及各表的结构,常⽤的 mysql 数据类型。
实验⼆表数据的插⼊、修改和删除
⽬的与要求
(1) 学会使⽤ sql 语句对数据表进⾏插⼊、删除和修改数据;centos官方
(2) 了解更新操作时要注意数据完整性;
(3) 了解 sql 语句对数据操作的灵活控制功能。
实验内容
1.实验题⽬
使⽤ sql 语句,向在上⼀个实验中建⽴的数据库 YGGL 的 3 个表插⼊多⾏数据,然后修改和删除⼀些记录。使⽤ SQL 进⾏有限制的修改和删除。要掌握 SQL 中⽤于对表数据进⾏插⼊、修改和删除命令分别 INSERT,UPDATE,DELETE。要特别注意在执⾏插⼊、删除和修改数据更新操作时,必须保证数据的完整性。命令⾏下的 SQL 语句在对表数据进⾏插⼊、修改删除时,⽐在图形界⾯下操作更灵活,功能更强⼤。
实验步骤
(1)使⽤ SQL 语句插⼊数据,向表 employees 插⼊数据。启动 mysql 客户端,在命令⾏中
⼊:
insert into employees values('000001',' 王 林 ',' ⼤ 专 ','1986-01-23','1',8,' 中 ⼭ 路 32-1-508','83355668','2');
insert into employees values('010008',' 伍容华 ',' 本 科 ','1988-03-28','1',3,' 北 京 东 路 100-2','83321321','1');
以上两⾏数据,并没有明确要求列出列名,所以各列值及类型要和表中的列⼀⼀对应。
insert into employees (employeeid,name,education,birthday,sex,workyear,address,phonenumber,departmentid) values
('020018','王向容','硕⼠','1982-12-09','1',2,'四牌楼 10-0-108','83792361','1');
以上命令在插⼊数据的过程中明确了列名和值的对应关系。
也可以同时插⼊多⾏数据, 如上所⽰, 多⾏值中间以逗号分割:
insert into employees (employeeid,name,education,birthday,sex,workyear,address,phonenumber,departmentid)
values('102201','刘明','本科','1978-07-30','1',3,'虎距路 100-2','83606608','5'), ('020010','李丽','⼤专','1972-10-18','1',3,'中⼭东路 102-2','83413301','1');
温馨提⽰:在查看运⾏结果的时候如果出现乱码(不管是命令⾏还是图形界⾯下)试⽤show variables like 'char%';和 show variables like 'collation_%';来查看⼀下 mysql 的数据使⽤编码!
⾄于怎么解决编码不⼀致导致的乱码问题,可以作为⼀个问题由同学们通过查询相关⽂献、⽹络资源⾃⾏解决以提⾼⾃我学习能⼒。更深⼊的了解可以查询字符编码相关知识。
伦勃朗全部视频
(2)删除⼀⾏
delete from employees where Employeeid='000001';
(3)修改⼀⾏
insert into employees values('000002',' 容 华 ',' 本 科 ','1988-03-28','0',3,' 北 京 东 路100-2','83321321','1');
update employees set address=' 北 京 西 路 100-2',phonenumber='83321320',departmentid='2' where
employeeid='000002';
(4)替换已经存在的⾏
insert into Departments values ('1','⼴告部','负责推⼴产品');
replace into Departments values ('1','⼴告 2 部','负责推⼴产品');
fmincon函数应用实例总结:向表中“替换插⼊”⼀条数据,如果原表中没有 id=1 这条数据就作为新数据插⼊(相当于 insert int
o 作⽤);如果原表中有 id=1这条数据就做替换(相当于 update 作⽤)。对于没有指定的字段以默认值插⼊。
(5)删除表中所有的⾏
truncate table salary 谨慎操作此命令。
实验三数据库查询
⽬的与要求
hbase基本命令(1) 掌握 SELECT 语句的基本语法;
(2) 掌握⼦查询的表⽰;
(3) 掌握连接查询的表⽰;
(4) GROUP BY ⼦句的作⽤和使⽤⽅法;
(5) ORDER BY ⼦句的作⽤和使⽤⽅法;
(6) LIMIT ⼦句的作⽤和使⽤⽅法。
实验内容
1,SELECT 语句的基本使⽤
(1)查询每个雇员的数据,
use yggl; 选择数据库
select * from employees;
(2)查询每个雇员的姓名,地址和电话
Select name,address,phonenumber from employees;
(3)查询 employeeid 为 000001 的雇员的地址和电话
select address ,phonenumber from employees where employeeid='000001';
练习:查询⽉收⼊⾼于 2000 的员⼯号码。
(4)查询 employess 表中⼥雇员的地址和电话,使⽤ AS 字句将结果中各列的标题分别指
定为地址,电话。
select address as 地址,phonenumber as 电话 from employees where sex='0';
(5)查询 employees 表中员⼯的姓名和性别,要求 sex 值为 1 时显⽰为男,为 0 时显⽰为
⼥(实际上是把 SEX 列的值由 0 和 1 改变为男和⼥,同时 sex 列名也被显⽰为性别)。
select name as 姓名,case
when sex='1' then '男'
when sex='0' then '⼥'
end
as 性别from employees;
(6)获取员⼯总数
select count(*) from employees;
镀锌z型钢批发
练习 :
1) 计算 salary 表中员⼯⽉收⼊的平均数
2) 获得 Employees 表中最⼤的员⼯号码
3) 计算 salary 表中所有员⼯的总⽀出
4) 查询账务部雇员的最⾼和最低实际收⼊
(7)出所有姓王的雇员的部门号和姓名
select name, departmentid from employees where name like '王%';
思考与练习:
a. 出所有地址中含有'中⼭'的雇员的号码及部门号,
select phonenumber,departmentid from employees where address like '中⼭%';
注意:
%与_都可以替换字符,%与_的不同之处在于%可以替换若⼲个字符,⽽_只能替换⼀个字符。看下⾯这⼀题就明⽩了。
b. 查员⼯号码中倒数第 2 个数字为 0 的姓名、地址和学历。
⽤%写:
select name,address,education from employees where employeeid like '%0_';
因为employeeid是6位的,所以前四位可以⽤⼀个%进⾏代替,⽽倒数第⼆位是0,最后⼀位⽤_代替⼀位数字。
错误写法:select name,address,education from employees where employeeid like '%0%';
这样写前后两个%会导致指代不明,不明⽩0到底是哪⼀位的。结果中只要employeeid中中间有0的都会被查询出来。
⽤_写:
select name,address,education from employees where employeeid like '____0_';
前⾯4个_,后⾯1个_,因为吗,每⼀个_只能替代⼀位。
(8)出所有收⼊在 2000~3000 元之间的员⼯号码,
Select employeeID from salary Where income between 2000 and 3000;
思考与练习:出所有部门号是 1 或 2 的⼯作的雇员的号码,
2 ⼦查询的使⽤,
(1)查在财务部⼯作的雇员的情况
Select * From employees Where departmentId=( Select DepartmentId From departments Where departmentname='财务部');思考与练习:⽤⼦查询的⽅法查所有收⼊在 2500 元以下的雇员的情况
注意:
如果⼦查询的表中的departmentid只有⼀个,不会出现查多个的情况。
如果⼦查询的表中的departmentid有多个,会出现Subquery returns more than 1 row。
例如:查询employees表中income少于2500的雇员的信息:
select * from employees where employeeid=(select employeeid from salary where income<'2500');
在salary表中,income少于2500的雇员有多个,所以会出现Subquery returns more than 1 row。
解决:添加any。select * from employees where employeeid= any(select employeeid from salary where income<'2500');(2)查研发部年龄不低于市场部所有雇员年龄的雇员的姓名
select name From employees Where departmentId in
( Select departmentid from departments where
departmentname='研发部')
and
Birthday<=all( Select birthday from employees where departmentID in
( Select departmentid from departments where
departmentname='市场部'));
思考与练习:⽤⼦查询的⽅法,查研发部⽐市场部所有雇员收⼊都⾼的雇员的姓名
(3) 查⽐⼴告部所有的雇员收⼊都⾼的雇员的姓名
select name from employees where employeeid in (
select employeeid from salary where income>all(
select income from salary where employeeid in (
select employeeid from employees where departmentID=(
select departmentID from departments where departmentname='
⼴告部')
)
)
);
3 连接查询的使⽤
(1)查询每个雇员的情况及薪⽔的情况
select employees.*,salary.* from
employees ,salary where
思考与练习查询每个雇员的情况及⼯作部门的情况
(2)使⽤内连接的⽅法查询名字为“王林”的员⼯所在部门
select departmentname from departments join employees on
departments.departmentID=employees.departmentID where employees.name='王林';思考与练习:
1) 使⽤内连接⽅法查不在⼴告部⼯作的所有员⼯信息。
2) 使⽤外连接⽅法查所有员⼯的⽤收⼊。
(3) 查⼴告部收⼊在 2000 元以上的雇员姓名及薪⽔详情
select name,income,outcome from employees,salary,departments
ployeeID
and
employees.departmentID=departments.departmentID
and
departmentname='⼴告部'
and
income>2000;
思考与练习: 查询研发部在 1966 年以前出⽣的雇员姓名及薪⽔详情
4,GROUP BY,ORDER BY 和 LIMIT ⼦句的使⽤
(1)查 employees 中男性和⼥性的⼈数
select sex,count(sex)
from employees
group by sex;
思考与练习:
1) 按部门列出在该部门⼯作的员⼯的⼈数。
2) 按员⼯的学历分组,列出本科,⼤专和硕⼠的⼈数。
(2)查员⼯数超过 2 ⼈的部门名称和员⼯数量
select departmentname ,count(*) as ⼈数
from employees,departments
where employees.departmentID=departments.departmentID
group by employees.departmentid
having count(*)>2;
思考与练习:
按员⼯的⼯作年份分组,统计各个⼯作年份的⼈数,如⼯作⼀年的多少⼈?⼯作⼆年的
多少⼈?
(3)将 Employees 表中的员⼯号码由⼤到⼩排列
select employeeid
from employees
order by employeeid desc;
思考与练习:
a. 将员⼯信息按出⽣⽇期从⼩到⼤排列。
b. 在 order by ⼦句中使⽤⼦查询,查询员⼯姓名,性别和⼯龄信息,要求按实际收⼊从⼤到⼩排列。
(4)返回 employees 表中的前 5 位的员⼯信息
select *
from employees limit 5;
思考与练习:返回 employees 表中从第 3 位员⼯开始的 5 个员⼯的信息?

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。