全网最牛最全最常用MySQL命令的使用

一,启动数据库命令:net start mysqlC:\Windows\system32>net start mysqlMySQL 服务正在启动 .MySQL 服务已经启动成功。二,链接数据库命令1、第一种链接方式:mysql -u root -p 0620C:\Windows\system32>mysql -uroot -p0620mysql: [Warning] Using a password on the command line interface can be ins
摘要由CSDN通过智能技术生成

在这里插入图片描述

一:启动、退出数据库

1、启动数据库

net start mysql
C:\Windows\system32>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。

2、关闭数据库

net stop mysql
C:\Windows\system32>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。

二:链接数据库命令

1、第一种链接方式

登录:

mysql -u<用户名> -p<密码>

用户名加密码【-u登录用户名、-p登录密码】

C:\Windows\system32>mysql -uroot -p0620
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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.

2、第二种链接方式

登录:

mysql -u <用户名> -p

先输入用户名,再输入密码

C:\Windows\system32>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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.

三:数据库的创建及删除

1、创建数据库命令

create database <自定义的库名>;
mysql> create database test;
Query OK, 1 row affected (0.02 sec)

2、展示已有的数据库命令

show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sqtest             |
| sys                |
| test               |
+--------------------+
6 rows in set (0.00 sec)

3、查看数据库定义

show create database test;
mysql> show create database test;
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                                                |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

4、选中数据库命令

use test;
mysql> use test;
Database changed

5、删除数据库

drop database <库名>;
mysql> drop database sqtest;
Query OK, 0 rows affected (0.07 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

四:创建表信息


1、字段约束

约束 解释 命令
主键约束 作为表记录中的唯一标识 primary key
自增约束 每次新增数据时+1,通常用于主键 auto_increment
外键约束 用于标识两个表之间建立的关系【foreign key(本表外键字段名) references 关联的表名(关联的表名中的字段名) 】 foreign key
唯一约束 字段的值是唯一的,可以为空,但只能有一个为空 unique
非空约束 字段不允许为空 not null
默认约束 设置默认值 default
字段注释 注释、进行字段解释:comment ‘字段的注释’ comment
编码格式 charset=utf8mb4 charset

2、数据类型

数值类型

类型 大小 范围(有符号) 范围(无符号) 用途
tinyint 1 Bytes (-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或integer 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的值 小数值

日期和时间类型

类型 大小( bytes) 范围 格式 用途
date 3 1000-01-01/9999-12-31 yyyy-mm-dd 日期值
time 3 ‘-838:59:59’/‘838:59:59’ hh:mm:ss 时间值或持续时间
year 1 1901/2155 yyyy 年份值
datetime 8 1000-01-01 00:00:00/9999-12-31 23:59:59 yyyy-mm-dd hh:mm:ss 混合日期和时间值
timestamp 4 1970-01-01 00:00:00/2038结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 yyyymmdd hhmmss 混合日期和时间值,时间戳

字符串类型

类型 大小 用途
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 极大文本数据

二进制类型

类型名称 说明 存储需求
bit(m) 位字段类型 大约 (m+7)/8 字节
binary(m) 固定长度二进制字符串 m 字节
varbinary (m) 可变长度二进制字符串 m+1 字节
tinyblob (m) 非常小的blob l+1 字节,在此,l<2^8,最大长度为255 (28-1)字节
blob (m) 小 blob l+2 字节,在此,l<2^16,最大长度为65535 (216-1)字节
mediumblob (m) 中等大小的blob l+3 字节,在此,l<2^24,最大长度为16777215 (224-1)字节
longblob (m) 非常大的blob l+4 字节,在此,l<2^32,最大长度为4294967295或4gb (231-1)字节

转义字符的使用

转义字符 转义后的字符
\" 双引号(")
\’ 单引号(')
\|反斜线(\)
\n 换行符
\r 回车符
\t 制表符
\0 ASCII 0(NUL)
\b 退格符

3、创建、显示、删除表

3.1 创建表信息

创建楼

create table tower(
ter_id integer not null primary key auto_increment comment'主键,自增非空约束',
ter_name varchar(50) not null unique comment'楼名称,唯一非空约束',
ter_location varchar(200)  not null unique comment'楼位置,唯一非空约束'
)charset=utf8mb4;
mysql> create table tower(
    -> ter_id integer not null primary key auto_increment comment'主键,自增非空约束',
    -> ter_name varchar(50) not null unique comment'楼名称,唯一非空约束',
    -> ter_location varchar(200)  not null unique comment'楼位置,唯一非空约束'
    -> )charset=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

创建班级

create table class(
cla_id integer not null primary key auto_increment comment'主键,自增非空约束',
cla_name varchar(50) not null unique comment'班级名称,唯一非空约束',
cla_terid integer not null comment'教学楼名称,唯一非空约束',
cla_location varchar(100)  not null comment'班级位置,非空约束,例如二楼201室教室',
cla_number integer not null comment'班级所容纳的人数限制,非空约束',
foreign key(cla_terid) references tower(ter_id)
)charset=utf8mb4;
mysql> create table class(
    -> cla_id integer not null primary key auto_increment comment'主键,自增非空约束',
    -> cla_name varchar(50) not null unique comment'班级名称,唯一非空约束',
    -> cla_terid integer not null comment'教学楼名称,唯一非空约束',
    -> cla_location varchar(100)  not null comment'班级位置,非空约束,例如二楼201室教室',
    -> cla_number integer not null comment'班级所容纳的人数限制,非空约束',
    -> foreign key(cla_terid) references tower(ter_id)
    -> )charset=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

创建职位表

create table jobtitle(
jbt_id integer not null primary key auto_increment comment'主键,自增非空约束',
jbt_name varchar(20) not null unique comment'职位名称,唯一非空约束'
)charset=utf8mb4;
mysql> create table jobtitle(
    -> jbt_id integer not null primary key auto_increment comment'主键,自增非空约束',
    -> jbt_name varchar(20) not null unique comment'职位名称,唯一非空约束'
    -> )charset=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

创建宿舍表

create table dormitory(
drt_id integer not null primary key auto_increment comment'主键,自增非空约束',
drt_name varchar(50) not null unique comment'宿舍名称,唯一非空约束',
drt_terid integer not null comment'宿舍楼名称,唯一非空约束',
drt_location varchar(100)  not null unique comment'宿舍位置,唯一非空约束,例如二楼201室宿舍',
drt_number integer not null comment'宿舍所容纳的人数限制,非空约束',
foreign key(drt_terid) references tower(ter_id)
)charset=utf8mb4;
mysql> create table dormitory(
    -> drt_id integer not null primary key auto_increment comment'主键,自增非空约束',
    -> drt_name varchar(50) not null unique comment'宿舍名称,唯一非空约束',
    -> drt_terid integer not null comment'宿舍楼名称,唯一非空约束',
    -> drt_location varchar(100)  not null unique comment'宿舍位置,唯一非空约束,例如二楼201室宿舍',
    -> drt_number integer not null comment'宿舍所容纳的人数限制,非空约束',
    -> foreign key(drt_terid) references tower(ter_id)
    -> )charset=utf8mb4;
Query OK, 0 rows affected (0.03 sec)

创建人员表

create table people(
pel_id integer not null primary key auto_increment comment'主键,自增非空约束',
pel_name varchar(50) not null comment&#
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

trysoharder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值