MySQL数据库常用语法|JDBC、Template常用方法归纳

数据库语法归纳

第一大块:入门四大类别数据库操作(DDL DML DQL DCL)

一、DDL

  • a.数据库

create database if not exists 数据库名 character set 字符编码格式;
drop database if exists 数据库名;
show databses;
select database();
show create database 数据库名;
alter database 数据库名 character set 新字符编码格式;
use 数据库名;

  • b.表

create table 表名(字段名 字段类型,…);
create table 新表名 like 目标表名;
drop table 表名;
show tables;
desc 表名;
alter table 表名 rename to 新表名;
alter table 表名 字段名 character set 新字段名;
alter table 表名 add 字段名 字段数据类型;
alter table 表名 modify 字段名 字段新数据类型;
alter table 表名 change 字段名 新字段名 字段新数据类型;
alter table 表名 drop 字段名;

二、DML

insert into [表名(字段名…)] values(字段值);
delete from 表名 [where 条件];
truncate tabale 表名;
update 表名 set 字段名 = 字段值… [where 条件];

三、DQL

select 字段… distinct、as、空格、ifnull(字段名,字段值)、算术运算符、聚合函数
from 表名… as、空格
where 条件… and、between and、or、in(值…)、is(null)、isnot(null)、比较运算符、逻辑运算符
order by 字段 asc… asc、desc
group by 字段… [having 同where条件,额外可添加聚合函数]
limit 开始索引 信息条数

四、DCL

create user ‘’@’’ identified by ‘’;
drop user ‘’@’’;
update user set password=password(’’) where user=’’;
set password for ‘’@’’=password(’’);
show grants for ‘’@’’;
grant 权限列表 on 数据库.表名 to ‘’@’’; 可以使用通配符,权限列表字段可以使用all关键字代表所有
revoke 权限列表 on 数据库名.表名 from ‘’@’’;
第二大块 约束

一、非空约束

not null
alter table 表名 modify 字段名 数据类型 [not null] 通过modify添加或去除非空约束
alter table 表名 drop 字段名 [not null];
alter table 表名 add 字段名 数据类型 [not null]

二、唯一约束

unique
alter table 表名 modify 字段名 数据类型 [unique] 通过modify可以添加唯一约束,但是不可以删除
alter table 表名 drop 字段名 unique; 删除含有唯一约束的字段必须在后面添加修饰符
alter table 表名 add 字段名 数据类型 [unique]

三、主键约束

primary key
alter table 表名 modify 字段名 数据类型 [primary key] 通过modify可以添加主键约束,但是不可以删除
alter table 表名 drop 字段名 [primary key]; 删除含有唯一约束的字段必须在后面添加修饰符
alter table 表名 add 字段名 数据类型 [primary key]
auto_increment
alter table 表名 modify 字段名 数据类型 [primary key] [auto_increment] 通过modify可以添加或者删除自动增长属性,可以单独写自动增长属性
alter table 表名 drop 字段名 [primary key];
alter table 表名 add 字段名 数据类型 [primary key] [auto_increment];

四、外键约束

[constraint 外键名] foreign key(外键字段名) references 主表名(主表关联外键字段名)
alter table 表名 drop foreign key(外键名); 这里的删除外键操作还是比较特别的,需要额外记忆
alter table 表名 add [constraint 外键名] foreign key(外键字段名) 主表名(主表关联外键字段名); 创建外键也是有简写形式
第三大块 多表查询

一、内连接查询

  • a.显示内连接

select … from … where 条件

  • b.隐式内连接

select … from 左表 [inner] join 右表 on 条件;

二、外连接查询

left right
select … from 左表 left [outer] join 右表 on 条件;

三、子连接查询(就是select语句创建出来的表作为虚拟表或者条件)

  • a.单行单列

select … from … where [select语句] 条件

  • b.单行多列

select … from … where [select语句] in(值…)

  • c.多行多列

select … from …[select语句] where 条件
第四大块 事务、隔离级别

一、事务

start transaction;
commit;
rollback;

二、隔离级别

  • a.隔离级别

read uncommitted
read committed
repeatable read
serializable

  • b.设置

select @@tx_isolation;
set global transaction isolation level 隔离级别字段;

数据库JDBC

第一大块 五个对象

一、DriverManager

Class.forName(“com.mysql.jdbc.Driver”);
DriverManager.getConnection(jdbc:mysql//ip地址(域名):端口号/数据库名);

二、Connection

Statement createStatement();
PreparedStatement prepareStatement(String SQL语句);
void setAutoCommit(boolean 标识符); false代表开启事务
void commit();
void rollback();

三、Statement

int executeUpdate(String SQL语句);
ResultSet executeQuery(String SQL语句)

四、PreparedStatement

int executeUpdate();
void setXxx(参数);
ResultSet executeQuery();

五、ResultSet

T(数据) getXxx(参数);
第二大块 连接池、Spring框架

一、C3P0连接池

DataSource ComboPoolDataSource();
DataSource ComboPoolDataSource(Sring 配置文件内部设置种类名);
Connection getConnection(); //DataSorce对象调用
void close();//Connection对象调用

二、Druid连接池

DataSource DruidDataSourceFactory.createDataSource(pro); //参数是一个双列集合
Connection getConnection();
void close();

三、Spring JDBCTemplate数据库连接框架

Template template = new JdbcTemplate(ds); //这里的参数是连接池对象
update(); //返回值是int型,适用于Statement和PrepareStatement对象的语句
queryForMap(); //这个方法只能封装一个结果集,也就是一行数据
queryForList(); //封装进去的Map对象
query(); //用于封装一个对象到List集合中,有两种方式:1.自己定义封装方式使用 RowMapper重写其中抽象方法mapRow(ResultSet rs, int i) 2.使用已经写好的实现类 BeanPropertyRowMapper(emp.class),注意哦这个方法使用前需要先把你要分装的对象的成员变量装箱,否则一些基本类型的变量无法接收null值,那么就会报错。
queryForObject()//查询聚合函数

发布了21 篇原创文章 · 获赞 9 · 访问量 1486

猜你喜欢

转载自blog.csdn.net/qq_43230007/article/details/104156008