MySQL

1. 数据库

就是用来存储数据的仓库,遵循一定数据格式的数据集合

1.1 特点

可以认为它是对文件系统的改进。它解决了不同操作系统之间,数据格式的兼容性问题。也就是说,只要是同一个数据库的数据文件,即使是从Windows迁移到了Linux上,也可以正常处理的。

1.2 分类

关系型数据库 对于java而言,一个类就可以对应一个表,一个类对象就可以对应表中一数据,一个成员变量可以对应表中的一个(字段),能够做到一一的映射,数据库中是以表作为基本单位的。除此之外,关系型数据库的关系主要体现在它自己的关系模型,反应了表与表之间的关系。

非关系型数据库 是存储键值对的,每个表之间不存在关联,都是独立的。但是它既有关系型数据库处理数据的方便性,还能处理大量数据,比较火的就是Redis、mongoDB

DCL数据控制语言(Data Control Language)

代表关键字:grant,revoke

DDL数据定义语言(Data Definition Language)

代表关键字:create ,drop,

DML数据操作语言(Data Manipulation Language)

代表关键字:insert,delete,update

DQL数据查询语言(Data Query Language) 

代表关键字:select

TCL事务控制语言(Transactional Control Language)

代表关键字:commit ,rollback;

2. 简单sql命令操作

select version(); 数据库用//查看版本号

show databases;  查看本地所有的数据库

select  ‘xxxx’;  查什么写什么

use test;  切换数据库

show tables;  查看所有的表

Exit  退出

3. 基础操作

创建用户:CREATE USER 'username'@'host' IDENTIFIED BY 'password';

授权:GRANT ALL ON  ''.'' TO 'username'@'%' ;

关闭权限 revoke All ON ''.'' from 'username'@'%';

-- 表中添加数据Insert   insert into 表名 (列名1,列名2) values (值1,值2);
insert into grp_table01(student_name,student_age) values (123,22);

-- 更改表名  alter table 表名 rename 新表名;
alter table grp_table01 rename grp_table_01;

-- 更改字段名,类型  alter table 表名 change 列名 新列名 数据类型;
alter table grp_table01 change student_name student_name int;

-- 添加字段  alter table 表名add 列名类型;
alter table grp_table_01 add student_age int;

-- 删除字段  alter table 表名 drop 列名;
alter table grp_table_01 drop student_age;

-- modify 更改字段类型  alter table 表名 modify 列名 新数据类型;
alter table grp_table_01 modify `name` int;

-- 唯一约束unique   alter table 表名 add unique (id);
create table practice_01(
id int,
name varchar(20)
);
alter table practice_01 add unique (id);

-- 主键  添加主键约束  alter table 表名 add primary key(列名,列名...);
create table person(
id int,
`name` varchar(100),
income decimal(18,2)
);
alter table person add primary key(id);

-- 非空约束 not null 与默认值 default   alter table 表名 modify 列名 数据类型 not null  default 默认值;
create table temp(
id int,
`name` varchar(30),
sex varchar(10)
);
alter table temp modify sex varchar(10) not null default '男';
alter table temp modify id int not null; 
alter table temp modify name varchar(30) not null default '123456';

-- 外键约束  alter table 表名 add foreign key (外键列列名) references 指向的表名 (主键列列名);
create table student1 (
    id int ,
    name varchar(20),
    teacher_id int,
    primary key (id)
);
create table teacher1(
    id int ,
    name varchar(20),
    primary key (id)
);
alter table student1 add foreign key (teacher_id) references teacher1 (id);

-- 主键自增auto_increment   alter table 表名modify 主键列名 类型 auto_increment;
create table person1(
    id int ,
    name varchar(200),
     primary key(id)
);
alter table person1 modify id int auto_increment;

-- and 且、和   select 列限定 from 表限定 where A表达式 and B表达式;
-- 查询学生表中,name是张三且成绩大于90分
select * from student_test where name = '张三' and score > 90;

-- or 或    select 列限定 from 表限定 where A表达式 or B表达式;
-- 查询学生表中,name是张三 或 成绩大于90分
select * from student_test where name = '张三' or score > 90;

-- 关系表达式 注意 : 判断是否为空,应该使用is null,而不是 = null同理,判断不为空应该使用 is not null ,而不是 <>null,并且and和or同时出现的话,and优先级比or要高

-- between.and. 在..之间    select 列限定 from 表限定 where 列名 between 值1 and 值2;
-- 查询分数在97到98之间
select * from student_test where score between 97 and 98;

-- in 在指定数据中   select 列限定 from 表限定 where 列名 in(值1,值2....);  
select * from student_test where score in(97,98);

-- 模糊查询 like    select 列限定 from 表限定 where 列名 like  '值' ;
-- 其中 % 匹配任意个数的任意字符  _ 匹配单个任意字符  
-- 如果想要查询 _ 或者 % 需要转义  \%   \_

select * from student_test where name like '_明';

-- order by 排序    select 列限定 from 表限定 order by 列名 asc/desc;  asc升序
-- 按成绩排序
select * from student_test order by score asc;

-- limit 限制条数    
-- select 列限定 from 表限定 limit 条数;
-- select 列限定 from 表限定 limit 开始值(不包含) ,条数;

select * from student_test order by score asc limit 2;
select * from student_test order by score asc limit 2 , 1;  

4. 组运算

最大值    select max(score) from student

最小值    select min(score) from student

求和    select sum(score) from student

平均值    select avg(score) from student

查询分组 ,分组 as 取别名 group by 表示按什么分组

select teacher_id , count(*) as '个数' from student as s group by s.teacher_id

having 过滤 将分组后的组,按某种情况过滤

select teacher_id , count(*) as '个数' from student as s group by s.teacher_id having thacher_id>1

union 合并查询,去除重复项  、  union all 合并查询,不去除重复项(列字段数量必须一致)

select * from student where id=1

union all

select * from student where name= '张三'

5. 常用函数

select version() ;显示当前MySQL软件的版本

select database();显示当前所处数据库是哪个

select  char_length('中国');返回字符个数。

select  length('中国');返回字符所占字节数,MySQL中,一个UTF8编码的汉字占3个字节

select  concat(  'a',  'b',  'c',  'd');返回  'abcd'。字符串拼接函数

select  concat_ws(  '=',  'a',  'b',  'c');返回  'a=b=c'。字符串拼接函数,第一个是拼接间隔符

select   upper('abcd');返回ABCD。将参数中所有小写字母转换为大写

select  lower('ABCD');返回abcd。将参数中所有大写字母转换为小写

select  substring(  '系统信息类',  1,  3  );返回  系统信。第2个参数代表从1开始的第几个字符,第3个参数代表截取字符个数

select  trim('  abc  ');返回 abc。用于删去参数左右的所有空格

select  curdate();返回当前日期

select  curtime();返回当前时间

select  now();返回当前日期时间

select  unix_timestamp();返回当前日期时间对应的时间戳(单位秒)

select  unix_timestamp('2018-05-24 20:00:00');返回参数指定的日期时间对应的时间戳(单位秒)

select  from_unixtime(1527163397);返回参数指定时间戳(单位秒)对应的日期时间

select  datediff(  '2018-05-23',  now()  );返回两个参数对应日期相差的天数(用第一个参数减第二个参数)

select  adddate( now(), -2 );返回指定天数前/后的日期时间(第一个参数是日期时间,第二个参数是天数,向后加是正数,向前减是负数)

select year('2019-02-24');返回2019 获得年份

select month('2019-02-24')  返回2 获得月份

select day('2019-02-24')  返回 24 获取日

select  if(  <判断条件>,  <条件为真时的返回值>,  <条件为假时的返回值>  );相当于Java中的三目运算符<判断条件>  ?  <条件为真的返回值>  :  <条件为假的返回值>。

如select  if(1=1,  2,  3);返回2。

select  ifnull(<表达式或者字段>,  <表达式或者字段为NULL时的返回值>);通常用于给有可能有NULL的情况下的提供默认值。

select ifnull(null,'无名氏') ; null这里可以写列名 就会把该列值为null的 以无名氏显示

select ifnull(name,'无名氏') from teacher ;

select concat(upper(substring(name,1,1)),lower(substring(name,2,char_length(name)-1)) )from student

6. 复杂查询

6.1 case  when  then

select *, (
case rank
when 'A' then '优秀'
when 'B' then '良好'
when 'C' then '差'
end
)as rank_ch
from (
select * ,
(case
when  score < 60 then 'C'
when score >=60 and score <80 then 'B'
when score >=80 then 'A'
end
)as rank
from student
) a;

6.2 行转列

6.2.1 方法一

select name,
min(
case course
when 'java' then score
end
) as 'java',
min(
case course
when 'MySql' then score
end
) as 'MySql'
from hang_lie
group by name

6.2.2  方法二

select name , group_concat(course,'=', score separator '|') as '各科成绩' from hang_lie group by name


6.3  连接查询

select *
from student s
inner join teacher t on t.teacher_id=s.teacher_id
 
select *
from student s
left join teacher t on t.teacher_id=s.teacher_id
 
select *
from student s
right join teacher t on t.teacher_id=s.teacher_id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值