The MariaDB Query Language (SQL) is used to communicate with MariaDB databases. Here are some basics to get you started:
1. Connecting to MariaDB:
Use the mysql command-line client to connect to your MariaDB server:
mysql -u username -p
Replace username with your username. You'll be prompted to enter your password.
2. Creating a Database:
To create a new database, use the CREATE DATABASE statement:
CREATE DATABASE mydatabase;
3. Using a Database:
To switch to a specific database, use the USE statement:
USE mydatabase;
4. Creating Tables:
Use the CREATE TABLE statement to create a new table within your database:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(100)
);
5. Inserting Data:
Use the INSERT INTO statement to add data into a table:
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
6. Selecting Data:
Use the SELECT statement to retrieve data from a table:
SELECT * FROM users;
7. Updating Data:
Use the UPDATE statement to modify existing data in a table:
UPDATE users SET email = 'newemail@example.com' WHERE id = 1;
8. Deleting Data:
Use the DELETE FROM statement to remove data from a table:
DELETE FROM users WHERE id = 1;
9. Filtering Data:
Use the WHERE clause to filter data based on specific conditions:
SELECT * FROM users WHERE name = 'John';
10. Sorting Data:
Use the ORDER BY clause to sort the results:
SELECT * FROM users ORDER BY name ASC;
11. Grouping Data:
Use the GROUP BY clause to group rows that have the same values:
SELECT department, COUNT(*) FROM employees GROUP BY department;
12. Joining Tables:
Use JOIN to combine rows from two or more tables based on a related column:
SELECT users.name, orders.order_id FROM users JOIN orders ON users.id = orders.user_id;
These are just the basics of SQL querying with MariaDB. As you become more familiar with SQL, you can explore more advanced topics such as subqueries, transactions, indexes, and stored procedures.
No comments:
Post a Comment