Mysql数据库
DBMS
- DataBaseManagementSystem,数据库管理系统,俗称数据库软件
常见的DBMS
- MySQL: Oracle公司产品, 08年被Sun公司收购了,09年Sun公司被Oracle收购了,MySQL开源产品,市占率排名第一
- Oracle: Oracle公司产品, 闭源产品,性能最强价格最贵, 市占率排名第二
- SQLServer: 微软公司产品,闭源产品,市占率第三
- DB2: IBM公司产品,闭源产品
- SQLite:轻量级数据库
SQL
- Structured Query Language,结构化查询语言, 通过此语言让程序员和数据库软件进行交流
使用时先下载好mysql的数据库软件,并打开输入用户名和密码
SQL语句分类
- DDL: 数据定义语言 包括数据库相关和表相关的SQL语句
- DML: 数据操作语言,包括增删改查.
- DQL: 数据查询语言,包括查询相关
- TCL: 事务控制语言,和事务相关的SQL语句.
- DCL:数据控制语言, 指用户相关和权限分配相关
数据类型
-
整数: int和bigInt bigInt等效Java中的long
-
浮点数: double(m,d) m代表总长度 d代表小数长度 , 25.444 m=5 d=3
-
字符串:
-
- char(m): 固定长度, m=5 存"abc" 占5个 最大长度255
- varchar(m): 可变长度,m=5 存"abc" 占3个 最大长度65535 内容少的用varchar
- text(m):可变长度 最大长度65535 内容长的用text
-
时间类型:
-
- date: 只能保存年月日
- time: 只能保存时分秒
- datetime: 年月日时分秒, 默认值为null, 最大值9999-12-31
- timestamp(时间戳,通过保存距离1970年1月1日的毫秒数来实现时间记录的): 年月日时分秒, 默认值为当前系统时间 , 最大值2038-1-19
相关语句
数据库相关语句:
-
创建数据库:create database 数据库名 charset=utf8/gbk;后面是指定数据库字符集
MariaDB [(none)]> create database ceshi charset=utf8; Query OK, 1 row affected (0.003 sec)
-
查询当前所有数据库:show databases;
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | ceshi | | information_schema | | mall_ams | | mall_pms | | mysql | | performance_schema | | test | +--------------------+ 7 rows in set (0.001 sec)
-
查询数据库信息:show create database 数据库名;
MariaDB [(none)]> show create database ceshi; +----------+----------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------+ | ceshi | CREATE DATABASE `ceshi` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+----------------------------------------------------------------+ 1 row in set (0.001 sec)
-
删除数据库:drop database 数据库名;
MariaDB [ceshi]> drop database ceshi; Query OK, 0 rows affected (0.032 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mall_ams | | mall_pms | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.001 sec)
-
使用数据库:use 数据库名;
MariaDB [(none)]> use ceshi; Database changed MariaDB [ceshi]>
表相关的语句:
注:在进行表的相关操作时需要先使用数据库,否则会报错。
-
创建表:create table 表名(字段1名 类型,字段2名 类型) charset=utf8/gbk;
MariaDB [shiyan]> create table person(name varchar(50),age int); Query OK, 0 rows affected (0.039 sec)
-
查询所有表:show tables;
MariaDB [shiyan]> show tables; +------------------+ | Tables_in_shiyan | +------------------+ | person | +------------------+ 1 row in set (0.001 sec)
-
查询表信息:show create table 表名;
MariaDB [shiyan]> show tables;
-
查询表字段信息: desc 表名;
MariaDB [shiyan]> desc person;
-
修改表名:rename table 原名 to 新名;
MariaDB [shiyan]> rename table person to person1; Query OK, 0 rows affected (0.021 sec)
-
添加表字段:
- 最后面添加格式: alter table 表名 add 字段名 类型;
- 最前面添加格式: alter table 表名 add 字段名 类型 first;
- 在xxx字段的后面添加: alter table 表名 add 字段名 类型 after xxx;
MariaDB [shiyan]> alter table person1 add sex int; Query OK, 0 rows affected (0.012 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [shiyan]> alter table person1 add ppts date first; Query OK, 0 rows affected (0.007 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [shiyan]> alter table person1 add salary int after name; Query OK, 0 rows affected (0.006 sec) Records: 0 Duplicates: 0 Warnings: 0
-
删除表字段:alter table 表名 drop 字段名;
MariaDB [shiyan]> alter table person1 drop salary; Query OK, 0 rows affected (0.005 sec) Records: 0 Duplicates: 0 Warnings: 0
-
修改表字段:alter table 表名 change 原名 新名 新类型;
MariaDB [shiyan]> alter table person1 change ppts pers int; Query OK, 0 rows affected (0.044 sec) Records: 0 Duplicates: 0 Warnings: 0
-
删除表:drop table 表名;
MariaDB [shiyan]> drop table person1; Query OK, 0 rows affected (0.011 sec)
数据相关的语句
注:在进行表的相关操作时需要先使用数据库并且创建好保存数据的表,否则会报错。
-
插入数据:
-
全表插入:insert into 表名 values(值1,值2);
MariaDB [shiyan]> insert into ceshi values("zhangsan",10,"san"); Query OK, 1 row affected (0.007 sec)
-
指定字段插入:insert into 表名(字段1名,字段2名) values (值1,值2);
MariaDB [shiyan]> insert into ceshi(name) values("lisi"); Query OK, 1 row affected (0.003 sec)
-
批量插入:insert into 表名(特定字段/全插入)values(值1,值2),(值1,值2);
MariaDB [shiyan]> insert into ceshi(name) values("wangwu"),("zhaoliu"); Query OK, 2 rows affected (0.002 sec) Records: 2 Duplicates: 0 Warnings: 0
-
插入中文:insert into 表名 values(“中文”,17);
MariaDB [shiyan]> insert into ceshi values("小白",18,"白白"); Query OK, 1 row affected (0.003 sec)
如果执行上面的SQL语句报以下错误, 执行 set names gbk; 之后再次执行
-
-
查询数据:select 字段信息 from 表名 where 条件;
-
查询所有信息:一般来说是不建议使用*来查询的,建议填写查询具体字段,本次只是为了测试
MariaDB [shiyan]> select * from ceshi;
-
查询固定信息:
MariaDB [shiyan]> select name from ceshi;
-
根据条件查询信息:
MariaDB [shiyan]> select age from ceshi where age>10;
-
根据条件查询多个信息:
MariaDB [shiyan]> select name,age,nick from ceshi where age>10;
-
-
修改数据:update 表名 set 字段名=值,字段名=值 where 条件;
MariaDB [shiyan]> update ceshi set age=20 where name="lisi";
-
删除数据:delete from 表名 where 条件;
MariaDB [shiyan]> delete from ceshi where age is null;
数据库语句的一些属性和查询
主键约束 primary key
- 约束: 创建表时给表字段添加的限制条件
- 主键: 表示数据唯一性的字段称为主键
- 主键约束: 唯一且非空
主键约束+自增 auto_increment
- 自增规则:从历史最大值加1
- 添加后可以为空,会自动加1,如果删除后相关数据后再添加则是在历史最大的过程中加1,例如之前id最大值为10,将10删除后在添加会在10的基础上加1,如果你指定为20,删除后在添加则会在20的基础上加1.
比较运算符:<(小于) >(大于) <=(小于等于) >=(大于等于) =(等于) !=和<>(不等于)
比较运算符和生活中的比较运算符相差不大,一般用于while后的判断条件,最后的不等于是两种方式都可以,进行操作时选择其一即可。
and or not 与 或 非
-
and: 查询多个条件同时满足时使用
MariaDB [shiyan]> select name from ceshi where age>15 and loc_id=2;
-
or : 查询多个条件满足一个条件时使用
MariaDB [shiyan]> select name from ceshi where age>15 or loc_id=2;
-
not: 取反 例如查询昵称不为空的人的姓名
MariaDB [shiyan]> select name from ceshi where nick is not null;
between x and y 两者之间,包括x和y
-
查询在两者之内的数据
MariaDB [shiyan]> select * from ceshi where age between 10 and 20;
-
查询在两者之外的数据
MariaDB [shiyan]> select * from ceshi where age not between 10 and 20;
in(x,y,z) 当查询某个字段的值为多个的时候使用
-
MariaDB [shiyan]> select * from ceshi where age in(10,20,30);
distinct 去重
-
MariaDB [shiyan]> select distinct age from ceshi where loc_id=2;
like 模糊查询
-
_: 代表1个未知字符
-
%: 代表0或多个未知字符
-
例:
-
-
以x开头 x%
-
以x结尾 %x
-
以x开头y结尾 x%y
-
包含x %x%
-
第二个字符是x _x%
-
第三个是x倒数第二个是y _ X%y _(此处没有空格,因为不空格会被注释掉,所以添加)
MariaDB [shiyan]> select * from ceshi where name like "李%";
-
order by 排序
-
order by 排序的字段名 asc升序(默认)/desc降序;
-
如果加条件查询的话需要将条件添加在order by 的前面
MariaDB [shiyan]> select name,age from ceshi order by age; //不写的话是默认升序排列的,第一种和第二种效果一样。 MariaDB [shiyan]> select name,age from ceshi order by age asc; MariaDB [shiyan]> select name,age from ceshi order by age desc;
limit 分页查询
-
格式: limit 跳过的条数,请求的条数(每页的条数)
-
跳过的条数=(请求的页数-1)*每页的条数
-
例:
-
-
第一页的5条数据 limit 0,5
-
第2页的5条数据 limit 5,5
-
第3页的第5条数据 limit 10,5
-
第4页的第7条数据 limit 21,7
MariaDB [shiyan]> select * from ceshi order by age limit 0,3;
-
别名:
- select name as “名字” from emp;
- select name “名字” from emp;
- select name 名字 from emp;
- 三种方式都可以,选择自己比较喜欢的的一种方式就好。
聚合函数
-
统计查询:对查询到的数据进行统计查询
-
avg 求平均值
MariaDB [shiyan]> select avg(age) from ceshi where loc_id=2;
-
max 最大值
MariaDB [shiyan]> select max(age) from ceshi;
-
min 最小值
MariaDB [shiyan]> select min(age) from ceshi;
-
sum 求和
MariaDB [shiyan]> select sum(age) from ceshi;
-
count(*) 统计数量
MariaDB [shiyan]> select count(*) from ceshi where loc_id=2;
-
-
分组查询:可以将某个字段相同的划分为一组,然后以组为单位进行查询
-
MariaDB [shiyan]> select loc_id,count(*) from ceshi group by loc_id;
having
-
where后面只能写普通字段的条件, 不能写聚合函数条件
-
having后面专门用来写聚合函数条件, 而且having要和group by 结合使用 写在group by的后面
-
查询每个部门的平均工资,只查询出平均工资大于2000
MariaDB [empdb]> select dept_id,avg(sal) from emp group by dept_id having avg(sal)>2000;
子查询(嵌套查询)
如果一个语句不能完成,需要用两句话来进行说明时,但两句话查询的内容有关联,则可以使用子查询来进行合并;
例:查询loc_id为2的高于年龄平均值的人
查询loc_id为2的人的平均年龄:select avg(age) from ceshi where loc_id=2
查询大于平均年龄:select * from ceshi where age>(select avg(age) from ceshi where loc_id=2);
数值计算:+ - * / %
在查询或修改的后面可以添加相关的计算
例:给loc_id为2的人的年龄加十岁
update ceshi set age=age+5 where loc_id=2;
查询每个人年龄扩大一倍后的数值:
select name,age,age*2 年龄 from ceshi;
关联查询
关联关系:
创建表的时候,表和表之间存在的几种业务关系
- 一对一:有AB两张表,A表中的一条数据对应B表中的一条数据, 同时B表中的一条数据也对应A表中的一条数据.
- 一对多:有AB两张表,A表中的一条数据对应B表中的多条数据, 同时B表中的一条数据对应A表中的一条数据.
- 多对多:有AB两张表,A表中的一条数据对应B表中的多条数据, 同时B表中的一条数据也对应A表中的多条数据.
如果表之间存在关联关系,应如何建立关系
- 一对一:在两张表的任意一张表中添加建立关系的字段指向另外一张表的主键
- 一对多:在多的表中添加建立关系的字段指向另外一张表的主键
- 多对多:新建一张关系表,表中至少有两个字段用于建立关系, 指向另外两张表的主键
关联查询的三种方式:
-
等值连接: select 字段信息 from A,B where A.xxx=B.xxx(关联关系) and 其它条件
例:select emp.name,sal,dept.name from emp ,dept where emp.dept_id=dept.id;
-
内连接:select 字段信息 from A join B on A.x=B.x where 其它条件
例:select e.name,sal,d.name,loc from emp e join dept d
on e.dept_id=d.id where sal>2000;
-
外连接:select 字段信息 from A left/right join B on A.x=B.x where 其它条件
例:select e.name,sal,d.name from emp e left join dept d on e.dept_id=d.id;
注:
- 如果需要同时查询多张表的数据使用关联查询
- 如果查询到的是两张表的交集数据使用等值连接或内连接(推荐)
- 如果查询的是一张表的全部和另外一张表的交集则使用外连接
idea中配置连接Mysql步骤:
-
点击左侧边框的Database,点出后在+号下选择Data Source,点击后选择Mysql
-
在页面中输入用户名和密码。然后点击Test Connection 如果不能点击则需要先下载驱动
-
之后会显示为一个文件,输入你要连接的数据库,在代码页面写use 数据库名;执行后连接成功会在上方进行显示。