Mariadb 简明教程
MariaDB - Insert Query
在本章中,我们将学习如何在表中插入数据。
In this chapter, we will learn how to insert data in a table.
在表中插入数据需要使用 INSERT 命令。该命令的一般语法是 INSERT 后跟表名、字段和值。
Inserting data into a table requires the INSERT command. The general syntax of the command is INSERT followed by the table name, fields, and values.
查看其给出的以下一般语法 −
Review its general syntax given below −
INSERT INTO tablename (field,field2,...) VALUES (value, value2,...);
该语句需要使用单引号或双引号来表示字符串值。该语句的其他选项包括 “INSERT…SET” 语句、“INSERT…SELECT” 语句以及其他几个选项。
The statement requires the use of single or double quotes for string values. Other options for the statement include “INSERT…SET” statements, “INSERT…SELECT” statements, and several other options.
Note − 仅适用于 INSERT 语句并且在其他地方使用时返回 NULL 的 VALUES() 函数出现在该语句中。
Note − The VALUES() function that appears within the statement, only applies to INSERT statements and returns NULL if used elsewhere.
有两种执行该操作的方法:使用命令行或使用 PHP 脚本。
Two options exist for performing the operation: use the command line or use a PHP script.
The Command Prompt
在提示符下,有许多执行选择操作的方法。下面给出了一个标准语句 −
At the prompt, there are many ways to perform a select operation. A standard statement is given below −
belowmysql>
INSERT INTO products_tbl (ID_number, Nomenclature) VALUES (12345,“Orbitron 4000”);
mysql> SHOW COLUMNS FROM products_tbl;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| ID_number | int(5) | | | | |
| Nomenclature| char(13) | | | | |
+-------------+-------------+------+-----+---------+-------+
您可以插入多行 −
You can insert multiple rows −
INSERT INTO products VALUES (1, “first row”), (2, “second row”);
您还可以使用 SET 子句 −
You can also employ the SET clause −
INSERT INTO products SELECT * FROM inventory WHERE status = 'available';
PHP Insertion Script
在 PHP 函数中使用相同的 “INSERT INTO…” 语句来执行该操作。您将再次使用 mysql_query() 函数。
Employ the same “INSERT INTO…” statement within a PHP function to perform the operation. You will use the mysql_query() function once again.
查看下面给出的示例 −
Review the example given below −
<?php
if(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() ) {
$product_name = addslashes ($_POST['product_name']);
$product_manufacturer = addslashes ($_POST['product_name']);
} else {
$product_name = $_POST['product_name'];
$product_manufacturer = $_POST['product_manufacturer'];
}
$ship_date = $_POST['ship_date'];
$sql = "INSERT INTO products_tbl ".
"(product_name,product_manufacturer, ship_date) ".
"VALUES"."('$product_name','$product_manufacturer','$ship_date')";
mysql_select_db('PRODUCTS');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
?>
当数据插入成功时,您将看到以下输出 −
On successful data insertion, you will see the following output −
mysql> Entered data successfully
您还可以将验证语句与插入语句配合使用,例如检查以确保数据输入正确。MariaDB 为此目的包含许多选项,其中一些是自动的。
You will also collaborate validation statements with insert statements such as checking to ensure correct data entry. MariaDB includes a number of options for this purpose, some of which are automatic.