SQL

List databases and use the database ‘mydb’:

SHOW databases;
USE mydb;

List tables of the currently selected database:

SHOW tables;

Show ‘users’ table structure:

DESCRIBE users;

List the ‘username’ column of table ‘users’:

SELECT username FROM users;
SELECT MediaId,VolumeName FROM Media WHERE VolumeName='000797L4';

Update records:

UPDATE Media SET VolumeName = '000797L4' WHERE MediaId = 1201;

Create user:

create user user_name@localhost name identified by ‘pass_word’;

list users:

SELECT user, password, host FROM mysql.user;

change password:

SET PASSWORD FOR 'techonthenet'@'localhost' = PASSWORD('newpassword');

MySQL/MariaDB

mysqldump

mysql -u username -p database_name < file.sql

mysqldump bareos > /root/bareos.sql

load dump

mysql -p bareos < /root/bareos.sql

Show history:

more ~/.mysql_history

Misc

The SQL Order of Execution

Order in which a database engine actually processes your query:

  1. FROM / JOIN: The database grabs the raw tables and combines them.
  2. WHERE: Filters individual rows. (The database doesn’t know what the final counts or totals are yet, so you cannot use COUNT() here).
  3. GROUP BY: Groups the remaining rows together (e.g., grouping rows by customer_id).
  4. HAVING: Filters the aggregated groups. (The database has now calculated the counts for each group, making COUNT(order_id) > 5 possible).
  5. SELECT: Pulls the final columns and applies aliases.

SQL sublanguages

SQL commands are divided into functional sublanguages based on what they modify:

  • DDL (Data Definition Language): Used to build and modify the blueprint of the database. Commands like CREATE, ALTER, and DROP define the schema, tables, and views.
  • DML (Data Manipulation Language): Used to manage the actual records within those structures. Commands like INSERT, UPDATE, and DELETE manipulate the data itself.
  • DCL (Data Control Language): Used by database administrators to manage security and user access. Commands like GRANT and REVOKE control permissions.