MariaDB Practice: Creating and Querying a Database
In this practice, we will cover how to create and query a database in MariaDB. To complete this practice, you will need access to a MariaDB server. If you do not have access to a MariaDB server, you can set up a local MariaDB server by following the tutorial I provided earlier.
Step 1: Connect to MariaDB To connect to a MariaDB server, you will use the mysql
client. To connect to the MariaDB server, use the following command:
cssCopy codemysql -u root -p
You will be prompted to enter the root password for the MariaDB server.
Step 2: Create a Database Once you are connected to the MariaDB server, you can create a database. To create a database, you can use the following SQL statement:
sqlCopy codeCREATE DATABASE mydatabase;
Step 3: Create a Table Next, you can create a table in the database you just created. To create a table, you can use the following SQL statement:
sqlCopy codeUSE mydatabase;
CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(255),
age INT
);
This will create a table named mytable
in the mydatabase
database. The table has three columns: id
, name
, and age
. The id
column is the primary key, which means that it is used to uniquely identify rows in the table.
Step 4: Insert Data into the Table Now that you have created a table, you can insert data into the table. To insert data into the table, you can use the following SQL statement:
sqlCopy codeINSERT INTO mytable (id, name, age)
VALUES (1, 'John Doe', 30),
(2, 'Jane Doe', 35),
(3, 'Jim Smith', 40);
This will insert three rows of data into the mytable
table.
Step 5: Query Data from the Table Finally, you can query data from the table. To query data from the table, you can use the following SQL statement:
sqlCopy codeSELECT * FROM mytable;
This will return all the rows from the mytable
table. You should see the three rows of data you inserted earlier.
Conclusion In this practice, we covered how to create and query a database in MariaDB. We created a database, created a table, inserted data into the table, and queried data from the table. This practice should provide a good foundation for further exploration and experimentation with MariaDB.