MySQL

SQL Cheat sheet

mysql -u root -p

  -- Reset password
  USE mysql;
  UPDATE mysql.user SET password = PASSWORD('my_pass') WHERE user = 'root';
  FLUSH PRIVILEGES;


  \h -- help
  \q -- quit

  status; -- details
  show databases; // SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA;
  SHOW DATABASES LIKE 'dbName'; -- check if exists
  SHOW DATABASES WHERE `database` = 'dbName' --safe way to check
  use <databaseName>;
  CREATE DATABASE IF NOT EXISTS <databaseName>;
  show tables;
  describe <tableName>; // desc <tableName>; // explain <tableName>; // SHOW COLUMNS FROM <tableName>;
  describe <databaseName>.<tableName>;
  create table <tableName> (id int primary key, name varchar(20));
  create table <databaseName>.<tableName> (id int primary key, name varchar(20)) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE =   ;
  show create table [db_name].table_name;
  drop table <tableName>;
  select <columnName> from <tableName>;
  select * from <tableName>;
  if exists/ if not exists;
  DEFAULT CHARACTER SET utf8 ;