https://www.tutorialspoint.com/mariadb
# mysqladmin –version
mysql> SHOW DATABASES;
# mysqladmin –u root password “[enter your password here]”;
# mysql –u root –p #connect to the server with your new credentials
#Administration
Three options for starting and stopping MariaDB.
‘newusername’@’localhost’ IDENTIFIED BY ‘userpassword’;
GRANT SELECT, INSERT, UPDATE, DELETE ON database1 TO ‘newusername’@‘localhost’;
A “FLUSH PRIVILEGES” operation tells the server to reload the grant tables.
MariaDB uses the system tables created with “mysql_install_db” to manage user rights and provide the data for the ‘help’ command
# mysql –u root –p
# mysqladmin –u root –p create PRODUCTS
mysql > CREATE DATABASE PRODUCTS;
#mysqladmin –u root –p drop PRODUCTS;
mysql > DROP DATABASE PRODUCTS;
MariaDB data types can be categorized as numeric, date and time, and string values.
TINYINT, BOOLEAN, SMALLINT, MEDIUMINT, INT, BIGINT, DECIMAL, FLOAT, DOUBLE, BIT
DATE, TIME, DATETIME, TIMESTAMP, YEAR
mysql> CREATE TABLE products_tbl(product_id INT NOT NULL AUTO_INCREMENT, product_name VARCHAR(100) NOT NULL, product_manufacturer VARCHAR(40) NOT NULL, submission_date DATE, PRIMARY KEY ( product_id ));
mysql> DROP TABLE products_tbl;
mysql> INSERT INTO tablename (field, field2, …) VALUES (value, value2, …), (value, value2, …), (value, value2, …),…;
mysql> SELECT field, field2,… FROM table_name, table_name2,… WHERE…;
[COMMAND] field,field2,… FROM table_name,table_name2,… WHERE [CONDITION]
mysql> UPDATE table_name SET field=new_value, field2=new_value2,…[WHERE …]
mysql> DELETE FROM table_name [WHERE …]
mysql> SELECT product_id, product_name, product_manufacturer, ship_date FROM products_tbl WHERE product_manufacturer LIKE “xyz%”
mysql> SELECT product_id, product_name, product_manufacturer, ship_date FROM products_tbl ORDER BY product_manufacturer DESC;
mysql> SELECT products.ID_number, products.Nomenclature, inventory.inventory_ct FROM products INNER JOIN inventory ON products.ID_numbeer = inventory.ID_number;
NULL is case-insensitive, and has these two aliases – UNKNOWN (a boolean value), \N
DESC order results in NULL values at the bottom.
IFNULL(), NULLIF()
On insertion of a NULL value in a column declared NOT NULL, an error occurs.
Beyond the pattern matching available from LIKE clauses, MariaDB offers regular expression-based matching through the REGEXP/NOT REGEXP operator.
mysql> SELECT column FROM table_name WHERE column REGEXP ‘[PATTERN]’;
Transactions are sequential group operations. They function as a single unit, and do not terminate until all operations within the group execute successfully. A single failure in the group causes the entire transaction to fail, and causes it to have no impact on the database.
Transactions conform to ACID (Atomicity, Consistency, Isolation, and Durability)
The ALTER command provides a way to change an existing table’s structure, meaning modifications like removing or adding columns, modifying indices, changing data types, or changing names. ALTER also waits to apply changes when a metadata lock is active.
mysql> ALTER TABLE products_tbl DROP version_num;
mysql> ALTER TABLE products_tbl ADD discontinued CHAR(1);
ALTER TABLE products_tbl ADD discontinued CHAR(1) FIRST;
ALTER TABLE products_tbl ADD discontinued CHAR(1) AFTER quantity;
mysql> ALTER TABLE products_tbl CHANGE discontinued status CHAR(4);
mysql> ALTER TABLE products_tbl MODIFY discontinued CHAR(4);
mysql> ALTER TABLE products_tbl ALTER discontinued SET DEFAULT N;
mysql> ALTER TABLE products_tbl ALTER discontinued DROP DEFAULT;
mysql> ALTER TABLE products_tbl TYPE = INNODB;
mysql> ALTER TABLE products_tbl RENAME TO products2016_tbl;
#Indexes & Statistics Tables
The terms “key” and “index” are identical in this usage.
CREATE [UNIQUE or FULLTEXT or…] INDEX index_name ON table_name column;
CREATE UNIQUE INDEX top_sellers ON products_tbl product;
You can drop an index with DROP INDEX or ALTER TABLE…DROP.
DROP INDEX index_name ON table_name;
DROP INDEX top_sellers ON product_tbl;
ALTER TABLE table_name DROP INDEX index_name, ADD INDEX new_index_name;
mysql > SHOW INDEX FROM products_tbl\G
MariaDB 10.0 made storage engine independent statistics tables available, which calculate data statistics for every table in every storage engine, and even statistics for columns that are not indexed.
The TEMPORARY keyword within a CREATE TABLE statement spawns a temporary table.
mysql>CREATE TEMPORARY TABLE order ( item_name VARCHAR(50) NOT NULL, price DECIMAL(7,2) NOT NULL DEFAULT 0.00, quantity INT UNSIGNED NOT NULL DEFAULT 0);
GRANT CREATE TEMPORARY TABLES ON orders TO ‘machine122’@‘localhost’;
The procedure for duplicating a table is as follows –
Another method for creating a duplicate uses a CREATE TABLE AS statement.
CREATE TABLE clone_tbl AS SELECT columns FROM original_tbl WHERE conditions];
INSTALL SONAME “ha_sequence”;
SHOW ENGINES\G #verify the installation
SELECT * FROM seq_77_to_99;
SELECT myseq.seq FROM seq_22_to_28 myseq LEFT JOIN table1 t ON myseq.seq= x.y WHERE x.y IS NULL;
SELECT x1.seq, x2.seq FROM seq_5_to_9 x1 JOIN seq_5_to_9 x2 ORDER BY 5, 6;
SELECT seq FROM seq_3_to_100_step_4;
Always consider data entered by users, suspect and are in need of strong validation prior to any processing. Perform this validation through pattern matching.
Also, utilize the REGEXP operator and LIKE clauses in creating input constraints.
Consider all types of necessary explicit control of input such as:
Logical backups offer the flexibility of restoring data on another machine with a different configuration in contrast to physical backups, which are often limited to the same machine and database type. Logical backups occur at database and table level, and physical occur at directory and file level.
Physical backups are smaller in size than logical, and also take less time to perform and restore. Physical backups also include log and configuration files, but logical backups do not.
The main tool used for MariaDB backups is mysqldump. It offers logical backups and flexibility. There are three options for mysqldump backups — Raw data, Data/Definitions export, and Transfer.
SELECT customer_id, firstname, surname INTO OUTFILE ‘/exportdata/customers.txt’ FIELDS TERMINATED BY ‘,’ OPTIONALLY ENCLOSED BY ‘"’ LINES TERMINATED BY ‘\n’ FROM customers;
XtraBackup, Snapshots, LVM, TokuBackup
mysql> LOAD DATA LOCAL INFILE ‘products_copy.txt’ INTO TABLE empty_tbl FIELDS TERMINATED BY ‘|’ LINES TERMINATED BY ‘\n’;
The mysqlimport tool acts as a LOAD DATA wrapper allowing the same operations from the command line.
$ mysqlimport -u root -p --local --fields-terminated-by=“|” --lines-terminated-by=“\n” database_name source_file.txt
Restoring with mysqldump requires this simple statement for loading the dump file back into the host −
shell> mysql database_name < source_file.sql
COUNT, MIN, MAX, AVG, SUM
SELECT ID, DOB, TIMESTAMPDIFF(YEAR,DOB,‘2015-07-01’) AS age FROM officer_info;
SELECT CONCAT(‘Ram’, ‘bu’, ‘tan’);
CURDATE(),DATE(), CURTIME(), DATE_SUB(), DATEDIFF(), DATE_ADD(), EXTRACT(), NOW(), DATE_FORMAT(), HOUR(), LOCALTIME(), MICROSECOND(), MINUTE(), SECOND(), TIME_FORMAT(), TIMESTAMP()
TRUNCATE(), COS(), CEILING(), DEGREES(), DIV(), EXP(), FLOOR(), LN(), LOG(), SQRT()
INSTR(), RIGHT(), LENGTH(), LOCATE(), INSERT(), LEFT(), UPPER(), LOWER(),STRCMP(), REPLACE(), REVERSE(), REPEAT(), SUBSTRING(), TRIM()