0% found this document useful (0 votes)
80 views

Osdc Cheatsheet-Mariadb

This document provides a cheat sheet for MariaDB and MySQL commands. It lists commands for connecting to databases, analyzing data by listing databases and tables or selecting fields, managing users by creating users and granting privileges, manipulating data by creating databases and tables or inserting/updating data, and performing joins between tables. The cheat sheet is intended to provide the essential SQL commands for both interactive use and scripts.

Uploaded by

Gino Cadiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Osdc Cheatsheet-Mariadb

This document provides a cheat sheet for MariaDB and MySQL commands. It lists commands for connecting to databases, analyzing data by listing databases and tables or selecting fields, managing users by creating users and granting privileges, manipulating data by creating databases and tables or inserting/updating data, and performing joins between tables. The cheat sheet is intended to provide the essential SQL commands for both interactive use and scripts.

Uploaded by

Gino Cadiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MariaDB & mySQL Cheat Sheet By Seth Kenlon

MariaDB and mySQL are open source databases that use Structured Query Language (SQL)
for interaction. The commands on this cheat sheet are valid for the interactive prompt and
SQL scripts, but much can be extrapolated for use with programming libraries as well.

Connecting
Connect to local database server $ mysql --user=root -p –-host localhost

Connect to remote database server $ mysql –user=root -p –host example.com

Place credentials in ~/.my.cnf and set [client]


permissions to 0600 to enable user=root
automatic authentication
password=my_secure_passphrase

Analyzing data
List databases > SHOW DATABASES;

Make example the active database > use example;

List tables in current database > SHOW TABLES;

List columns in mytable table > SHOW COLUMNS IN mytable;

Display contents of foo and bar fields > SELECT foo,bar FROM mytable;
from mytable +-----+-----+
| foo | bar |
+-----+-----+
| one | baz |
+-----+-----+
Display specified fields in mytable if a > SELECT user,host,select_priv
field matches a given value → FROM mytable WHERE user='tux'; FROM mytable WHERE user='tux';

opensource.com Twitter @opensourceway • Fosstodon.org/@osdc CC BY-SA 4.0


MariaDB & mySQL Cheat Sheet By Seth Kenlon

User management
Create a new user tux on localhost > CREATE USER ‘tux’@’localhost’
→ FROM mytable WHERE user='tux'; IDENTIFIED BY ‘tux_password’;
Grant select privileges to user tux > GRANT SELECT ON *.*
→ FROM mytable WHERE user='tux'; TO ‘tux’@’localhost’;
Manipulating data
Create database > CREATE DATABASE;

Create table and define fields > CREATE table IF NOT EXISTS mytable (
→ FROM mytable WHERE user='tux'; id INT auto_increment PRIMARY KEY,
→ FROM mytable WHERE user='tux'; foo varchar(64) NOT NULL);
Insert data into a table > INSERT INTO mytable (foo)
→ FROM mytable WHERE user='tux'; VALUES ('aaa'), ('bbb'),('ccc');
Query OK, 3 rows affected (0.1 sec)
Add a new column to mytable > ALTER TABLE mytable ADD COLUMN (
→ FROM mytable WHERE user='tux'; bar INT);
Update data in a table > UPDATE mytable SET foo=’aaa’
→ FROM mytable WHERE user='tux'; WHERE id=1;
Joins
Display an inner join > SELECT * FROM mytable
→ FROM mytable WHERE user='tux'; JOIN othertable
→ FROM mytable WHERE user='tux'; ON mytable.id=othertable.foo;
Display a right (or left) join > SELECT * FROM mytable RIGHT JOIN
→ FROM mytable WHERE user='tux'; ON mytable.id=othertable.foo;
Display a full join > SELECT foo,bar,baz FROM mytable
→ FROM mytable WHERE user='tux'; FULL JOIN othertable
→ FROM mytable WHERE user='tux'; ON mytable.id=othertable.quux;

opensource.com Twitter @opensourceway • Fosstodon.org/@osdc CC BY-SA 4.0

You might also like