Updated April 12, 2023
Introduction to MariaDB insert
MariaDB is one of the world-famous open source database systems. MariaDB works similar to MySQL, which means most of the statements are similar to MySQL, in which that insert statement is similar in MySQL and MariaDB. The insert statement is used to insert records or insert new rows into the table. With the help of an insert statement or command, we can insert a single row or multiple rows at a time, and it comes under the data manipulation command category. When we insert a record into the table by using an insert statement, then we must insert a value for every not null column. We can delete the column from the insert statement if the column allows null values.
Syntax of MariaDB insert
For single-row insertion operation.
insert into table name (colm_name_1 , colm_name_2,….. colm_name_N) values (statement 1, statement 2,…..statement N), (statement 1, statement 2,…..statement N),………;
Different parameters used in the above syntax as follows:
- insert into: The insert into command is used to insert records into the specified table.
- table name: Table name means an actual table that we need to insert new records.
- column name: It is a specified column name from the table to insert the values.
- statement: Statement is used to assign the values to the column in the table, so column name 1 is assigned values of statement 1; similarly, we consider all statements.
The syntax for multiple row insertion operation by using sub select statement in MariaDB.
insert into table name (colm_name_1 , colm_name_2,….. colm_name_N) select statement 1, statement 2 from source table name [Where condition]
Basically, this statement is useful when we insert records from another table.
Some of the parameters are similar like the first syntax here; some parameters are added as follows:
- source table name: Source Table Name means we can insert records from another table that table name means source table name.
- where condition: Where condition is an optional part of this syntax, but when we use where condition in a statement that must be stratified insert condition.
How does insert Statement Works in MariaDB?
Basically, insert statement allows us to add new records into the existing table by using the above syntax. Normally syntax of insert statement followed by the table name, after that column name and values this is a simple structure of insert statement.
Let’s see how to insert a statement in MariaDB as follows:
- Insert statement uses single or double quotes for the string value.
- If we want to skip column name from the column list, then we must ensure that the skip column has a default value; otherwise, an error will occur.
In MariaDB, we use the following expression when we skip column name from column list at the time of insert operation.
- The column name has an auto_increment property for the next sequential integer.
- Sometimes column names have a default value.
- If the value is NULL, then a column is a null column.
Examples of MariaDB insert
Let’s see how MariaDB insert statements work with the help of an example as follows:
For insertion operation, we need a table to create a table by using the following statement as follows.
create table emp( emp_id int auto_increment, emp_name varchar(255) not null, emp_dept varchar(255) not null, emp_address varchar(255) not null, primary key(emp_id) );
With the help of the above statement, we created emp with different parameters such as emp_id, emp_name, emp_dept, emp_address with different data types as shown in the above statement. Here we assign emp_id as the primary key. The result of the above statement we illustrate by using the following snapshot.
Now we can insert records into the emp table by using the following statement as follows.
Insert into emp(emp_name, emp_dept, emp_address) values ("John", "Mech", "Mumbai"); select * from emp;
With the help of a statement, we inserted a single row into the emp table; see in this example, we only inserted one row. The result of the above statement we illustrate by using the following snapshot.
Now let’s see how we can insert multiple rows by using insert into a statement as follows.
Insert into emp(emp_name, emp_dept, emp_address) values ("Jenny", "Comp", "Londan"), ("Sam", "Account", "Mumbai") ; select * from emp;
In the above example, we insert two rows at a time as shown in the above statement; here, we inserted two records into the emp table with different values. The result of the above statement we illustrate by using the following snapshot.
Now let’s see how we can insert values from another table as follows.
We have another table name as a customer with different attributes such as cust_id, cust_name cust_dept and cust_address. Now we need to insert a record from the emp table to the customer by using the following statement.
select emp_name, emp_dept, emp_address from emp where emp_id=1; insert into customer (cust_id, cust_name, cust_dept, cust_address) select 1, emp_name, emp_dept, emp_address from emp where emp_id=1;
In the above example, we combine select and insert statements as shown in the above statement. The result of the above statement we illustrate by using the following snapshot.
select * from customer;
Rules and Regulation for Using the insert Statement
- Basically, every time we cannot insert a value for a single column, some column we left as blank and provides a default value.
- In some cases, columns automatically generate their own value; in this case, there is no need to try and insert our own values.
- In insert statements, we always match the order of values and column, data type and number.
- If the value of the column is string, data, time or character, at that time, we need to enclose them in single quotes, and if the value of character is number or integer, then there is no need to quote.
- If we don’t want to insert values into the column list, we must insert values into all columns into the table and maintain the order of values.
From the above article, we saw the basic syntax of MariaDB insert statements, and we also saw different examples of insert statements. We also saw the rules of MariaDB insert statements. From this article, we saw how and when we use MariaDB insert statements.
We hope that this EDUCBA information on “MariaDB insert” was beneficial to you. You can view EDUCBA’s recommended articles for more information.