Mysql、Jdbc

MySQL数据库
运行打开方式:mysql -u root -p
SQL语句对数据库进行CRUD(增删改查)操作
创建数据库:create database 数据库名称;
create database 数据库名称 character set 字符集;(直接定义数据哭的编码集)
查看所有数据库:show databases;
查看某个数据库的信息:show create database 数据库名称;
删除数据库:drop database 数据库名称;
修改数据库:alter database 数据库名称 character set 字符集;(修改数据库的编码)
切换数据库:use 数据库
查看当前使用的数据库:select database();
SQl语句对数据库中表进行CRUD操作
创建表:create table 表名(字段名 类型(长度) 约束,字段名 类型(长度) 约束,…);
创建表时注意:
创建表之前要现选择数据库
主键约束(一个表中只能有一个主键且值肯定是唯一的):primary key
auto_increment自动增长,从1开始(只能用在主键上)
唯一约束(值必须是唯一的,但是一个表中可以有多个唯一的字段):unique
非空约束(值不能为空):not null
查看数据库中所有表:show tables;
查看表结构:desc 表名;
查看表创建信息:show create table 表名;
删除表:drop table 表名;
修改表:
alter table 表名 add 列名 类型(长度) 约束; 添加列
alter table 表名 modify 列名 类型(长度) 约束; 修改列的类型长度及约束
alter table 表名 change 旧列名 新列名 类型(长度) 约束; 修改列名,类型,长度,约束
alter table 表名 drop 列名; 删除列
rename table 表名 to 新表名; 修改表名
alter table 表名 character set 字符集; 修改表的字符集
SQL语句对表中数据进行操作
插入某些列数据:insert into 表名 (列…) value(值…)
插入所有列数据:insert into 表名 value(值…)
修改记录:update 表名 set 字段名=值,…where 条件;
like :占位符% _
in :范围
and 相当于且
or:相当于或
删除记录:delete from 表名 where 条件; (删除后会有痕迹遗留,可以回滚)
truncate table 表名;(删除后无痕迹遗留,不可以回滚)
回滚:
start transaction;开启事务
commit;提交事务
rollback;回滚事务
查询:select…
外键约束
表已存在外键约束:
alter table product add foreign key (category_id) references category(category_id);
创建表的时候直接外键约束:
create table product(product_id int primary key auto_increment,name varchar(20),price float,category_id int,foreign key (category_id) references category(category_id));
查询: 内连接查询(inner可以省略)
select * from product p inner join category c on p.category_id=c.category_id;
select * from product p join category c on p.category_id=c.category_id;
select * from product p,category c where p.category_id=c.category_id;
左外连接(outer可以省略)
select * from product p left outer join category c on p.category_id=c.category_id;
右外连接(outer可以省略)
select * from product p right outer join category c on p.category_id=c.category_id;
子查询:
查询“手机产品”分类的商品信息:
首先查询出“手机产品”对应的分类id:select category_id from category where name=‘手机产品’;
再拿着"手机产品的id"去产品表中查:select * from product where category_id in(select category_id from category where name=‘手机产品’);
分页查询:limit a,b;从a开始查b条出来
select * from product limit 0,5;
select * from product limit 5,5;

JDBC写法:
// 1.注册驱动
// DriverManager.registerDriver(new Driver());
Class.forName(“com.mysql.cj.jdbc.Driver”);
// 2.获取数据的连接对象
Connection conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/web02?serverTimezone=Asia/Shanghai”, “root”, “123456”);
// 3.获取用于执行静态SQL语句的Statement
Statement statement = conn.createStatement();
// 4.执行SQL语句
int i = statement.executeUpdate(“insert into user values(null,‘zhangsan’,‘112233’,‘女’)”);
System.out.println(i);
// 5.关闭资源
conn.close();
创建连接池(自定义)
开源的连接池:DBCP、c3p0、druid
创建DBUtils工具类
开源的DBUtils工具类:mysql-connector-java-8.0.11

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值