<> = MySQL = == Connect mysql host == {{{#!highlight bash mysql -h localhost -u userx dbx -ppasswordx mysql -h localhost -u userx dbx -p # asks for password later }}} == Dynamic SQL , conditional where == {{{#!highlight sql SELECT first_name, last_name, subsidiary_id, employee_id FROM employees WHERE ( subsidiary_id = :sub_id OR :sub_id IS NULL ) AND ( employee_id = :emp_id OR :emp_id IS NULL ) AND ( UPPER(last_name) = :name OR :name IS NULL ) select * from mysql.user where (user=NULL or NULL is null) and (host=NULL or NULL is null) select * from mysql.user where (user='root' or 'root' is null) and (host=NULL or NULL is null) select * from mysql.user where (user='root' or 'root' is null) and (host='127.0.0.1' or '127.0.0.1' is null) }}} == MySql workbench Slackware64 14.1 == {{{#!highlight bash wget http://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-workbench-community-6.1.7-1.el6.x86_64.rpm cp ~/Downloads/mysql-workbench-community-6.1.7-1.el6.x86_64.rpm /tmp cd /tmp rpm2tgz mysql-workbench-community-6.1.7-1.el6.x86_64.rpm installpkg mysql-workbench-community-6.1.7-1.el6.x86_64.tgz }}} == mysqldump - backup database == {{{#!highlight bash mysqldump -h hostx -u userx -ppassx dbname > /tmp/dump.sql mysqldump -h 127.0.0.1 -u userx -ppass -P 3306 --ssl db_exampleDB > backup_exampleDB_20210524.sql # restore DB mysql -h 127.0.0.1 -u root -p???????? mysql < dump.sql }}} == Error MariaDB 5.5 mysqldump == * mysqldump: Couldn't execute 'SET OPTION SQL_QUOTE_SHOW_CREATE=1': Unknown system variable 'OPTION' (1193) * cp /usr/bin/mysqldump /tmp/msd * vim /tmp/msd * Escape, * /SET OPTION * R * replace option with spaces * Escape * :wq Use /tmp/msd -h hostx -u userx -ppassx dbname > /tmp/dump.sql == Show users == {{{#!highlight sql select host,user,password from mysql.user; }}} == Create function == {{{#!highlight sql DELIMITER $$ DROP FUNCTION IF EXISTS hello; CREATE FUNCTION hello() RETURNS TEXT LANGUAGE SQL BEGIN RETURN 'Hello'; END; $$ DELIMITER ; -- test function select hello(); set sql_mode='ansi'; show create function hello; }}} == User creation and grants to select and execute function == {{{#!highlight sql create user 'userx'@'%' identified by 'passwordx'; grant select on dbx.tablex to 'userx'@'%'; grant execute on function dbx.hello to 'userx'@'%'; }}} == Most used commands == {{{#!highlight sql help contents; help show; help create; show databases; use mysql; -- select database show tables; desc user; -- describe table user set sql_mode=ansi; -- set sql mode to ansi show create table user; -- show DDL to create table user show grants; -- show grants for current user show privileges; -- show privileges to be used in grants }}} == Trigger before insert to generate unique id == {{{#!highlight sql CREATE TRIGGER T_BEFORE_INSERT_TABLEX BEFORE UPDATE ON dbx.TABLEX FOR EACH ROW SET NEW.UUID = uuid() ; }}} == Install on CentOS 6.6 == {{{#!highlight bash yum install mysql yum install mysql-server service mysqld start mysql create user 'userx'@'%' identified by 'passwordx'; create user 'userx'@'localhost' identified by 'passwordx'; grant all on test.* to 'userx'@'%'; grant all on test.* to 'userx'@'localhost'; show grants for 'userx'@'%'; show grants for 'userx'@'localhost'; }}} == Create test table == {{{#!highlight sql CREATE TABLE MyGuests ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, firstname VARCHAR(30) NOT NULL, lastname VARCHAR(30) NOT NULL, email VARCHAR(50), reg_date TIMESTAMP ) insert into MyGuests (firstname,lastname,email,reg_date) values('AA','BB','xyz@test.com','2015-02-13T00:00:00'); insert into MyGuests (firstname,lastname,email,reg_date) values('CC','DD','aaaaxyz@test.com','2015-02-13T14:00:00'); select * from MyGuests; }}} == Install MariaDB debian buster == === Install === {{{#!highlight bash apt-get update apt install mariadb-server mysql_secure_installation # define root pass su mysql GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY '????????' WITH GRANT OPTION; FLUSH PRIVILEGES; create database testdb; show databases; exit }}} === SpringBoot JPA application.properties === {{{ spring.datasource.url=jdbc:mariadb://localhost:3306/testdb spring.datasource.driverClassName=org.mariadb.jdbc.Driver spring.datasource.username=admin spring.datasource.password=???????? spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true }}} === pom.xml === {{{#!highlight xml org.mariadb.jdbc mariadb-java-client 1.4.4 }}} == Create view == {{{#!highlight sql create or replace view v_test as select fielda, fieldb from v_test2 where fieldId = tablex.fieldId union select fielda, fieldb from v_test3 where fieldId = tablez.fieldId; }}} == Explain == * https://dev.mysql.com/doc/refman/8.0/en/table-scan-avoidance.html The output from EXPLAIN shows ALL in the type column when MySQL uses a full table scan to resolve a query. {{{#!highlight sql explain select * from tablex; }}} == Current date == {{{#!highlight sql select DATE_FORMAT(NOW(),'%Y-%m-%dT%TZ'); }}}