mysql终端基本操作与数据类型

    技术2022-07-10  119

    mysql终端基本操作与数据类型笔记 ,mysql框架 : 服务器——数据库——表——字段 。 我使用的mysql80, 直接打开mysql8.0 Command Line Client, 输入密码即可登录服务器

    Enter password: ****** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 18 Server version: 8.0.11 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | school | | sys | +--------------------+ 5 rows in set (0.00 sec)

    如何新建数据库

    mysql> create database test; # 新建名为 test 的数据库 Query OK, 1 row affected (0.04 sec)

    如何删除数据库

    mysql> drop database test; # 删除名为 test 的数据库 Query OK, 0 rows affected (0.03 sec)

    如何选择数据库

    mysql> use school; # 选择名为school 的数据库 Database changed

    如何查看当前数据库有哪些表

    mysql> show tables; +------------------+ | Tables_in_school | +------------------+ | courses | | scores | | students | | teachers | +------------------+ 4 rows in set (0.00 sec)

    如何查看表的描述信息

    mysql> desc students; # 或者describe students; +-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | sid | int(11) | NO | PRI | NULL | | | sname | varchar(10) | NO | | NULL | | | sgender | varchar(10) | NO | | NULL | | | sbrithday | date | NO | | NULL | | | sclass | int(11) | NO | | NULL | | +-----------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)

    如何查看数据具体信息

    mysql> select * from students; +-----+------------+---------+------------+--------+ | sid | sname | sgender | sbrithday | sclass | +-----+------------+---------+------------+--------+ | 1 | zhangsan | nan | 1995-10-10 | 601 | | 2 | lisi | nv | 1995-10-10 | 601 | | 3 | wangermazi | nan | 1995-04-10 | 602 | | 4 | huangwu | nan | 1995-10-10 | 602 | | 5 | zhaoliu | nv | 1995-05-10 | 603 | | 6 | zhaoliu | nv | 1995-03-10 | 603 | +-----+------------+---------+------------+--------+ 6 rows in set (0.00 sec)

    如何退出数据库服务器

    exit; # 或者 quit;

    mysql 常见数据类型

    数据类型一共有四种,整型 , 浮点型, 字符串, 日期 。每个种类有不同长度或者格式类型。

    整型
    类型大小范围 (有符号)无符号适用INYINT1 byte(-128,127)(0,255)小整数值SMALLINT2 bytes(-32 768,32 767)(0,65 535)大整数值MEDIUMINT3 bytes(-8 388 608,8 388 607)(0,16 777 215)大整数值INT4 bytes(-2 147 483 648,2 147 483 647)(0,4 294 967 295)较大整数值BIGINT8 bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)极大整数值
    浮点型
    类型大小范围 (有符号)无符号适用FLOAT4 bytes(-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)0,(1.175 494 351 E-38,3.402 823 466 E+38)单精度浮点数值DOUBLE8 bytes(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)双精度浮点数值DECIMAL对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2依赖于M和D的值依赖于M和D的值小数
    字符串
    类型大小适用CHAR0-255 bytes定长字符串VARCHAR0-65535 bytes变长字符串TINYBLOB0-255 bytes不超过 255 个字符的二进制字符串TINYTEXT0-255 bytes短文本字符串BLOB0-65 535 bytes二进制形式的长文本数据TEXT0-65 535 bytes长文本数据MEDIUMBLOB0-16 777 215 bytes二进制形式的中等长度文本数据MEDIUMTEXT0-16 777 215 bytes中等长度文本数据LONGBLOB0-4 294 967 295 bytes二进制形式的极大文本数据LONGTEXT0-4 294 967 295 bytes极大文本数据
    日期与时间类型
    类型大小范围格式适用DATE3 byte1000-01-01/9999-12-31YYYY-MM-DD日期值TIME3 bytes‘-838:59:59’/‘838:59:59’HH:MM:SS时间值或持续时间YEAR1 bytes1901/2155YYYY年份值DATETIME8 bytes1000-01-01 00:00:00/9999-12-31 23:59:59YYYY-MM-DD HH:MM:SS混合日期和时间值TIMESTAMP4 bytes1970-01-01 00:00:00/2038YYYY-MM-DD HH:MM:SS混合日期和时间值

    具体用哪一种根据数据库的需求选择 ,长度越大,占内存越多,所以尽量选择适合的类型。 数据类型参考 : https://www.runoob.com/mysql/mysql-data-types.html

    Processed: 0.012, SQL: 9