Osdc Cheatsheet-Mariadb
Osdc Cheatsheet-Mariadb
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
Analyzing data
List databases > SHOW DATABASES;
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';
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;