增删改查
修改删除表:
SHOW CREATE DATABASE school --查看创建数据库的语句
SHOW CREATE TABLE student – 查看student数据表的定义数据
DESC student – 显示表的结构
– 修改表名 ALTER TABLE 旧表名 RENAME AS 新表名
ALTER TABLE student RENAME AS student1
– 增加表的字段 ALTER TABLE 表名 ADD 字段名 列属性
ALTER TABLE student1 ADD age INT(11)
– 修改表的字段(重命名,修改约束)
ALTER TABLE 表名 MODIFY 字段名 列属性[]
ALTER TABLE student1 MODIFY age VARCHAR(11) – 修改约束
ALTER TABLE 表名 CHANGE 旧名字 新名字 列属性[]
ALTER TABLE student1 CHANGE age age1 INT(1) – 字段重命名
– 删除表的字段
ALTER TABLE 表名 DROP 字段名
ALTER TABLE student1 DROP age1
– 删除表(如果表存在再删除)
DROP TABLE IF EXISTS student1
插入语句(添加)
insert into 表名([字段名1,字段名2,字段名3,…])values(‘值1’),(‘值2’),(‘值3’) ,…)
注意事项:1、字段的值可以省略,但值需要一一对应;
2、可以同时插入多条数据;
修改语句
UPDATE 表名 SET colnum_name = value,[colnum_name = value,…] WHERE 条件;
– 修改学员名字,带了简介;
UPDATE ‘student’ SET ‘name’=‘狂神’ WHERE id = 1;
– 不指定条件的情况下,会改动所有表;
UPDATE ‘student’ SET ‘name’=‘狂神’
删除语句
delete from 表名 where id=1 (不会影响自增)
truncate table 表名 (自增计数器归零)
DQL
查询select
4.1、指定查询字段 (select 字段 from 表)
– 查询全部学生
select * from student
– 查询指定字段
select ‘studentno’,‘studentname’ from student
– 别名,给结果起一个名字 as 可以给字段或者表起别名
select ‘studentno’ as 学号,‘studentname’ as 学生姓名 from student as s
– 函数 concat(a,b)
select concat('姓名: ',studentname) as 新名字 from student
去重 (distinct)去除select查询出的结果中有重复的
select distinct ‘studentno’ from result
操作数据库的列(表达式0
select 表达式 from 表
模糊查询
is null
is not null
between
like
in
– like结合 %(代表0到任意个字符) _(一个字符)
– 查询姓刘的学生
select ‘studentno’,‘studentname’ from ‘student’
where studentname like ‘刘%’
– 查询姓刘的且名字后面只有两个字的
select ‘studentno’,‘studentname’ from ‘student’
where studentname like ‘刘_’
– 查询姓刘的且名字后面只有两个字的
select ‘studentno’,‘studentname’ from ‘student’
where studentname like ‘刘__’
– 查询名字中间有嘉的同学
select ‘studentno’,‘studentname’ from ‘student’
where studentname like ‘%嘉%’
– in (具体的一个或者多个值)
select ‘studentno’,‘studentname’ from ‘student’
where studentname in (1001,1002,1003)
select ‘studentno’,‘studentname’ from ‘student’
where studentname in (‘北京’)
join
自连接
升序asc 降序desc
order by 表列名 asc
分页 limit
limit 起始页,页面大小 limit 0,10
子查询,嵌套查询
select …(
select …(
select …
)
)
事务:
ACID,是指数据库管理系统(DBMS)在写入或更新资料的过程中,为保证事务(transaction)是正确可靠的,所必须具备的四个特性:
原子性(atomicity,或称不可分割性)、一致性(consistency)、
隔离性(isolation,又称独立性)、持久性(durability)。
https://blog.youkuaiyun.com/dengjili/article/details/82468576
mysql是默认开启事务自动提交的
SET autocommit = 0
SET autocommit = 1
事务开启
start transaction – 标记一个事务的开启,从这个之后的sql都在同一个事务内
xxx
xxx
– 提交,持久化(成功!)
commit
– 回滚:回到原来的样子(失败!)
rollback
– 事务结束
savepoint 保存点名 --设置一个事务的保存点
release savepoint 保存点名 – 撤销一个保存点
索引
主键索引(primary key)
唯一索引(unique key)
常规索引(key/index)
全文索引(fulltext)
【MySQL优化】——看懂explain
https://blog.youkuaiyun.com/jiadajing267/article/details/81269067
索引的数据结构
https://blog.codinglabs.org/articles/theory-of-mysql-index.html
数据库用户管理
mysql数据库备份
1、物理拷贝
2、sqlyog可视化工具中手动导出
– 在想要导出的表或者库中,右键,选择备份或导出
3、mysqldump 命令行
三大范式
jdbc(重点)
package com.hu.lesson1;
import java.sql.*;
public class jdbcdemo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
/* 1.加载驱动 */
Class.forName("com.mysql.cj.jdbc.Driver");
//2.连接用户
String url;
url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String password = "123456";
//3.连接成功,数据库对象
Connection connection1 = DriverManager.getConnection(url, username, password);
//4.执行sql的对象
Statement statement1 = connection1.createStatement();
//5.执行aql的对象去执行sql,可能存在返回结果,查看返回结果
String sql = "SELECT * FROM users";
ResultSet resultSet1 = statement1.executeQuery(sql);
while(resultSet1.next()){
System.out.println("id=" + resultSet1.getObject("id"));
System.out.println("name=" + resultSet1.getObject("namess"));
System.out.println("pwd=" + resultSet1.getObject("pwd"));
System.out.println("email=" + resultSet1.getObject("email"));
System.out.println("birth=" + resultSet1.getObject("birthday"));
}
//6.释放连接
resultSet1.close();
statement1.close();
connection1.close();
}
}
代码解释:
statement对象
jdbc中的statement对象用于向数据库发送sql语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可。
statement对象中的executeUpdate方法,用于向数据库发送增删改的sql语句,executeUpdate执行完后,将会返回一个整数(即增删改语句导致了数据库几行数据发生了变化)。
statement.executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。
sql注入问题
preparestatement解决注入问题
使用idea连接数据库