CyanHall.com 创建于 2020-11-13, 上次更新:2021-04-30。
👉  github shields 如果有用请点赞。

1. 命令行登录

    mysql -h localhost -u root -p
// /usr/local/Cellar/[email protected]/x.xx.xx/bin/mysql -h localhost -u root
  

2. 导入 SQL 文件

    mysql -u username -p database_name < file.sql
  

3. 列出所有数据库

    show databases;
  

4. 列出一个数据库中的所有表

    use [db name];
show tables;
  

5. 列出所有用户

    SELECT User, Host, authentication_string FROM mysql.user;
  

6. 创建用户和数据库

    # Create a user
CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'password';

# Create a database
CREATE DATABASE example_db;

# Grant privileges
GRANT ALL ON example_db.* TO 'db_user'@'localhost';
  

7. 删除用户和数据库

    # Delete database
DROP DATABASE example_db;

# Delete user
DROP USER 'db_user'@'localhost';
  

8. 退出命令行

    quit
  

9. sql_mode

    SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

# result: 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

SET GLOBAL sql_mode = '...';
SET SESSION sql_mode = '...';

# Remove ONLY_FULL_GROUP_BY
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
  

10. JOIN

    INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行
# LEFT JOIN and LEFT OUTER JOIN are equivalent.
  

11. UUID

    select uuid_short() as id;
  

1. 命令行登录

    mysql -h localhost -u root -p
// /usr/local/Cellar/[email protected]/x.xx.xx/bin/mysql -h localhost -u root
  

3. 列出所有数据库

    show databases;
  

5. 列出所有用户

    SELECT User, Host, authentication_string FROM mysql.user;
  

7. 删除用户和数据库

    # Delete database
DROP DATABASE example_db;

# Delete user
DROP USER 'db_user'@'localhost';
  

9. sql_mode

    SELECT @@GLOBAL.sql_mode;
SELECT @@SESSION.sql_mode;

# result: 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

SET GLOBAL sql_mode = '...';
SET SESSION sql_mode = '...';

# Remove ONLY_FULL_GROUP_BY
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
  

11. UUID

    select uuid_short() as id;
  

2. 导入 SQL 文件

    mysql -u username -p database_name < file.sql
  

4. 列出一个数据库中的所有表

    use [db name];
show tables;
  

6. 创建用户和数据库

    # Create a user
CREATE USER 'db_user'@'localhost' IDENTIFIED BY 'password';

# Create a database
CREATE DATABASE example_db;

# Grant privileges
GRANT ALL ON example_db.* TO 'db_user'@'localhost';
  

8. 退出命令行

    quit
  

10. JOIN

    INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行
# LEFT JOIN and LEFT OUTER JOIN are equivalent.
  


Maitained by Cyanhall.com, Copy Rights @ CC BY-NC-SA 4.0