mysql数据库基础梳理

一、数据库

1. MySql数据库

1.1 安装和管理

使用dos命令:

  • 启动服务:net start mysql
  • 关闭服务:net stop mysql
  • 登录MySql:mysql -u用户名 -p密码

2. SQL语句操作数据库

2.1 DDL

2.1.1 操作database
-- 1.创建database
create database 库名称;

-- 2.删除database
drop database 库名称;

-- 3.使用database
use 库名称;
-- 4.查看使用的是哪个database
select database();

-- 5.列出所有库
show databases;
-- 6.查看库创建信息
show create database 库名称;
2.1.2 操作table
-- 1.创建表
create table 表名称(
	字段名 类型 [约束],
    ...
    字段名 类型 [约束]
);

-- 2.查看表
show tables; -- 列出所有表
show create table 表名称; -- 查看表创建信息
desc 表名称; -- 查看表结构

-- 3.删除表
drop table 表名称;

-- 4. 修改表
-- 4.1 修改表名称
rename table 表名称 to 新名称;
-- 4.2 修改表:添加列
alter table 表名称  add 字段名 类型 [约束];
-- 4.3 修改表:修改列类型
alter table 表名称  modify 字段名 新类型 [约束];
-- 4.4 修改表:修改列名称
alter table 表名称  change 字段名  新字段名 类型 [约束];
-- 4.5 修改表:删除字段
alter talbe 表名称 drop 字段名;

2.2 DML

-- 1.插入数据
insert into 表名称(字段1,字段2,...) values (1,2,...);
-- 2.修改数据
update 表名称 set 字段1=1, 字段2=2,... where 条件
-- 3.删除数据
delete from 表名称 where 条件;

2.3 DQL

2.3.1 单表查询
-- 1 简单查询
select * from 表名称; -- 查询表里所有数据
select 字段1,字段2,... from 表名称; -- 查询表里指定字段的数据
select 字段1+字段2,... from 表名称; -- 查询,并运算
select ifnull(字段, 默认值) from 表名称; -- 查询并处理null值
select 字段1 as 别名1, 字段2 as 别名2,... from 表名称; -- 查询并起别名,as可以省略

-- 2.条件查询: =, >, <, >=, <=, <>, like, between..and.. , in(), and, or, not
select * from 表名称 where 字段=;
select * from 表名称 where 字段  like '张%'; -- %表示任意个任意字符; _ 表示一个任意字符
select * from 表名称 where 字段 between 起始值 and 结束值; -- 包含起始值和结束值
select * from 表名称 where 字段 in(1,2,..); -- 集合查询

select * from 表名称 where 条件1 and 条件2...;
select * from 表名称 where 条件1 or 条件2...;
select * from 表名称 where not(条件1);

-- 3.聚合函数:聚合函数会忽略null值
select count(*) from 表名称; -- 查询数量
select sum(字段) from 表名称; -- 求和
select avg(字段) from 表名称; -- 求平均值
select max(字段) from 表名称; -- 求最大值
select min(字段) from 表名称; -- 求最小值

-- 4.排序查询:order by 字段名 排序规则
select * from 表名称 order by 字段 DESC; -- 降序
select * from 表名称 order by 字段 ASC; --升序,是默认的排序规则

-- 5.分组查询:group by 分组字段 having 分组后过滤条件
select 分组字段,聚合函数 from 表名称 where 条件 group by 分组字段 having 分组后的过滤条件

-- 6.分页查询
select * from 表名称 limit 起始索引,查询数量;
2.3.2 多表查询
-- 1. 内连接查询。查询必定有关联的数据,无关数据是查询不出来的
-- 1.1 显式内连接
select * from1 inner join2 on 关联条件 where 过滤条件;
-- 1.2 隐式内连接
select * from1,2 where 关联条件 and 过滤条件;

-- 2. 外连接查询。查询一张表的全部数据,及另外一张表的关联数据
-- 2.1 左外连接:查询左表的全部数据
select * from1 left join2 on 关联条件 where 过滤条件;
-- 2.2 右外连接:查询右表的全部数据
select * from1 right join2 on 关联条件 where 过滤条件;

-- 3. 子查询。是查询技巧,没有固定语法,是查询的嵌套
-- 3.1 子查询结果是一个值
select * from emp where salary = (select max(salary) from emp);
-- 3.2 子查询结果是一个集合:查询工资大于5000的员工,所在的部门信息
select * from dept where id in (select dept_id from emp where salary > 5000);
-- 3.3 子查询结果是一张虚拟表:查询工资大于5000的员工信息和部门信息
select * from dept d, (select * from emp where salary> 5000) t where d.id =t.dept_id;

2.4 DCL

-- 创建用户
create user '用户名'@'%' identified by '密码';
-- 用户授权
grant all on *.* to '用户名'@'%';
-- 查看权限
show grants for '用户名'@'%';
-- 取消授权
revoke delete on *.* from '用户名'@'%';
-- 删除用户
drop user '用户名'@'%';
-- 修改密码
set password for '用户名'@'%' = password ('新密码');

2.5 TCL

-- 1.开启事务
start transaction;  -- 或者写成:set autocommit = 0;
-- 2. 执行多条DML语句
.....
-- 3. 关闭事务
-- 3.1 提交事务:
commit;
-- 3.2 回滚事务
rollback;

3. JDBC+预编译对象

  • 不带事务管理的步骤
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection conn = DriverManager.getConnection("url","username","password");
//3.创建SQL执行平台
PreparedStatement pstmt = conn.prepareStatement("使用?处理过的SQL语句");
pstmt.setXXX(参数序号, 参数值);
//4.执行SQL语句  执行DML:executeUpdate(), 执行任意:execute()
ResultSet rs = pstmt.executeQuery();
//5.处理结果
while(rs.next()){
    XXX 值 = rs.getXXX("列名称");
}
//6.释放资源
rs.close();
pstmt.close();
conn.close();
  • 带事务管理的步骤
Connection conn =null;
PreparedStatement pstmt = null;
try{
    //1.注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    //2.获取连接
    conn = DriverManager.getConnection("url","username","password");
    // ====开启事务====
    conn.setAutoCommit(false);
    
    //3.创建SQL执行平台
    pstmt = conn.prepareStatement("使用?处理过的SQL语句");
    pstmt.setXXX(参数序号, 参数值);
    //4.执行SQL语句  执行DML:executeUpdate(), 执行任意:execute()
    int count = pstmt.executeUpdate();
    //5.处理结果
    System.out.println(count);
    
    //===提交事务===
    conn.commit();
}catch(Exception e){
    //===回滚事务===
    conn.rollback();
    e.printStackTrace();
}finally{
    //6.释放资源
    pstmt.close();
    conn.close();
}

4. 连接池

4.1 c3p0连接池的使用

  1. 导入jar包:c3p0-*.jar, mchange-*....jar, 数据库驱动包

  2. 提供配置文件:

    • 文件名称:c3p0-config.xml
    • 文件位置:类加载路径下(src下)
    <c3p0-config>
        <default-config>
            
            
            <property name="driverClass">com.mysql.jdbc.Driverproperty>
            
            <property name="jdbcUrl">jdbc:mysql:///hupuproperty>
            
            <property name="user">rootproperty>
            
            <property name="password">rootproperty>
    
            
            
            <property name="checkoutTimeout">30000property>
            
            <property name="initialPoolSize">10property>
            
            <property name="maxIdleTime">30property>
            
            <property name="maxPoolSize">100property>
            
            <property name="minPoolSize">10property>
        default-config>
    
        
        <named-config name="heima">
            <property name="driverClass">com.mysql.jdbc.Driverproperty>
            <property name="jdbcUrl">jdbc:mysql:///hupuproperty>
            <property name="user">rootproperty>
            <property name="password">rootproperty>
        named-config>
    c3p0-config>
    
  3. 编写代码

    //1. 创建连接池对象
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //2. 从连接池中获取连接
    Connection conn = dataSource.getConnection();
    //3.使用连接操作数据库
    //...
    //4. 释放资源:把连接归还到连接池
    conn.close();
    

4.2 druid连接池的使用

  1. 导入jar包:druid-*.jar,数据库驱动包

  2. 提供配置文件

    • 文件名称:xxx.properties
    • 文件位置:建议放在类加载路径下(src下)
    #数据库的url地址
    url=jdbc:mysql:///hupu
    #数据库的用户名
    username=root
    #数据库密码
    password=root
    #数据库的驱动类名
    driverClassName=com.mysql.jdbc.Driver
    #连接池初始化容量
    initialSize=30
    #连接池最大容量
    maxActive=50
    #连接池最小容量
    minIdle=10
    
  3. 编写代码使用连接池

    //1. 创建连接池对象
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties对象);
    //2. 从连接池中获取连接
    Connection conn = dataSource.getConnection();
    //3. 使用连接操作数据库
    //...
    //4. 释放资源:归还到连接池
    conn.close();
    

5. JDBCTemplate

5.1 使用步骤

  1. 导入jar包:数据库驱动包、连接池的jar包、JDBCTemplate的jar包

  2. 提供连接池的配置文件、和工具类

  3. 编写代码使用JDBCTemplate

    //1. 创建JDBCTemplate对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate(连接池对象);
    //2. 使用JdbcTemplate执行SQL语句
    

5.2 常用执行SQL的API

//1. 执行任意语句:没有返回值
jdbcTemplate.execute("create table....");

//2. 执行DML语句:返回影响的行数
int count = jdbcTemplate.update("sql语句", 参数值);

//3. 执行DQL语句:
//3.1 查询一个值:通常用于查询数量
Integer value = jdbcTemplate.queryForObject("sql",Integer.class, 参数值);
//3.2 查询一条数据,得到Map
Map<String, Object> map = jdbcTemplate.queryForMap("sql", 参数值);
//3.3 查询多条数据,得到List
List<Map<String,Object>> mapList = jdbcTemplate.queryForList("sql", 参数值);
//3.4 查询多条数据,得到JavaBean的集合
List<JavaBean> list = jdbcTemplate.query("sql",new BeanPropertyRowMapper<>(JavaBean类名.class), 参数值);
//3.5 查询一条数据,得到JavaBean对象
Javabean obj = jdbcTemplate.queryForObject("sql", new BeanPropertyRowMapp<>(JavaBean类名.class), 参数值);

你可能感兴趣的:(基础)