Open In App

MariaDB INSERT Statement

MariaDB is a famous open-source relational database management system. It is renowned for its performance, scalability, and robust functions. One essential thing about operating with databases is the ability to insert information, and the INSERT statement plays a crucial role in this technique. In this article, we can explore the MariaDB INSERT declaration, its syntax, and the numerous approaches it can utilize to feature records to tables.

INSERT Statement in MariaDB

In MariaDB, new rows of data are added to a table using the INSERT command. It allows you to directly specify each column’s values. In the same order as the table’s columns, it offers a collection of values.



Syntax:

INSERT INTO table_name (field1, field2, field3,...) VALUES (value1, value2, value3, ...);

Example: INSERT Statement

Let’s see some examples of INSERT statements in MariaDB. Create a table of employees.



Create Table

CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
salary DECIMAL(10, 2),
Hire_date DATE
);

Example 1: Inserting Data into All Columns

INSERT INTO employees (employee_id, first_name, last_name, salary, Hire_Date) VALUES (1, 'John', 'Doe', 50000.00, ‘2023-12-31’);

This query will insert the first row in the employees table. Now check the data is inserted or not using the SELECT Statement

SELECT * FROM employees;

INSERT Statement

Example 2: Insert Data into Specific Columns

INSERT INTO employees (employee_id, first_name, Hire_Date) VALUES (2, 'Vivek', '2020-12-15');

This query will only insert the data into specific columns like employee_id, first_name and Hire_Date.

Check the inserted data.

INSERT Statement

Example 3: Inserting Bulk Data into Table

INSERT INTO employees (employee_id, first_name, last_name, salary, Hire_Date) VALUES 
(3, ‘Maram’, ‘Harsha’, 50000.00, ‘2022-12-14’),
(4, ‘Kavya’,’Sharma’,40000.00,’2023-01-16’);

We are inserting data in multiple rows with this query. Now check whether the data is inserted successfully or not using SELECT statement.

INSERT Statement

Example 4: Inserting Null Values

INSERT INTO employees (employee_id, first_name, last_name,  salary, Hire_Date) VALUES (5, 'Minal', NULL, 70000.00,'2022-03-10');


If a column allows a NULL value and you do not assign a value to that column in an INSERT operation, or We explicitly use the NULL keyword, the database inserts a NULL value into that column.

INSERT Statement

Example 5: Inserting Data with Default Values

INSERT INTO employees (employee_id, first_name, last_name, salary, Hire_Date) VALUES (6, 'Asad',DEFAULT,20000,'2022-12-14');


The DEFAULT key-word is used to specify that the required default value need to be used for the corresponding column within the table. If a column does no longer explicitly specify a value in the VALUES clause of an INSERT operation, and a default cost for that column is described inside the desk’s schema, the default price is used. If a column does now not have a default value defined within the schema and you try and insert a row without assigning a value for that column, and specifying the DEFAULT keyword, the INSERT declaration will generate an errors or it’ll leave the value empty relying upon your database and its settings.

INSERT Statement

Conclusion

The INSERT statement is critical for anyone running with MariaDB or every other relational database. Whether you’re placing a single record or a couple of pieces of records, understanding the syntax and alternatives of the INSERT statement allows you to manage and manipulate your database more correctly.

Article Tags :