rdbms

---------------

table:field, record,

sql

---------------

crud

insert into tablename(id,...) values(1,'sss',...) ;

delete from tablename where id = 1 or id = 3 or id = 5 ;

delete from tablename where id in(1,3,5); //in()范围运算  == or

delete from tablename where id between a and b //id >= a and id <= b 


update tablename set name = 'xx',age = xxx where id = xxx ;


select 1 + 1 ; //表达式

select age + 1 from stus ; //

select (age + 1) as newage ,name ,age from stus ; //as 指定字段的别名,as可以省略


select concat('hello ',name) from stus ; //concat()连接函数


source //执行外部sql脚本

source d:/mysql.sql



//聚集函数

count(*) //查询记录数量

sum() //求和

max() //最大值

min() //最小值

avg() //平均值



//注释

/*  */ 块注释

-- 行注释,--后面必须跟一个空格。

//sql注入

select * from stus where name = '1' or 1= 1 ;-- ' and password = 'xxx' 


truncate stus ; //截断表


性能评测(15000条)

--------------

preparedStatement : 782毫秒7389

Statement  : 


//查询数据时间

select now(); //


Statement < PreparedStatement < CallableStatement{存储过程}


PreparedStatement

--------------------

1.放置sql注入

2.实现批量处理

ppst.addBatch(); //7268

ppst.executeBatch();


DDL 

----------------------

alter table stus add 

BLOB //binary large object,二进制大对象

Text //mysql的大文本类型,character large object,字段类型。


mysql表级锁定

--------------

1. LOCK TABLE t WRITE  | Read;

表级读锁和写锁。

读锁 : 本线程只能读,其他线程只能读,都无法写入。

写锁 : 本线程可以读写,其他线程不能读写。

mysql存放大对象

----------------

1.设计表时,使用longblob字段类型

alter table stus add pic longblob ;

alter table stus modify pic longblob ;

2.java使用PreparedStatement;

ppst.setBlob(...); //该方法不可用,没有实现

ppst.setBinaryStream(int index,InputStream is); //该方法不可用,没有实现。

ppst.setBinaryStream(int index,InputStream is,int length);

3.mysql终端查询

select id from stus where pic is not null ;


存储过程

------------------

1.mysql client -- 修改行结束符 ; 

delimiter // 

2.create procedure procAdd(in|out|inout a int,in b int , out c int)

begin

-- set c = a + b ;

select  a + b into c ;

end

//

-- 大量插入

delimiter // -- 设置行结束符

create procedure procBatchInsert(in n int)

begin

DECLARE  i int ;

set i = 0 ; -- 赋值操作

start transaction ; -- 启动事务

while i < n do -- 循环操作

insert into stus(id,name) values(i,concat('tom',i));

set i = i + 1 ;

end while ;

commit ;

end 

//


3.java编程

Connection conn = DriverManager.getConnection(url, username, password);

//存储过程调用方式"{call procName(?,?,?,...)}"

CallableStatement cst = conn.prepareCall("{call procAdd(?,?,?)}");

cst.setInt(1, 1); //绑定输入参数,

cst.setInt(2, 2);

cst.registerOutParameter(3, Types.INTEGER); //注册输出类型

cst.execute(); //执行存储过程

int r = cst.getInt(3); //获取返回结果


事务并发执行

--------------

1.脏读,读脏

读未提交。

2.不可重复读

读不回去.有其他事务做了update。

3.幻读.

读多了。


事务的隔离级别

-----------------

1--读未提交,可能导致脏读、不可重复读、幻读。

2--读已提交,避免脏读,不可重复读、幻读还能发生。

4--可以重复读,避免脏读和不可重复读,幻读还能发生。

8--串行化。


select @@session.tx_isolation; -- 会话的隔离级别

select @@global.tx_isolation; -- 全局级别


set transaction 



属性外部化

----------------

Properties prop = new Properties();

InputStream is = DBUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");

prop.load(is);