{"id":100408,"date":"2024-07-18T11:16:10","date_gmt":"2024-07-18T03:16:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=100408"},"modified":"2024-07-18T11:16:10","modified_gmt":"2024-07-18T03:16:10","slug":"mariadb-insert-data","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/mariadb-insert-data.html","title":{"rendered":"Mariadb\u5b66\u4e60\u603b\u7ed3\uff08\u56db\uff09\uff1a\u6570\u636e\u7684\u63d2\u5165\u5220\u9664\u4e0e\u4fee\u6539"},"content":{"rendered":"
INSERT\u8bed\u53e5\u683c\u5f0f\uff1a<\/p>\n
\r\nINSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]\r\n [INTO] tbl_name [PARTITION (partition_list)] [(col,...)]\r\n {VALUES | VALUE} ({expr | DEFAULT},...),(...),...\r\n [ ON DUPLICATE KEY UPDATE\r\n col=expr\r\n [, col=expr] ... ]<\/pre>\n\u8868\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n
\r\nMariaDB [mydb]> DESC user;\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n| id | int(11) | NO | PRI | NULL | auto_increment |\r\n| username | varchar(10) | NO | | NULL | |\r\n| password | varchar(10) | NO | | NULL | |\r\n| regtime | timestamp | NO | | CURRENT_TIMESTAMP | |\r\n| logtime | timestamp | NO | | 0000-00-00 00:00:00 | |\r\n| logip | varchar(20) | YES | | NULL | |\r\n+----------+-------------+------+-----+---------------------+----------------+\r\n6 rows in set (0.01 sec)<\/pre>\n\u63d2\u5165\u5355\u6761\u6570\u636e<\/strong><\/span><\/div>\nMariaDB [mydb]> INSERT INTO user VALUES(1,'test','test',NOW(),NOW(),'127.0.0.1');<\/pre>\n\u56e0\u4e3a\u5728\u5f88\u591a\u65f6\u5019\u6709\u4e9b\u503c\u662f\u9ed8\u8ba4\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u4e3a\u54ea\u4e9b\u5217\u63d2\u5165\u6570\u636e\uff0c\u800c\u5176\u4ed6\u5217\u5219\u4f7f\u7528\u9ed8\u8ba4\u503c\uff0c\u5982\u4e0b\uff1a<\/p>\n
MariaDB [mydb]> INSERT INTO user(username,password) VALUES('test2','test2');<\/pre>\n\u540c\u6837\u7684\uff0c\u63d2\u5165\u7279\u5b9a\u5217\u6570\u636e\u8fd8\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n
MariaDB [mydb]> INSERT INTO user SET username='test3',password='test3';<\/pre>\n\u8fd9\u6837\u5c31\u4ec5\u4ec5\u63d2\u5165\u4e86\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5176\u4ed6\u503c\u5219\u4f7f\u7528\u9ed8\u8ba4\u3002<\/p>\n
\r\nMariaDB [mydb]> SELECT * FROM user;\r\n+----+----------+----------+---------------------+---------------------+-----------+\r\n| id | username | password | regtime | logtime | logip |\r\n+----+----------+----------+---------------------+---------------------+-----------+\r\n| 1 | test | test | 2018-02-24 15:43:41 | 2018-02-24 15:43:41 | 127.0.0.1 |\r\n| 2 | test2 | test2 | 2018-02-24 15:45:16 | 0000-00-00 00:00:00 | NULL |\r\n| 3 | test3 | test3 | 2018-02-24 15:46:56 | 0000-00-00 00:00:00 | NULL |\r\n+----+----------+----------+---------------------+---------------------+-----------+\r\n3 rows in set (0.00 sec)\r\n<\/pre>\n\u63d2\u5165\u591a\u884c\u6570\u636e<\/strong><\/span><\/div>\n\u5f88\u591a\u65f6\u5019\u6211\u4eec\u4f1a\u6709\u4f7f\u7528\u4e00\u6761INSERT\u8bed\u53e5\u5411\u6570\u636e\u8868\u63d2\u5165\u591a\u6761\u8bb0\u5f55\u7684\u9700\u6c42\uff0c\u5c31\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n
\r\nMariaDB [mydb]> INSERT INTO user(username,password) VALUES('test4','test4'),('test5',\r\n -> 'test5');\r\n<\/pre>\n\u83ab\u540d\u5176\u5999\u7684\u4f18\u5148\u7ea7\uff1f<\/strong><\/span><\/div>\n<\/p>\n
\u5f53\u4f7f\u7528\u7684\u5b58\u50a8\u5f15\u64ce(MyISAM, MEMORY, MERGE)\u4f7f\u7528\u8868\u7ea7\u9501\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528LOW_PRIORITY| HIGH_PRIORITY\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\uff1a
\n \u5f53\u4f7f\u7528LOW_PRIORITY\u5173\u952e\u5b57\u65f6\uff0c\u5f53\u6ca1\u6709\u5ba2\u6237\u7aef\u518d\u8bfb\u53d6\u8be5\u8868\u65f6\u624d\u5199\u5165\u6570\u636e\u3002
\n \u5f53\u4f7f\u7528HIGH_PRIORITY\u65f6\uff0cINSERT\u8bed\u53e5\u5177\u6709\u540cSELECT\u8bed\u53e5\u4e00\u6837\u7684\u4f18\u5148\u7ea7\u3002\uff08\u9ed8\u8ba4\u7b56\u7565\uff09<\/p>\n\u6240\u4ee5\uff0c\u5f53INSERT\u8bed\u53e5\u6267\u884c\u524d\u6709SELECT\u8bed\u53e5\u6267\u884c\u65f6\uff0cINSERT\u963b\u585e\u5e76\u7b49\u5f85SELECT\u8bfb\u53d6\u5b8c\u6bd5\uff0c\u4f46\u6b64\u65f6\uff0c\u5982\u679c\u6709SELECT\u518d\u6b21\u8fdb\u5165\u8c03\u5ea6\uff0c\u5219SELECT\u88ab\u963b\u585e\uff08\u6309\u7406\u8bfb\u9501\u53ef\u4ee5\u76f4\u63a5\u8bfb\uff09\u4f46\u6b64\u65f6\u56e0\u4e3aINSERT\u8bed\u53e5\u8ddfSELECT\u8bed\u53e5\u6709\u540c\u6837\u7684\u4f18\u5148\u7ea7\uff0c\u6240\u4ee5SELECT\u8981\u7b49INSERT\u7ed3\u675f\u624d\u80fd\u6267\u884c\uff0c\u6240\u4ee5INSERT\u53ef\u4ee5\u52a0LOW_PRIORITY\u6765\u4f18\u5316\u8bfb\u53d6\u901f\u5ea6\u3002<\/p>\n
\u8fd9\u91cc\u6162\u6162\u770b\u5427\uff0c\u5bf9\u9501\u8fd9\u4e2a\u4e1c\u897f\u61c2\u7684\u4e0d\u662f\u5f88\u591a\u3002<\/p>\n
\u4fee\u6539\u6570\u636e<\/strong><\/div>\nUPDATE\u8bed\u53e5\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n
UPDATE [LOW_PRIORITY] [IGNORE] table_reference \r\n [PARTITION (partition_list)]\r\n SET col1={expr1|DEFAULT} [,col2={expr2|DEFAULT}] ...\r\n [WHERE where_condition]\r\n [ORDER BY ...]\r\n [LIMIT row_count]<\/pre>\n\u66f4\u65b0\u6240\u6709<\/strong><\/span><\/div>\n\u5f53\u4e0d\u4f7f\u7528WHERE\u5b50\u53e5\u7ea6\u675f\u9009\u62e9\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u5219\u66f4\u65b0\u6240\u6709\u6570\u636e\uff0c\u4f8b\u5982\uff0c\u4fee\u6539user\u8868\u6240\u6709\u8bb0\u5f55\u7684\u767b\u9646\u65f6\u95f4\u4e3a\u73b0\u5728\uff1a<\/p>\n
\r\nMariaDB [mydb]> UPDATE user SET logtime=NOW();\r\nQuery OK, 5 rows affected (0.01 sec)\r\nRows matched: 5 Changed: 5 Warnings: 0\r\n<\/pre>\n\u66f4\u65b0\u6700\u65e9\u6ce8\u518c\u7684\u4e09\u4e2a\u4eba\u7684\u767b\u5f55IP\u4e3a127.0.0.1<\/strong><\/span><\/div>\n\r\nMariaDB [mydb]> UPDATE user SET logip='127.0.0.1' ORDER BY regtime LIMIT 3;<\/pre>\nORDER BY\u8bed\u53e5\u53ef\u4ee5\u7528\u4e8eSELECT UPDATE DELETE\u7b49\uff0c\u662f\u6307\u793a\u8be5\u8868\u8f93\u51fa\u3001\u5220\u9664\u3001\u66f4\u65b0\u65f6\u9075\u5faa\u8be5\u8868\u54ea\u4e2a\u5b57\u6bb5\u6392\u5217\u3002
\n\u4f8b\u5982\u4ee5\u4e0a\uff0cORDER BY regtime\u5c31\u662f\u6839\u636e\u6ce8\u518c\u65f6\u95f4\u6765\u6b63\u5e8f\u6392\u5217\u66f4\u65b0\uff0c\u5e76\u4e14\u914d\u5408LIMIT\u8bed\u53e5\u53ea\u66f4\u65b0\u4e86\u524d\u4e09\u884c\u3002<\/p>\n\u518d\u4f7f\u7528DESC\u6765\u6307\u5b9a\u5012\u53d9\u6392\u5217\uff0c\u4f8b\u5982\uff1aORDER BY regtime DESC<\/p>\n
LIMIT\u8bed\u53e5\uff1a\u7528\u4e8e\u9650\u5236\u67e5\u8be2\u7ed3\u679c\u7684\u6761\u6570\u3002
\n\u7528\u6cd5\uff1a<\/p>\nLIMIT[\u4f4d\u7f6e\u504f\u79fb\u91cf,]\u884c\u6570<\/pre>\n\u7b2c\u4e00\u884c\u4ece0\u5f00\u59cb\uff0c\u6240\u4ee5\u4ee5\u4e0b\uff1a<\/p>\n
SELECT * FROM user LIMIT 2,2; \/\/\u4ece\u7b2c3\u884c\u5f00\u59cb\uff0c\u53d6\u4e24\u884c\uff0c\u5373\u53d6\u7b2c3\u30014\u6761\u8bb0\u5f55\u3002<\/pre>\n\u4f7f\u7528WHERE\u8bed\u53e5\u9009\u4e2d\u7279\u5b9a\u884c\u66f4\u65b0<\/strong><\/span><\/div>\nMariaDB [mydb]> UPDATE user SET logip='192.168.1.2' WHERE username='test2';<\/pre>\n\u56e0\u4e3aWHERE\u5b50\u53e5\u4e5f\u6709\u5f88\u591a\u4e1c\u897f\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8fc7\u591a\u8d34\u5185\u5bb9\u4e86\u3002<\/p>\n
LOW_PRIORITY\uff1a\u8fd9\u4e2a\u8ddfINSERT\u7684LOW_PRIORITY\u662f\u4e00\u6837\u7684\u3002<\/p>\n
REPLACE\u8bed\u53e5
\n<\/strong><\/span><\/div>\nMariaDB [mydb]> REPLACE INTO user VALUES(1,'test111','test111',NOW(),NOW(),'192.168.1.1');<\/pre>\n\u4ee5\u4e0a\u8fd9\u6761\u8bed\u53e5\u662fMariaDB\u7684\u6269\u5c55SQL\uff0c\u76f8\u5f53\u4e8e\u5220\u9664\u91cd\u590d\uff08\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15\uff09\u7684\u8bb0\u5f55\u518d\u6dfb\u52a0\u65b0\u7684\u8bb0\u5f55\u3002<\/p>\n
\u597d\u50cf\u6709\u70b9\u9e21\u808b\uff1f....<\/p>\n
\u5220\u9664\u6570\u636e<\/strong><\/div>\nDELETE\u8bed\u8bed\u6cd5\uff1a<\/p>\n
\r\nDELETE [LOW_PRIORITY] [QUICK] [IGNORE] \r\n FROM tbl_name [PARTITION (partition_list)]\r\n [WHERE where_condition]\r\n [ORDER BY ...]\r\n [LIMIT row_count]\r\n [RETURNING select_expr \r\n [, select_expr ...]]<\/pre>\n\u5220\u9664\u6240\u6709\u6570\u636e<\/strong><\/span><\/div>\n\u4e0d\u4f7f\u7528WHERE\u8bed\u53e5\u6765\u7ea6\u675f\u6761\u4ef6\u65f6\uff0c\u5219\u5220\u9664\u6240\u6709\u6570\u636e\uff0c\u5982\u4e0b\uff1a<\/p>\n
MariaDB [mydb]> DELETE FROM user;<\/pre>\n\u4f7f\u7528WHERE\u8bed\u53e5\u7ea6\u675f\u9009\u4e2d\u884c<\/strong><\/span><\/div>\nMariaDB [mydb]> DELETE FROM user WHERE username='test1';<\/pre>\n\u90a3\u4e48\uff1f\u8fd9\u7bc7\u6587\u7ae0\u5c31\u7b80\u5355\u5199\u5230\u8fd9\u5427~~<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/100ye.wang\/linux\/mariadb-study-data-insert-delete.html<\/a><\/p>\n