MySQL²åÈë¶àÌõ¼Ç¼ºÍREPLACEÓï¾ä
1.ʹÓÃINSERT²åÈë¶àÌõ¼Ç¼
¿´µ½Õâ¸ö±êÌâÒ²Ðí´ó¼Ò»áÎÊ£¬ÕâÓÐʲôºÃ˵µÄ£¬µ÷Óöà´ÎINSERTÓï¾ä²»¾Í¿ÉÒÔ²åÈë¶àÌõ¼Ç¼ÁËÂð!µ«Ê¹ÓÃÕâÖÖ·½·¨ÒªÔö¼Ó·þÎñÆ÷µÄ¸ººÉ£¬ÒòΪ£¬
Ö´ÐÐÿһ´ÎSQL·þÎñÆ÷¶¼ÒªÍ¬Ñù¶ÔSQL½øÐзÖÎö¡¢ÓÅ»¯µÈ²Ù×÷¡£ÐÒºÃMySQLÌṩÁËÁíÒ»ÖÖ½â¾ö·½°¸£¬¾ÍÊÇʹÓÃÒ»ÌõINSERTÓï¾äÀ´²åÈë¶àÌõ¼Ç¼¡£Õâ²¢
²»ÊDZê×¼µÄSQLÓï·¨£¬Òò´ËÖ»ÄÜÔÚMySQLÖÐʹÓá£
INSERT INTO users(name, age) VALUES('Ò¦Ã÷', 25), ('±È¶û.¸Ç´Ä', 50),
('»ðÐÇÈË', 600);
ÉÏÃæµÄINSERT
Óï¾äÏòusers±íÖÐÁ¬Ðø²åÈëÁË3Ìõ¼Ç¼¡£ÖµµÃ×¢ÒâµÄÊÇ£¬ÉÏÃæµÄINSERTÓï¾äÖеÄVALUESºó±ØÐëÿһÌõ¼Ç¼µÄÖµ·Åµ½Ò»¶Ô(…)ÖУ¬ÖмäʹÓÃ","
·Ö¸î¡£¼ÙÉèÓÐÒ»¸ö±ítable1
CREATE TABLE table1(n INT);
Èç¹ûÒªÏòtable1ÖвåÈë5Ìõ¼Ç¼£¬ÏÂÃæд·¨ÊÇ´íÎóµÄ£º
INSERT INTO table1 (i) VALUES(1,2,3,4,5);
MySQL½«»áÅ׳öÏÂÃæµÄ´íÎó
ERROR 1136: Column count doesn't match value count at row 1
¶øÕýÈ·µÄд·¨Ó¦¸ÃÊÇÕâÑù£º
INSERT INTO table1(i) VALUES(1),(2),(3),(4),(5);
µ±È»£¬ÕâÖÖд·¨Ò²¿ÉÒÔÊ¡ÂÔÁÐÃû£¬ÕâÑùÿһ¶ÔÀ¨ºÅÀïµÄÖµµÄÊýÄ¿±ØÐëÒ»Ö£¬¶øÇÒÕâ¸öÊýÄ¿±ØÐëºÍÁÐÊýÒ»Ö¡£È磺
INSERT INTO table1 VALUES(1),(2),(3),(4),(5);
2. REPLACEÓï¾ä
ÎÒÃÇÔÚʹÓÃÊý¾Ý¿âʱ¿ÉÄܻᾳ£Óöµ½ÕâÖÖÇé¿ö¡£Èç¹ûÒ»¸ö±íÔÚÒ»¸ö×Ö¶ÎÉϽ¨Á¢ÁËΨһË÷Òý£¬µ±ÎÒÃÇÔÙÏòÕâ¸ö±íÖÐʹÓÃÒѾ´æÔڵļüÖµ²åÈëÒ»Ìõ¼Ç¼£¬ÄÇ
½«»áÅ׳öÒ»¸öÖ÷¼ü³åÍ»µÄ´íÎó¡£µ±È»£¬ÎÒÃÇ¿ÉÄÜÏëÓÃмǼµÄÖµÀ´¸²¸ÇÔÀ´µÄ¼Ç¼ֵ¡£Èç¹ûʹÓô«Í³µÄ×ö·¨£¬±ØÐëÏÈʹÓÃ
DELETEÓï¾äɾ³ýÔÏȵļǼ£¬È»ºóÔÙʹÓÃINSERT²åÈëеļǼ¡£¶øÔÚMySQLÖÐΪÎÒÃÇÌṩÁËÒ»ÖÖеĽâ¾ö·½°¸£¬Õâ¾ÍÊÇREPLACEÓï¾ä¡£Ê¹ÓÃ
REPLACE²åÈëÒ»Ìõ¼Ç¼ʱ£¬Èç¹û²»Öظ´£¬REPLACE¾ÍºÍINSERTµÄ¹¦ÄÜÒ»Ñù£¬Èç¹ûÓÐÖظ´¼Ç¼£¬REPLACE¾ÍʹÓÃмǼµÄÖµÀ´Ìæ»»ÔÀ´µÄ¼Ç
¼ֵ¡£
ʹÓÃREPLACEµÄ×î´óºÃ´¦¾ÍÊÇ¿ÉÒÔ½«DELETEºÍINSERTºÏ¶þΪһ£¬ÐγÉÒ»¸öÔ×Ó²Ù×÷¡£ÕâÑù¾Í¿ÉÒÔ²»±Ø¿¼ÂÇÔÚͬʱʹÓÃDELETE ºÍ
INSERTʱÌí¼ÓÊÂÎñµÈ¸´ÔÓ²Ù×÷ÁË¡£
ÔÚʹÓÃREPLACEʱ£¬±íÖбØÐëÓÐΨһË÷Òý£¬¶øÇÒÕâ¸öË÷ÒýËùÔÚµÄ×ֶβ»ÄÜÔÊÐí¿ÕÖµ£¬·ñÔòREPLACE¾ÍºÍINSERTÍêÈ«Ò»ÑùµÄ¡£
ÔÚÖ´ÐÐREPLACEºó£¬ÏµÍ³·µ»ØÁËËùÓ°ÏìµÄÐÐÊý£¬Èç¹û·µ»Ø1£¬ËµÃ÷Ô
Ïà¹ØÎĵµ£º
µÚÒ»¸ö MySQL Community Server£¬Õâ¸ö²»ÒªÇ®£¡
µÚ¶þ¸ö MySQL Enterprise Õâ¸öÒªÌÍÇ®£¬²»¹ý¿ÉÒÔ´òµç»°×ÉѯÎÊÌ⣬Ҳ¾ÍÊǵ绰¼¼ÊõÖ§³Ö¡£
µÚÈý¸ö MySQL Cluster£¬Õâ¸öµ¥¶ÀÊÇû·¨Óõģ¬ÒªÔÚ1»ò2µÄ»ù´¡ÉÏÓᣵ±È»ÓÃÀ´Æ½ºâ¶ą̀Êý¾Ý¿âµÄ¡£
µÚËĸö MySQL Workbench£¬ÕâÊǸöºÃ¶«Î÷£¬ÓÃÀ´Éè¼ÆÊý¾Ý¿âµÄ¡£erwinÖªµÀÂð£¿Ëû¾ÍÊÇÕâ¸ö×÷Óà ......
1.DATE_FORMAT('2010-05-15 15:47:36','%H:%i:%s') -> ½á¹û£º15:47:36
½«ÈÕÆÚ¸ñʽ¸ù¾ÝÌõ¼þ²»Í¬×ª»»³ÉËùÐèÒªµÄÈÕÆÚ¡¢Ê±¼ä¸ñʽ
2.timediff('23:40:00', ' 18:30:00') -> ½á¹û£º05:10:00
......
£¨1£©INFORMATION_SCHEMA
select (sum(data_length) + sum(index_length))/(1024*1024) from INFORMATION_SCHEMA.`TABLES` where table_schema = 'your_table_schema' and table_name like 'your_table_name';
£¨2£©show table status like '';
try {
Class.forName("com.mysql.jdbc.Driver");
......
1.½¨±í¹ý³ÌÈçÏÂ
create database test default character set utf8 collate utf8_general_ci
use test;
create table devicedata
(
ID int auto_increment PRIMARY key ,
TimeStamp datetime,
Device_Name varchar(100),
Tag_Name varchar(100),
Value varchar(50)
)
2 ......