• Home
  • DBA Scripts
    • Oracle Scripts
    • SQL Server Scripts
  • Knowledge Base
    • Oracle Database
    • MS SQL Server
    • MongoDB
    • MariaDB
  • Troubleshoot
    • Oracle Database Issues
    • SQL Server Issues
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • Courses
    • Oracle Database
    • Oracle DBA L1
    • Oracle DBA L2
    • Oracle DBA L3
  • Home
  • DBA Scripts
    • Oracle Scripts
    • SQL Server Scripts
  • Knowledge Base
    • Oracle Database
    • MS SQL Server
    • MongoDB
    • MariaDB
  • Troubleshoot
    • Oracle Database Issues
    • SQL Server Issues
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • Courses
    • Oracle Database
    • Oracle DBA L1
    • Oracle DBA L2
    • Oracle DBA L3
home/Knowledge Base/MariaDB/MariaDB – Insert Query
Popular Search:Oracle, SQL Server, MongoDB

MariaDB – Insert Query

74 views 0 June 2, 2020 admin

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,...);

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: The VALUES() function that appears within the statement, only applies to INSERT statements and returns NULL if used elsewhere.

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:

mysql> 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”);

You can also employ the SET clause:

INSERT INTO products SELECT * FROM inventory WHERE status = 'available';

PHP Insertion Script

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);
}
else
{
?>

On successful data insertion, you will see the following output:

mysql> Entered data successfully

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.

Was this helpful?

Yes  No
Related Articles
  • MariaDB – Where Clause
  • MariaDB – Select Query
  • MariaDB – Drop Tables
  • MariaDB – Create Tables
  • MariaDB – Data Types
  • MariaDB – Select Database
Leave A Comment Cancel reply

MariaDB
  • MariaDB – Insert Query
  • MariaDB – Where Clause
  • MariaDB – Select Query
  • MariaDB – Drop Tables
  • MariaDB – Create Tables
  • MariaDB – Data Types
View All 14  
Popular Articles
  • Upgrade Oracle Database from 11.2.0.3 to 12.1.0.2
  • Upgrading MariaDB on Windows
  • Oracle DBA Basic Interview Questions Part A
  • Purging Oracle Sysaux Tablespace
  • Client – Server Architecture
KB Categories
  • Oracle Database
    • Oracle RAC
    • Oracle ASM
    • Oracle GoldenGate
    • Oracle Tuning
    • Oracle 11g Database
    • Oracle Database Upgrade
    • Oracle 12c Database
    • ALL KB Oracle
    • Oracle 18c Database
    • Oracle Standby Database
  • MongoDB
  • MS SQL Server
  • MySQL
  • Interview Questions
    • AWS Interview Questions
    • Oracle DBA Interview Questions
    • SQL Server Interview Questions
  • MariaDB
Database Organization

Database Organization (DB ORG) is knowledge base for DBA to learn and execute the fundamental of different databases under one website. DB ORG is a non-profit initiative. ORACLE, MS SQL Server, MongoDB, MariaDB, Couchbase

Join Our Community
  • KnowledgeBase
  • Documentation
  • Troubleshoot
  • FAQ
Information Links
  • About DBOrg
  • Licenses
  • Terms
  • Privacy Policy
Contact Us
    DB ORG - Database Administration,
    Knowledge Base for DBA
    Mail: support@databaseorg.com
    WhatsApp: (+91) 9306440957
    Monday to Friday: EST - 11:30 AM to 06:30 PM (IST - 9:00 PM to 4:00 AM)
  • © 2023 Database Organization - DB ORG. All Rights Reserved.

Popular Search:Oracle, SQL Server, MongoDB

WhatsApp DB Org