MYSQL

MYSQL

  • datatime

    YY-MM-DD HH:mm:ss 最常用的时间格式

  • timestamp

    时间戳 1970.1.1到现在的毫秒数

数据库的字段属性

Unsigned:

  • 无符号的整数

  • 声明该列不能声明为负数

zerofill:

  • 0填充的

  • 不足的位数,使用0填充

自增:

  • 通常理解为自增,自动在上一条记录基础上+1(默认)

  • 通常用来设计唯一的主键 ~index 必须是整数类型

  • 可以自定义设计主键自增的起始值和步长

非空 Null not null

  • 假设设置为not null,如果不赋值就会报错

默认:

  • 设置默认的值

  • sex,默认值为男,如果不指定值,为默认值

 

-- 修改表名
alter table student rename as teacher
-- 增加表的字段
alter table teacher add age int(11)
-- 修改表的字段
alter table teacher modify age varchar(11) -- 修改约束
alter table teacher change age age1 int(1) -- 字段重名
-- 删除表的字段
alter table teacher drop age1

DML

添加

-- 插入语句
-- insert into 表名({字段名1,字段名2})values('值1'),('值2')
​
insert into  teacher (`name`,`pwd`,`sex`,`address`) values('海绵宝宝1','11111','男','大法师')
​
insert into teacher VALUES(5,'派大星1','22222','女','小法师')
​
insert into teacher (`name`,`pwd`) VALUES ('奥特曼','33333')
​

修改

-- 修改学员名称 where条件
-- 不指定条件会改动所有表
update teacher set name ='xiao骗子'
​
-- 修改多个属性 逗号隔开
update teacher set name = 'da',address = '13123' where id =1
-- 通过多个条件定位 where
UPDATE teacher set sex ='奥特曼' where name ='xiao骗子' and id = 4

删除

-- 删除数据
delete from teacher
​
-- 删除指定数据
delete from teacher where id = 3
-- 清空数据 truncate 自增会清0 delete 不会清0
truncate teacher
​

查询DQL

-- 查询所有字段
select * from teacher
-- 查询指定字段
SELECT id,name from teacher
-- 起一个别名
select sex as '性别' from teacher as s
-- 函数 concat(a,b)
select CONCAT('姓名',name) as 名字 from teacher 
-- 去重
select DISTINCT name from teacher
-- 模糊查询 like 结合 %(0到任意个字符) _(一个字符)
select  name from teacher where name like 'xiao___'
select name from teacher where name like '%子%'
​
​
-- in 
select name from teacher where id in (1,5);
select name from teacher where sex in ('女');
select * from teacher where address is null;
​
​
​
操作 描述
Inner join 如果表中至少有一个匹配,就返回行
left join 会从左边表中返回所有的值,即使右表中没有匹配
right join 会从右表中返回所有的值,即使左表中没有匹配

on 交叉条件

排序

-- 排序 升序 ASC 降序DESC order by
-- limit 分页 limit 0,5   0是起始值,5是当前页面数据的数量
-- limit 5,5 第二页
select * from teacher
order by id DESC
limit 2,5

函数

-- 数学运算  绝对值
select ABS(-8)
select CEILING(9.4) -- 向上取整
​
select floor(9.4) -- 向下取整
​
select RAND() -- 返回一个0~1的随机数
select SIGN(10) -- 判断一个数的符号 负数返回-1 正数0
​
-- 字符串函数
select CHAR_LENGTH('猜猜我多长') -- 字符串长度
select CONCAT('1','2','3') -- 拼接字符串
select LOWER('AAA') -- 小写
select upper('aaa') -- 小写
select REPLACE('121','2','3') -- 替换出现的
​

# mysql 字符串函数各种复习
-- CONCAT(str1, str2):字符连接函数
-- UPPER(str):将字符串改为大写字母
-- LOWER(str):将字符串改为小写字母
-- LENGTH(str):判定字符串长度
-- SUBSTRING(str, a, b):提取字段中的一段,从字符串str的第a位开始提取,提取b个字符
-- LEFT(str, n):提取字符串最左边的n个字符
-- RIGHT(str, n):提取字符串最右边的n个字符
-- md5加密
update teacher set pwd = MD5(pwd)
​

聚合函数

函数名称 描述
count() 计数
sum() 求和
avg() 平均值
max() 最大
min() 最小

事务原则

原子性

要么都成功,要么都失败

一致性

事务前后的数据完整性保证一致

持久性

事务一旦提交不可逆,被持久化到数据库

隔离性

排除其他事务对本事务的影响


 

脏读

一个事务读取了另一个事务未提交的数据

不可重复度

在一个事务内读取表中的某一行数据,多次读取结果不同

虚读 (幻读)

是指在一个事务内读取到了别的事务插入的数据,导致前后读取不一致

-- mysql 默认事务自动提交
set autocommit = 0 -- 关闭
​
start TRANSACTION -- 事务开启
​
​
-- 提交 持久化 成功
COMMIT
-- 回滚 回到原来 失败
ROLLBACK
​
-- 事务结束
set autocommit = 1 -- 开启自动提交
​
SAVEPOINT -- 保存点名
ROLLBACK TO SAVEPOINT -- 回滚到保存点
RELEASE SAVEPOINT 保存点名

索引

帮助MYSQL高效获取数据的数据结构

  • 主键索引 primary key

    • 唯一的标识,主键不可重复,只能有一个主键

  • 唯一索引 unique key

    • 避免重复的列出现,可以重复

  • 常规索引 index/key

    • 默认的,index,key关键字来设置

  • 全文索引 fulltext

    • 在特定的数据库引擎下才有 MyISAM

    • 快速定位数据

三大范式

第一范式1NF:要求数据库的每一列都是不可分的

第二范式:都依赖主键,每张表只描述一件事

第三范式:确保数据表中的每一列数据都和主键直接相关而不能间接相关

JDBC

###

package com.lesson;
​
import java.sql.*;
​
public class JdbcFirstDemo {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2 用户信息和url
​
        String url = "jdbc:mysql://localhost:3306/db_ssm_hotel?userUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String password = "123456";
        //3 连接成功,数据库对象connection 代表数据库
        Connection connection = DriverManager.getConnection(url, username, password);
​
        //4 执行sql的对象statement
​
        Statement statement = connection.createStatement();
​
        //5 执行sql的对象statement 执行sql,查看返回结果
​
        String sql = "select * from teacher";
        ResultSet resultSet = statement.executeQuery(sql);
​
        while(resultSet.next()){
            System.out.println("id = " +resultSet.getObject("id"));
            System.out.println("name = " +resultSet.getObject("name"));
        }
        //6 释放连接
​
        resultSet.close();
        statement.close();
        connection.close();
    }
}
​

PrepareStatement

防止sql注入

package com.lesson2;
​
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
​
public class TestPSelect {
    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
​
        try {
            connection = JdbcUtil.getConnection();
            String sql = "select * from teacher where id = ?";
            //预编译 先写sql不执行
            ps = connection.prepareStatement(sql);
​
​
            ps.setInt(1,3);
​
            ResultSet resultSet1 = ps.executeQuery();
            if (resultSet1.next()){
                System.out.println(resultSet1.getString("name"));
            }
​
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        }finally {
////            JdbcUtil.relase(resultSet,statement,connection);
//        }
​
​
    }
}
​

 

posted @ 2022-03-20 13:52  每天--自然醒  阅读(56)  评论(0)    收藏  举报