• mysql数据库之必备知识


    mysql核心知识之服务管理

    查看mysql的服务进程: ps -ef | grep mysql

    service服务管理: cp -a mysql.server /etc/rc.d/init.d/mysql

    启动命令: service mysql start or systemctl mysql start

    关闭命令 service mysql stop  or systemctl mysql stop

    重启命令: service mysql restart or system mysql restart

    查看状态命令: service mysql status

    登录管理  ln -s /usr/local/mysql/bin/*  /bin

    登录命令: mysql -u root -p

    默认端口: 3306

    配置文件: /etc/my.cnf

    mysql 的库表深入解析

    • (1) 什么是库

    数据仓库

    mysql数据库:本质上是一个关系型数据服务管理系统

    • (2) 什么是表?

    数据库: database

    数据表: table

    字段(列):column

    行: row

    1. mysql> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | information_schema |
    6. | mysql |
    7. | performance_schema |
    8. | sys |
    9. +--------------------+
    10. 4 rows in set (0.00 sec)
    11. mysql> use mysql
    12. Reading table information for completion of table and column names
    13. You can turn off this feature to get a quicker startup with -A
    14. Database changed
    15. mysql>
    16. mysql>
    17. mysql> show tables;
    18. +---------------------------+
    19. | Tables_in_mysql |
    20. +---------------------------+
    21. | columns_priv |
    22. | db |
    23. | engine_cost |
    24. | event |
    25. | func |
    26. | general_log |
    27. | gtid_executed |
    28. | help_category |
    29. | help_keyword |
    30. | help_relation |
    31. | help_topic |
    32. | innodb_index_stats |
    33. | innodb_table_stats |
    34. | ndb_binlog_index |
    35. | plugin |
    36. | proc |
    37. | procs_priv |
    38. | proxies_priv |
    39. | server_cost |
    40. | servers |
    41. | slave_master_info |
    42. | slave_relay_log_info |
    43. | slave_worker_info |
    44. | slow_log |
    45. | tables_priv |
    46. | time_zone |
    47. | time_zone_leap_second |
    48. | time_zone_name |
    49. | time_zone_transition |
    50. | time_zone_transition_type |
    51. | user |
    52. +---------------------------+
    53. 31 rows in set (0.00 sec)
    54. mysql>

    mysql的sql各类语句精讲

    DDL 数据定义语言(Data Definition Language)

    DDL数据操作语言

    DQL数据查询语句

    DCL数据控制语言

    mysql 核心知识之DDL

    mysql 数据库必备知识之创建、查看以及使用/切换

    1. /*mysql数据库必备知识之创建、查看以及使用、切换*/
    2. 1) 直接创建数据库 db1
    3. CREATE database db1;
    4. (2) 判断是否存在,如果不存在则创建数据库 db2
    5. CREATE database IF NOT EXISTS db2;
    6. (3) 创建数据库并指定字符集为gbk
    7. CREATE database db3 DEFAULT CHARACTER SET gbk;
    8. (4) 查看某个库是什么字符集;
    9. SHOW CREATE DATABASE XD;
    10. (5) 查看当前mysql 使用的字符集
    11. show variables LIKE 'character%';
    12. 6) 查看当前数据库
    13. SELECT database();
    14. 7) 进入库名
    15. use 库名;

    [root@mysqlmaster mysql]# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 5.7.40 MySQL Community Server (GPL)

    Copyright (c) 2000, 2022, Oracle and/or its affiliates.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> select database();
    +------------+
    | database() |
    +------------+
    | NULL       |
    +------------+
    1 row in set (0.00 sec)

    mysql> create database xiaoming;
    Query OK, 1 row affected (0.01 sec)

    mysql> create database xiaoming;
    ERROR 1007 (HY000): Can't create database 'xiaoming'; database exists
    mysql>
    mysql> create database if not exists xiaoming;
    Query OK, 1 row affected, 1 warning (0.00 sec)

    mysql>
    mysql> create database db3 default character set gbk;
    Query OK, 1 row affected (0.00 sec)

    mysql> show create database xiaoming;
    +----------+---------------------------------------------------------------------+
    | Database | Create Database                                                     |
    +----------+---------------------------------------------------------------------+
    | xiaoming | CREATE DATABASE `xiaoming` /*!40100 DEFAULT CHARACTER SET latin1 */ |
    +----------+---------------------------------------------------------------------+
    1 row in set (0.02 sec)

    mysql> show create database db3;
    +----------+-------------------------------------------------------------+
    | Database | Create Database                                             |
    +----------+-------------------------------------------------------------+
    | db3      | CREATE DATABASE `db3` /*!40100 DEFAULT CHARACTER SET gbk */ |
    +----------+-------------------------------------------------------------+
    1 row in set (0.00 sec)

    mysql> create database xiaohong;
    Query OK, 1 row affected (0.00 sec)

    mysql> show create database xiaohong;
    +----------+---------------------------------------------------------------------+
    | Database | Create Database                                                     |
    +----------+---------------------------------------------------------------------+
    | xiaohong | CREATE DATABASE `xiaohong` /*!40100 DEFAULT CHARACTER SET latin1 */ |
    +----------+---------------------------------------------------------------------+
    1 row in set (0.00 sec)

    mysql> show variables LIKE 'character%';
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.12 sec)

    mysql>

    mysql 创建表之常用数据类型

    (1) 数据类型是什么?

    数据类型是指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型。有一些数据是要存储为数字的,数字当中有些是要存储为整数、小数、日期型等

    (2)mysql常见数据类型

    (1) 整数型               

    <1> 整数型
    类型大小范围(有符号)范围(无符号unsigned)用途
    TINYINT1字节(-128,127)(0, 255)小整数值
    SMALLINT2字节(-32768,32767)(0,65535)大整数值
    MEDIUMINT3字节(-8388608,8388607)(0,4294967295)大整数值
    INT4字节(-2147483648,2147483647)(0,4294967295)大整数值
    BIGINT8字节

    (-92223372036854775808,

    9223372036854775807)

    (0, 18446744073709551615)大整数值

    (2) 浮点型

    浮点型
    FLOAT(m,d)4字节单精度浮点型备注:m代表总个数,d代表小数位
    DOUBLE(m,d)8字节双精度浮点型备注:m代表总个数,d代表小数位

    (3) 定点型

    (3) 定点型
    DECIMAL(m,d)依赖于M和D的值备注:m代表总个数,d代表小数位

    (4)字符串型

    (4)字符串类型
    类型大小用途
    CHAR0-255字节定长字符串
    VARCHAR0-65535字节变长字符串
    TINYTEXT0-255字节短文本字符串
    TEXT0-65535字节长文本数据
    MEDIUMTEXT0-16777215字节

    中等长度文本数据

    LONGTEXT0-4294967295字节极大文本数据

    (5)时间类型

    (5)时间类型(需要用单引号引起来)
    数据类型字节数格式备注
    date3yyyy-MM-dd存储日期值
    time3HH:mm:SS存储时分秒
    year1yyyy存储年
    datetime8yyyy-MM-dd HH:mm:SS存储日期+时间
    timestamp4yyyy-MM-dd HH:mm:SS存储日期+时间,可作时间戳

    mysql数据库必备知识值创建表

    语法:

    1. CREATE TABLE table_name(
    2. column1 type1 CONSTRAINT1 description1,
    3. column2 type2 CONSTRAINT2 description2,
    4. column3 type3 CONSTRAINT3 description3,
    5. );
    6. CONSTRAINT CONDITION :
    7. comment --- description
    8. NOT NULL -- NOT NULL
    9. DEFAULT -- DEFAULT value
    10. unsigned -- unsigned (int)
    11. auto_increment -- increase automatically
    12. zerofill --- fillment automatically
    13. UNIQUE KEY --- UNIQUE value

    mysql数据库必备知识之查看

    查看数据库中的所有表 : show tables;

    查看表结构: DESC table_name;

    查看创建表的sql语句:
    show CREATE TABLE table_name;
    \G : 有结束sql语句的作用,也有显示数据纵向旋转90度
    \g : 有结束sql语句的作用

    1. mysql> show databases;
    2. +--------------------+
    3. | Database |
    4. +--------------------+
    5. | information_schema |
    6. | db3 |
    7. | mysql |
    8. | performance_schema |
    9. | sys |
    10. | xiaohong |
    11. | xiaoming |
    12. +--------------------+
    13. 7 rows in set (0.00 sec)
    14. mysql> use xiaohong
    15. Reading table information for completion of table and column names
    16. You can turn off this feature to get a quicker startup with -A
    17. Database changed
    18. mysql> show tables;
    19. +--------------------+
    20. | Tables_in_xiaohong |
    21. +--------------------+
    22. | student |
    23. +--------------------+
    24. 1 row in set (0.00 sec)
    25. mysql> desc student;
    26. +-------+-------------+------+-----+---------+-------+
    27. | Field | Type | Null | Key | Default | Extra |
    28. +-------+-------------+------+-----+---------+-------+
    29. | id | tinyint(4) | NO | | NULL | |
    30. | name | varchar(20) | NO | | NULL | |
    31. | age | tinyint(4) | NO | | NULL | |
    32. | class | varchar(20) | NO | | NULL | |
    33. | sex | char(5) | NO | | NULL | |
    34. +-------+-------------+------+-----+---------+-------+
    35. 5 rows in set (0.03 sec)
    36. mysql> show create table student\G
    37. *************************** 1. row ***************************
    38. Table: student
    39. Create Table: CREATE TABLE `student` (
    40. `id` tinyint(4) NOT NULL COMMENT 'student number',
    41. `name` varchar(20) NOT NULL COMMENT 'student name',
    42. `age` tinyint(4) NOT NULL COMMENT 'student age',
    43. `class` varchar(20) NOT NULL COMMENT 'student class',
    44. `sex` char(5) NOT NULL COMMENT 'student sex'
    45. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    46. 1 row in set (0.00 sec)
    47. mysql> CREATE TABLE teacher AS SELECT * FROM student WHERE 1=2;
    48. Query OK, 0 rows affected (0.16 sec)
    49. Records: 0 Duplicates: 0 Warnings: 0
    50. mysql> show tables;
    51. +--------------------+
    52. | Tables_in_xiaohong |
    53. +--------------------+
    54. | student |
    55. | teacher |
    56. +--------------------+
    57. 2 rows in set (0.01 sec)
    58. mysql> desc teacher;
    59. +-------+-------------+------+-----+---------+-------+
    60. | Field | Type | Null | Key | Default | Extra |
    61. +-------+-------------+------+-----+---------+-------+
    62. | id | tinyint(4) | NO | | NULL | |
    63. | name | varchar(20) | NO | | NULL | |
    64. | age | tinyint(4) | NO | | NULL | |
    65. | class | varchar(20) | NO | | NULL | |
    66. | sex | char(5) | NO | | NULL | |
    67. +-------+-------------+------+-----+---------+-------+
    68. 5 rows in set (0.00 sec)
    69. mysql> show create table teacher\G
    70. *************************** 1. row ***************************
    71. Table: teacher
    72. Create Table: CREATE TABLE `teacher` (
    73. `id` tinyint(4) NOT NULL COMMENT 'student number',
    74. `name` varchar(20) NOT NULL COMMENT 'student name',
    75. `age` tinyint(4) NOT NULL COMMENT 'student age',
    76. `class` varchar(20) NOT NULL COMMENT 'student class',
    77. `sex` char(5) NOT NULL COMMENT 'student sex'
    78. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
    79. 1 row in set (0.00 sec)
    80. mysql>

    mysql数据库必备知识之表结构维护与删除

    1. 1) CHANGE TABLE name
    2. RENAME TABLE old_tab_name TO new_tab_name;
    3. (2) ADD COLUMN
    4. ADD a COLUMN :
    5. ALTER TABLE tab_name ADD column_name TYPE;
    6. ALTER TABLE ADD COLUMN_name TYPE comment 'description';
    7. ADD a COLUMN IN TABLE FIRST:
    8. ALTER TABLE tab_name ADD column_name TYPE FIRST;
    9. ADD a COLUMN IN TABLE LAST:
    10. ALTER TABLE tab_name ADD column_name TYPE AFTER column_name;
    11. (3) MODIFY COLUMN TYPE
    12. ALTER TABLE tab_name MODIFY column_name NEW_type;
    13. (4) MODIFY COLUMN name
    14. ALTER TABLE tab_name CHANGE OLD_column_name new_column_name TYPE;
    15. (5) DELETE COLUMN name
    16. ALTER TABLE tab_name DROP column_name;
    17. (6) MODIFY CHARACTER collection
    18. ALTER TABLE tab_name CHARACTER SET character_collection;
    19. (7) mysql tables delete
    20. drop table tab_name;
    21. DROP TABLE IF EXISTS tab_name;
  • 相关阅读:
    关于Facebook营销的十个常见问题,一次性讲清楚!
    golang给函数参数设置默认值的几种方式(函数参数默认值)
    DeepExtrema: A Deep Learning Approach for Forecasting Block Maximain Time Series Data
    Vuex 和 Redux 的区别?
    fastapi-Depends
    SpringBoot框架
    如何在docker中运行windows
    4、Maven命令构建工程
    架构与思维:了解Http 和 Https的区别(图文详解)
    Pinia使用笔记
  • 原文地址:https://blog.csdn.net/u011868279/article/details/127406584