MySQL replace into Ó÷¨
ÔÚÏò±íÖвåÈëÊý¾ÝµÄʱºò£¬¾³£Óöµ½ÕâÑùµÄÇé¿ö£º1. Ê×ÏÈÅжÏÊý¾ÝÊÇ·ñ´æÔÚ£» 2. Èç¹û²»´æÔÚ£¬Ôò²åÈ룻3.Èç¹û´æÔÚ£¬Ôò¸üС£
ÔÚ SQL Server ÖпÉÒÔÕâÑù´¦Àí£º
if not exists (select 1 from t where id = 1) insert into t(id, update_time) values(1, getdate()) else update t set update_time = getdate() where id = 1
ÄÇô MySQL ÖÐÈçºÎʵÏÖÕâÑùµÄÂß¼ÄØ£¿±ð׿±£¡MySQL ÖÐÓиü¼òµ¥µÄ·½·¨£º replace into
replace into t(id, update_time) values(1, now());
»ò
replace into t(id, update_time) select 1, now();
replace into ¸ú insert ¹¦ÄÜÀàËÆ£¬²»Í¬µãÔÚÓÚ£ºreplace into Ê×Ïȳ¢ÊÔ²åÈëÊý¾Ýµ½±íÖУ¬ 1. Èç¹û·¢ÏÖ±íÖÐÒѾÓдËÐÐÊý¾Ý£¨¸ù¾ÝÖ÷¼ü»òÕßΨһË÷ÒýÅжϣ©ÔòÏÈɾ³ý´ËÐÐÊý¾Ý£¬È»ºó²åÈëеÄÊý¾Ý¡£ 2. ·ñÔò£¬Ö±½Ó²åÈëÐÂÊý¾Ý¡£
ҪעÒâµÄÊÇ£º²åÈëÊý¾ÝµÄ±í±ØÐëÓÐÖ÷¼ü»òÕßÊÇΨһË÷Òý£¡·ñÔòµÄ»°£¬replace into »áÖ±½Ó²åÈëÊý¾Ý£¬Õ⽫µ¼Ö±íÖгöÏÖÖظ´µÄÊý¾Ý¡£
MySQL replace into ÓÐÈýÖÖÐÎʽ£º
1. replace into tbl_name(col_name, ...) values(...) 2. replace into tbl_name(col_name, ...) select ... 3. replace into tbl_name set col_name=value, ...
Ç°Á½ÖÖÐÎʽÓõĶàЩ¡£ÆäÖÐ “into” ¹Ø¼ü×Ö¿ÉÒÔÊ¡ÂÔ£¬²»¹ý×îºÃ¼ÓÉÏ “into”£¬ÕâÑùÒâ˼¸ü¼ÓÖ±¹Û¡£ÁíÍ⣬¶ÔÓÚÄÇЩûÓиøÓèÖµµÄÁУ¬MySQL ½«×Ô¶¯ÎªÕâЩÁи³ÉÏĬÈÏÖµ¡£
Ïà¹ØÎĵµ£º
°²×°JDBCÇý¶¯£º
½âѹËõmysql-connector-java-3.1.10.zip
½«ÒªÊ¹ÓõÄÊÇmysql-connector-java-3.1.10-bin-g.jarºÍmysql-connector-java-3.1.10-bin.jar
ÅäÖÃ
ÔÚC:\Program Files\JavaĿ¼Ï½¨Á¢mysqlforjdbc×ÓĿ¼£¬½øÈë¸ÃĿ¼½«mysql-connector-java-3.1.10-bin.jarµ½¸ÃĿ¼ÏÂ
½øÈëC:\Program Files\Java\jdk1.5.0_04\l ......
µÚÒ»ÖÖ:²éSlow queryµÄSQLÓï·¨:
log_slow_queries = /var/log/mysql/mysql-slow.log
long_query_time = 2 (³¬¹ý2ÃëµÄSQLÓï·¨¼Ç¼ÆðÀ´,Éè¶ÌÒ»µãÀ´¼Ç¼³ý´íÒ²ÊÇÒ»ÖÖ·½·¨.)
µÚ¶þÖÖ:ÉèMySQL ReplicationÓÃbinlog:
log_bin = /var/log/mysql/mysql-bin.log (´ËµµÒªÓÃmysqlbinlog½âÀ´¿´£¬
mysqlbinl ......
¹«Ë¾µÄ·þÎñÆ÷ÊÇCentOS 5.4(Final)°æµÄ£¬½ñÌìÔÚÉÏÃæ°²×°ÁËÒ»»Ømysql5.5£¬¼Ç¼ÈçÏ£º
1¡¢´Óhttp://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.2-m2.tar.gz ÏÂÔØÔ´Âë°²×°°üµ½/usr/local/src
2¡¢tar zxvf mysql-5.5.2-m2.tar.gz ½âѹµ½/usr/local/src/mysql-5.5.2-m2ÏÂ
cd mysql-5.5.2-m2/
&n ......
Ò»Ö±ÒÔΪmysqlËæ»ú²éѯ¼¸ÌõÊý¾Ý£¬¾ÍÓÃ
SELECT
*
from
`
table
`
ORDER
BY
RAND
() LIMIT
5
¾Í¿ÉÒÔÁË¡£
µ«ÊÇÕæÕý²âÊÔһϲŷ¢ÏÖÕâÑùЧÂʷdz£µÍ¡£Ò»¸ö15ÍòÓàÌõµÄ¿â£¬²éѯ5ÌõÊý¾Ý£¬¾ÓȻҪ8ÃëÒÔÉÏ
²é¿´¹Ù·½ÊֲᣬҲ˵rand()·ÅÔÚORDER BY ×Ó¾äÖлᱻִÐж ......
/*ÑÝʾһ¸öJDBC³ÌÐò£¬´ÓMysqlµÄLMDÊý¾Ý¿âÖжÁ³ö±íadminµÄÒ»¸öÐÅÏ¢*/
import java.sql.*;
public class TestMysqlConnection {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = ......