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£¬ËµÃ÷Ô
Ïà¹ØÎĵµ£º
cmdÏÂÇл»µ½ mysql °²×°Ä¿Â¼
Àý
d:/mysql/bin (Èç¹ûÓÃWAMP°²×°ÔòÊÇD:\wamp\bin\mysql\mysql5.1.30\bin)
Ç°Ì᣺mysqlÓû§rootÃÜÂëΪ¿Õ.
ÊäÈë mysql -u root mysql
mysql> ״̬ÏÂÊäÈë update user set password=password('ÐÂÃÜÂë') where user='root';
»ØÏÔ
Query OK, 0 rows ......
ÏÈÏÂÔØÒ»¸ömysqlµÄÇý¶¯Æ÷£º£¨ÎÒϵÄÊÇmysql-connector-java-3.1.11-bin.jar£©
½«Ëü·Åµ½java\libĿ¼ÏÂ
ÔÚ»·¾³±äÁ¿µÄCLASSPATHÀïÌí¼Ó¸ÃÀࣨ%JAV_HOMG%\lib\mysql-connector-java-3.1.11-bin.jar)
³ö²âÊԸöδúÂë¼´¿É£º
import java.net.URL;
import java.sql.*;
public class sqlTest {
public static void mai ......
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 ......
×÷/ÒëÕߣºÒ¶½ðÈÙ£¨Email: £©£¬À´Ô´£ºhttp://imysql.cn£¬×ªÔØÇë×¢Ã÷×÷/ÒëÕߺͳö´¦£¬²¢ÇÒ²»ÄÜÓÃÓÚÉÌÒµÓÃ;£¬Î¥Õ߱ؾ¿¡£
Ê×ÏÈ,ÕâƬÎÄÕ´¿´âÊÇÎҵĸöÈ˾Ñé̸֮,ÊÊÓÃÓÚÎÒ³£¼ûµÄ»·¾³¼°ÏîÄ¿ÖÐ.
¸öÈ˽¨Òé,Êý¾Ý¿â×Ö·û¼¯¾¡Á¿Ê¹ÓÃutf8(HTMLÒ³Ãæ¶ÔÓ¦µÄÊÇutf-8),ÒÔʹÄãµÄÊý¾ÝÄܺÜ˳ÀûµÄʵÏÖǨÒÆ,ÒòΪutf8×Ö·û¼¯ÊÇÄ¿Ç°×îÊʺÏÓÚÊ ......
1£ºMysql ÃüÁî²»¿ÉÒÔÖ´ÐÐ
ÔÒò £ººÇºÇ ±ÈÈç show databases µÄÃüÁî £¬Ö´ÐÐÍêÖ®ºó ɶҲûÓÐ £¬×Ðϸ ÕÒÁËÔÒò £¬Oh my god ÉÙÁË ·ÖºÅ¡£
Ӧд³É£ºshow databases;
2£ºÃüÁîÐÐÖÐÎÄÂÒÂëÎÊÌâ
ÏÖÏó£ºmysqlµÄĬÈϱàÂëÉèÖÃΪutf-8£¬Ò³ÃæÒÔutf-8µÄ±àÂ뷽ʽдÈëµ½Êý¾Ý¿â¡£Óù¤¾ß²é¿´Ò»ÇÐÕý³££¬µ«´ÓmysqlµÄÃüÁîÐжÁÊý¾Ý³öÀ´× ......