Managing Databases and Tables
Now that you have your MariaDB server up and running, you can start creating and managing databases and tables. In this section, we'll cover the basic operations for working with databases and tables in MariaDB.
Creating a Database
To create a new database, you can use the CREATE DATABASE
statement. For example, to create a database named my_database
, you would run the following command:
CREATE DATABASE my_database;
Selecting a Database
Before you can interact with the tables in a database, you need to select the database you want to use. You can do this using the USE
statement:
USE my_database;
Creating a Table
To create a new table, you can use the CREATE TABLE
statement. Here's an example of creating a table named users
with three columns:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL
);
Inserting Data into a Table
Once you have a table created, you can insert data into it using the INSERT INTO
statement:
INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');
INSERT INTO users (name, email) VALUES ('Jane Smith', '[email protected]');
Querying Data from a Table
To retrieve data from a table, you can use the SELECT
statement. Here's an example of selecting all the rows and columns from the users
table:
SELECT * FROM users;
You can also select specific columns or filter the results using WHERE
clauses, ORDER BY
, and other SQL keywords.
Modifying and Deleting Data
To update existing data in a table, you can use the UPDATE
statement:
UPDATE users SET email = '[email protected]' WHERE name = 'Jane Smith';
To delete data from a table, you can use the DELETE FROM
statement:
DELETE FROM users WHERE id = 1;
By mastering these basic database and table management operations, you'll be well on your way to building powerful applications with MariaDB.