MariaDB Basic CRUD Tutorial
Objectives
-
MariaDB Insert Data into Table
-
MariaDB Select Data from Table
-
MariaDB Select Specific Data from Table
-
MariaDB Update Existing Data to Table
-
MariaDB Delete Data from Table
Create database and table if it is not created already
--- Create database if not exist
CREATE DATABASE IF NOT EXISTS mariadb_tutorial;
--- Select the database for further operation
USE mariadb_tutorial;
--- Create table if not exist
CREATE TABLE IF NOT EXISTS person (
id int(12) NOT NULL AUTO_INCREMENT,
first_name varchar(150) NOT NULL,
last_name varchar(150),
email varchar(100),
age int,
income double,
PRIMARY KEY (id)
)
MariaDB Insert Data into Table
Syntax
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
-- OR If you are adding values for all the columns of the table with sequence
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Example
INSERT INTO person (first_name, last_name, email, age, income) VALUES ('Touhid', 'Mia', 'hmtmcse.com@gmail.com', 30, 500);
-- OR
INSERT INTO person VALUES (NULL,'Touhid', 'Mia', 'hmtmcse.com@gmail.com', 30, 500);
Output
Query OK, 1 row affected (0.594 sec)
MariaDB Select Data from Table
Syntax
SELECT column1, column2, ... FROM table_name;
-- OR Show all columns from the table
SELECT * FROM table_name;
Example
SELECT first_name, email, age FROM person;
-- OR Show all columns from the table
SELECT * FROM person;
Output
+----+------------+-----------+-----------------------+------+--------+
| id | first_name | last_name | email | age | income |
+----+------------+-----------+-----------------------+------+--------+
| 1 | Touhid | Mia | hmtmcse.com@gmail.com | 30 | 500 |
| 2 | Touhid | Mia | hmtmcse.com@gmail.com | 30 | 500 |
+----+------------+-----------+-----------------------+------+--------+
2 rows in set (0.000 sec)
MariaDB Select Specific Data from Table
Syntax
SELECT * FROM table_name WHERE condition;
Example
SELECT * FROM person WHERE id = 1;
Note : Here we update id number 1, you saw the id from select query earlier.
Output
+----+------------+-----------+-----------------------+------+--------+
| id | first_name | last_name | email | age | income |
+----+------------+-----------+-----------------------+------+--------+
| 1 | Touhid | Mia | hmtmcse.com@gmail.com | 30 | 500 |
+----+------------+-----------+-----------------------+------+--------+
1 row in set (0.000 sec)
MariaDB Update Existing Data to Table
Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
Example
UPDATE person SET income = 1000, age = 31, email = 'hmtmcsecom@gmail.com' WHERE id = 1;
Note : Here we update id number 1, you saw the id from select query earlier.
Output
Query OK, 1 row affected (0.674 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB Delete Data from Table
Syntax
DELETE FROM table_name WHERE condition;
Example
DELETE FROM person WHERE id = 1;
Note : Here we update id number 1, you saw the id from select query earlier.
Output
Query OK, 1 row affected (0.108 sec)