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;
Query OK, 1 row affected ( 0.04 sec)
如何删除数据库
mysql> drop database test;
Query OK, 0 rows affected ( 0.03 sec)
如何选择数据库
mysql> use school;
Database changed
如何查看当前数据库有哪些表
mysql> show tables;
+ - - - - - - - - - - - - - - - - - - +
| Tables_in_school |
+ - - - - - - - - - - - - - - - - - - +
| courses |
| scores |
| students |
| teachers |
+ - - - - - - - - - - - - - - - - - - +
4 rows in set ( 0.00 sec)
如何查看表的描述信息
mysql> desc 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;
mysql 常见数据类型
数据类型一共有四种,整型 , 浮点型, 字符串, 日期 。每个种类有不同长度或者格式类型。
整型
类型
大小
范围 (有符号)
无符号
适用
INYINT
1 byte
(-128,127)
(0,255)
小整数值
SMALLINT
2 bytes
(-32 768,32 767)
(0,65 535)
大整数值
MEDIUMINT
3 bytes
(-8 388 608,8 388 607)
(0,16 777 215)
大整数值
INT
4 bytes
(-2 147 483 648,2 147 483 647)
(0,4 294 967 295)
较大整数值
BIGINT
8 bytes
(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)
(0,18 446 744 073 709 551 615)
极大整数值
浮点型
类型
大小
范围 (有符号)
无符号
适用
FLOAT
4 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)
单精度浮点数值
DOUBLE
8 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的值
小数
字符串
类型
大小
适用
CHAR
0-255 bytes
定长字符串
VARCHAR
0-65535 bytes
变长字符串
TINYBLOB
0-255 bytes
不超过 255 个字符的二进制字符串
TINYTEXT
0-255 bytes
短文本字符串
BLOB
0-65 535 bytes
二进制形式的长文本数据
TEXT
0-65 535 bytes
长文本数据
MEDIUMBLOB
0-16 777 215 bytes
二进制形式的中等长度文本数据
MEDIUMTEXT
0-16 777 215 bytes
中等长度文本数据
LONGBLOB
0-4 294 967 295 bytes
二进制形式的极大文本数据
LONGTEXT
0-4 294 967 295 bytes
极大文本数据
日期与时间类型
类型
大小
范围
格式
适用
DATE
3 byte
1000-01-01/9999-12-31
YYYY-MM-DD
日期值
TIME
3 bytes
‘-838:59:59’/‘838:59:59’
HH:MM:SS
时间值或持续时间
YEAR
1 bytes
1901/2155
YYYY
年份值
DATETIME
8 bytes
1000-01-01 00:00:00/9999-12-31 23:59:59
YYYY-MM-DD HH:MM:SS
混合日期和时间值
TIMESTAMP
4 bytes
1970-01-01 00:00:00/2038
YYYY-MM-DD HH:MM:SS
混合日期和时间值
具体用哪一种根据数据库的需求选择 ,长度越大,占内存越多,所以尽量选择适合的类型。 数据类型参考 : https://www.runoob.com/mysql/mysql-data-types.html