MySQL数据库基础学习笔记

本文为数据库的基础概念和命令总结,包括查询、增删改、库表管理等。

1、数据库的好处

持久化数据到本地、可以实现结构化查询,方便管理。

2、常见概念

DB:数据库,保存一组有组织的数据的容器。

DBMS:数据库管理系统,又称为数据库软件(产品),用于管理DB中的数据。

SQL:结构化查询语言,用于和DBMS通信的语言。

3、数据库存储数据的特点

将数据放到表中,表再放到库中。

一个数据库中可以有多个表,每个表都有一个的名字,用来标识自己。表名具有唯一性。

表具有一些特性,这些特性定义了数据在表中如何存储,类似java中“类”的设计。

表由列组成,我们也称为字段。所有表都是由一个或多个列组成的,每一列类似java 中的”属性”。

表中的数据是按行存储的,每一行类似于java中的“对象”。

4、MySQL的启动和停止

启动:net start 服务名(例如:net start mysql)

停止:net stop 服务名(例如:net stop mysql)

5、MySQL的登录和退出

登录:mysql -h 主机名 -P 端口号 -u 用户名 -p密码

(注意:-p和密码中间不能加空格)

(例如:mysql 【-h localhost P 3306】 -u root -p密码 ,若是登录本地的,【】内的可以不写)

退出:exit 或 ctl+c

6、MySQL常见命令

show databases; #查看所有的数据库

use 库名; #打开指定 的库

show tables ; #显示库中的所有表

show tables from 库名; #显示指定库中的所有表

create table 表名(

字段名 字段类型,

字段名 字段类型

); 创建表

desc 表名; #查看指定表的结构

select * from 表名; #显示表中的所有数据

7、查询语句

select 字段名 from 表名;

select 字段名1,字段名2,...... from 表名;

select * from 表名;

select 常量值;(注意:字符型和日期型的常量值要用单引号括起来,数值型不需要)

select 函数名;

select 表达式;

select 数值+数值;  /*结果为数值*/

select 字符+数值; /*试图将字符转换成数值,转换成功则继续运算,转换不成功则把字符当成成0再运算*/

select null+值; /*结果都为null*/

#别名

select 字段名 as 别名 from 表名;

select 字段名 别名 from 表名; /*可直接用空格代替as*/

#去重

select distinct 字段名 from 表名;

#concat函数

select concat(字符1,字符2,......) from 表名;

#ifnull函数

#判断某字段或表达式是否为null,如果为null,返回指定的值(比如指定0),否则返回原本的值

select ifnull(字符, 0) from 表名;

#isnull

#判断某字段或表达式是否为null,是则返回1,不是则返回0

select 字符1 from 表名 where 字符1 is null;

select 字符1 from 表名 where 字符1 is not null;

8、条件运算符

>
<
>= 
<= 
= ,<=> 安全等于
!= ,<>

9、逻辑运算符

and(&&):两个条件如果同时成立,结果为true,否则为false
or(||):两个条件只要有一个成立,结果为true,否则为false
not(!):如果条件成立,则not后为false,否则为true

9、模糊查询

like 
between and
in
is null

10、排序查询

#语法
select
	要查询的东西
from
	表
where 
	条件

order by 排序的字段|表达式|函数|别名 【asc|desc】
(默认升序)

#实践代码-----------------------------
select * from employees order by salary desc;
select * from employees order by salary asc;

select * 
from employees
where department_id >= 90
order by hiredate asc;

#按年薪的高低显示员工的信息和年薪(按表达式排序)
select *, salary*12*(1+ifnull(commission_pct, 0)) as 年薪
from employees
#order by salary*12*(1+ifnull(commission_pct, 0)) desc;
order by 年薪 desc;

#按姓名长度显示员工的姓名和工资【按函数排序】
select length(last_name) as 字节长度, last_name, salary
from employees
order by 字节长度 desc;

#查询员工信息,先按工资什序,再按员工编号降序【按多个字段排序】
select *
from employees
order by salary asc, employee_id desc;

11、实践代码

use myemployees;

select * from employees;
select first_name from employees; 
select first_name as hhh from employees; 
select first_name hhh from employees; 
select distinct department_id from employees; 
select ifnull(commission_pct, 0)  as '奖金率', commission_pct from employees;
select concat(first_name,'___' ,last_name,'___',ifnull(commission_pct, 0)) as output from employees; 
select 100;
select 'xym';
select 100*20 as resulet;
select version();

/*
select 
		查询列表
from
		表名
where
		筛选条件;
*/

select * from employees where salary>12000;
select last_name, department_id from employees where department_id <> 90;

select last_name, salary, commission_pct 
from employees 
where salary >= 10000 and salary <= 20000;

select *
from employees
where not(department_id < 90 or department_id>110) 
	  or salary > 15000;

/*通配符:%任意多个字符
		  _任意单个字符
*/
select * from employees where last_name like '%a%';
select * from employees where last_name like '_e_l';

select * 
from employees 
where last_name 
like '_$_%' escape '$';*/  /*查询第二个字符为_的员工,转义*/

select * 
from employees 
where employee_id 
between 100 and 120;  /*包含临界值(闭区间),不能颠倒大小*/

select last_name, job_id
from employees 
#where job_id='IT_PROT' or  job_id= 'AD_VP' or job_id = 'AD_PRES';
where job_id in('IT_PROT','AD_VP','AD_PRES')

/*
is null:仅可以判断 null
<=>:既可以判断 null,又可以判断数值
*/

select last_name, commission_pct
from employees 
#where commission_pct is null;
where commission_pct is not null;

select last_name, commission_pct
from employees 
where commission_pct <=> null;   /*安全等于*/

select last_name, commission_pct, salary
from employees 
where salary <=> 12000;   /*安全等于*/

12、函数

#语法
select 函数名() from 表

13、单行函数

日期函数

/*now当前系统日期+时间
curdate当前系统日期
curtime当前系统时间
datediff(date1, date2)日期之差date1-date2
str_to_date 将字符转换成日期
date_format将日期转换成字符
*/

select now();
select curdate();
select curtime();
select year(now()) 年, month(now()) 月;
select year('1998-8-18') 年;
select monthname(now()) 月;
select datediff(now(), '1998-10-8');

#str_to_data:将日期格式的字符转换成指定格式日期
select str_to_date('1992-4-3', '%Y-%m-%d') output;
#查询入职日期为1992-4-3的员工信息
select * from employees where hiredate='1992-4-3';
select * from employees where hiredate=str_to_date('4-3-1992', '%m-%d-%Y');

#date-format:将日期转换成字符
select date_format(now(), '%Y年%m月%d日');
#查询有奖金的员工名和入职日期
select last_name, date_format(hiredate, '%m月/%d日 %Y年') 入职日期
from employees
where commission_pct is not null;

其它函数

/*version版本
database当前库
user当前连接用户*/

select version();
select database();
select user();

👋 感谢您的观看!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享