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:
FROM/JOIN: The database grabs the raw tables and combines them.WHERE: Filters individual rows. (The database doesn’t know what the final counts or totals are yet, so you cannot useCOUNT()here).GROUP BY: Groups the remaining rows together (e.g., grouping rows bycustomer_id).HAVING: Filters the aggregated groups. (The database has now calculated the counts for each group, makingCOUNT(order_id) > 5possible).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, andDROPdefine the schema, tables, and views. - DML (Data Manipulation Language): Used to manage the actual records within those structures. Commands like
INSERT,UPDATE, andDELETEmanipulate the data itself. - DCL (Data Control Language): Used by database administrators to manage security and user access. Commands like
GRANTandREVOKEcontrol permissions.