INSERT INTO table_name ( field1, field2,...fieldN )VALUES( value1, value2,...valueN );如果数据是字符型,必须使用单引号或者双引号,如:"value"。
root@host# mysql -u root -p password;Enter password:*******mysql> use RUNOOB;Database changedmysql> INSERT INTO runoob_tbl->(runoob_title, runoob_author, submission_date) ->VALUES ->("Learn PHP", "John Poul", NOW());Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO runoob_tbl ->(runoob_title, runoob_author, submission_date) ->VALUES ->("Learn MySQL", "Abdul S", NOW());Query OK, 1 row affected (0.01 sec)mysql> INSERT INTO runoob_tbl ->(runoob_title, runoob_author, submission_date) ->VALUES ->("JAVA Tutorial", "Sanjay", "2007-05-06");Query OK, 1 row affected (0.01 sec)mysql>
bool mysql_query( sql, connection );参数:
<html><head><title>向 MySQL 数据库添加数据</title></head><body><?phpif(isset($_POST["add"])){$dbhost = "localhost:3036";$dbuser = "root";$dbpass = "rootpassword";$conn = mysql_connect($dbhost, $dbuser, $dbpass);if(! $conn ){ die("Could not connect: " . mysql_error());}if(! get_magic_quotes_gpc() ){$runoob_title = addslashes ($_POST["runoob_title"]);$runoob_author = addslashes ($_POST["runoob_author"]);}else{$runoob_title = $_POST["runoob_title"];$runoob_author = $_POST["runoob_author"];}$submission_date = $_POST["submission_date"];$sql = "INSERT INTO runoob_tbl "."(runoob_title,runoob_author, submission_date) "."VALUES "."("$runoob_title","$runoob_author","$submission_date")";mysql_select_db("RUNOOB");$retval = mysql_query( $sql, $conn );if(! $retval ){ die("Could not enter data: " . mysql_error());}echo "Entered data successfully ";mysql_close($conn);}else{?><form method="post" action="<?php $_PHP_SELF ?>"><table width="600" border="0" cellspacing="1" cellpadding="2"><tr><td width="250">Tutorial Title</td><td><input name="runoob_title" type="text" id="runoob_title"></td></tr><tr><td width="250">Tutorial Author</td><td><input name="runoob_author" type="text" id="runoob_author"></td></tr><tr><td width="250">Submission Date [ yyyy-mm-dd ]</td><td><input name="submission_date" type="text" id="submission_date"></td></tr><tr><td width="250"> </td><td> </td></tr><tr><td width="250"> </td><td><input name="add" type="submit" id="add" value="Add Tutorial"></td></tr></table></form><?php}?></body></html>在我们接收用户提交的数据时,为了数据的安全性我们需要使用 get_magic_quotes_gpc() 函数来判断特殊字符的转义是否已经开启。如果这个选项为off(未开启),返回0,那么我们就必须调用addslashes 这个函数来为字符串增加转义。
insert into `table`(`field1`,`field2`) values("value1","value2");提高insert 性能的方法
INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ("userid_0", "content_0", 0); INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ("userid_1", "content_1", 1);可以写成
START TRANSACTION; INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ("userid_0", "content_0", 0); INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ("userid_1", "content_1", 1); ... COMMIT;注意
关于事务的配置项说明:
1.innodb_buffer_pool_size
如 果用Innodb,那么这是一个重要变量。相对于MyISAM来说,Innodb对于buffer size更敏感。MySIAM可能对于大数据量使用默认的key_buffer_size也还好,但Innodb在大数据量时用默认值就感觉在爬了。 Innodb的缓冲池会缓存数据和索引,所以不需要给系统的缓存留空间,如果只用Innodb,可以把这个值设为内存的70%-80%。和 key_buffer相同,如果数据量比较小也不怎么增加,那么不要把这个值设太高也可以提高内存的使用率。
2.innodb_additional_pool_size
这个的效果不是很明显,至少是当操作系统能合理分配内存时。但你可能仍需要设成20M或更多一点以看Innodb会分配多少内存做其他用途。
3.innodb_log_file_size
对于写很多尤其是大数据量时非常重要。要注意,大的文件提供更高的性能,但数据库恢复时会用更多的时间。我一般用64M-512M,具体取决于服务器的空间。
4.innodb_log_buffer_size
默认值对于多数中等写操作和事务短的运用都是可以的。如 果经常做更新或者使用了很多blob数据,应该增大这个值。但太大了也是浪费内存,因为1秒钟总会 flush(这个词的中文怎么说呢?)一次,所以不需要设到超过1秒的需求。8M-16M一般应该够了。小的运用可以设更小一点。
5.innodb_flush_log_at_trx_commit
抱怨Innodb比MyISAM慢 100倍?那么你大概是忘了调整这个值。默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入(flush)硬盘,这是很费时的。特别是使用电 池供电缓存(Battery backed up cache)时。设成2对于很多运用,特别是从MyISAM表转过来的是可以的,它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬 盘,所以你一般不会丢失超过1-2秒的更新。设成0会更快一点,但安全方面比较差,即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统 挂了时才可能丢数据。