本篇是在已下载Mysql的情况下进行的,若还未下载或未创建Mysql服务,请转到这篇:
2024 年 MySQL 8.0.40 安装配置、Workbench汉化教程最简易(保姆级)_mysql8.0.40下载安装教程-CSDN博客
本文对于mysql的操作均使用控制台sql原生代码编写
目录
一、MySQL背景介绍
MySQL 是一种广泛使用的关系型数据库管理系统(RDBMS),以其高性能、可靠性和易用性而闻名。作为开源软件,MySQL 遵循 GNU 通用公共许可证(GPL),提供免费和商业版本,适用于各种规模的应用程序开发。它支持多种操作系统,如 Windows、Linux 和 macOS,具有跨平台的灵活性。MySQL 的核心优势在于其高效的查询处理能力,特别适合高并发读写操作,并且内置了丰富的安全特性,如用户权限管理和 SSL 支持,确保数据传输的安全性。此外,MySQL 提供多种存储引擎选择,如 InnoDB 和 MyISAM,以满足不同的应用场景需求。它的广泛应用涵盖了从简单的网站到复杂的企业级应用,包括内容管理系统(CMS)、电子商务平台和大数据分析等。活跃的社区支持和丰富的生态系统使得 MySQL 成为开发者首选的数据库解决方案之一。Oracle 公司自2010年起拥有 MySQL,但其开源版本继续受到全球开发者的青睐和支持。
二、Mysql基本操作
1、运行Mysql
方法一:
直接软件包中自带登录客户端

输入之前下载mysql时输入的密码,点击Enter(设置密码可以看之前这篇:
2024 年 MySQL 8.0.40 安装配置、Workbench汉化教程最简易(保姆级)_mysql8.0.40下载安装教程-CSDN博客)

最左边出现mysql>即代表已进入mysql
方法二:
在cmd终端中进入到mysql文件中的bin目录
依次输入命令:
D:
cd D:load_softwareMysql8.0MySQL Server 8.0Installin
如下图:

输入mysql的进入命令
mysql -uroot -p
这里的p后面加root代表显示输入密码,若不加则默认隐藏密码
输入密码后成功进入页面如下:

注意!在mysql>目录下,输入的语句必须以";"结尾才可运行!
2、修改密码
在mysql>下输入:
ALTER USER 'root'@'localhost' IDENTIFIED BY '718718';
若是在其他设备和端口,则修改root和localhost即可。
8.0以上版本的直接输入下面语句即可:
set password = '';
3、显示、创建和删除数据库
显示
在mysql>目录下输入
show databases;
可以显示目前该数据库服务上所安装的数据库文件,当然,大家的页面和我肯定不一样:

创建数据库
在mysql目录下输入:
create database test default character set utf8;
(default character set设置默认编码关键字,test是数据库名称)
再次显示数据库,发现test数据库已加入:

删除数据库
输入以下命令即可(test替换成自己要删除的数据库名称):
drop database test;
三、数据库的基本语句
数据库,我的理解是在一个文件夹中放了很多excel表
首先,进入到一个数据库中:
use test;

查看该数据库中的所有数据表:
show tables;
可以看到现在还是空的

1、基础数据类型
整数:

小数:

decimal是精确小数,double和float和decimal用法相似
字符串:



时间类型:

2、创建表:

设置主键一般为非空,并且可以自增(默认也是非空,可以不写not null)
MySQL还有很多其他的数据类型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob等,详细见官方文档:
简单的创建:
create table tb1(id int not null primary key auto_increment,name varchar(64))DEFAULT CHARSET=utf8;
这里的primary key auto_increment是设置主键,并且自动加一

这样创建可视性不高,也可以分行,只要不加";"就不会运行:
create table tb2(
age int,
salary tinyint
)DEFAULT CHARSET=utf8;

查看数据库下创建的表:
show tables;

查看表属性:
desc tb1;
(这里的desc全称是descript)

查看表内容:
select * from tb1;
(*号表示全部)
现在还是空表

添加内容
insert into 表名(列名,...) values(值,...);

也可以一次存入多组数据
insert into 表名(列名,...) values(值,...),(值,...),(值,...)...;
显示:

由于前面设置了主键为auto_increment,序号会自动加一
查看表属性:
desc 表名

删除表:
drop table 表名;

3、表内的增删改查
增加数据
#在表中为某两列插入一行数据
insert into 表名(列名,列名)values(值,值);
#在表中为某几列插入任意行数据
insert into 表名(列名,列名...)values(值,值...),(值,值...),(值,值...),
(值,值...)...;
删除数据
#删除整张表
delete from 表名;
删除表中满足条件的表
delete from 表名 where 条件;
#例子
delete from tb2;
delete from tb2 where id=1;
delete from tb2 where id =1 and name="谭sir";
delete from tb2 where id =2 or name="林sir";
delete from tb2 where id>1;
delete from tb2 where id >= 2;
delete from tb2 where id != 3;
delete from tb2 where id in(l,3);
修改数据
#更新某一列为某值
update 表名 set 列=值;
#更新几列的值
update 表名 set 列=值,列=值...;
#更新满足
update 表名 set 列=值 where 条件;
#例子
update tb2 set password="哈哈哈";
update tb2 set email="哈哈哈"where id >1;
update tb2 set age=age+10 where id>2;
查找数据
#查询整张表
select *from 表名称;
#查询满足条件的列
select 列名称,列名称 from 表名称;
select 列名称,列名称 from 表名称 where 条件;
#例子
select *from tb2;
select id,name from tb2;
select id,name from tb2 where id>10;
select id,name from tb2 where name="xx" and password="xx";
小结
由上文很容易看出,创建数据库和数据表结构,使用原生代码很容易实现,需要提前通过工具+命令创建。
但是,为表中添加数据,若数据量很大时,一行一行添加很不现实,这种情况下一般使用程序来实现增删改查。
四、Mysql综合实例
综合使用所有数据类型创建一个表
创建表:
create table tb2(
id int not null primary key auto_increment,
name varchar(64) not null,
password char(64) not null,
email varchar(64) not null,
age tinyint,
salary decimal(10,2),
ctime datetime
)DEFAULT CHARSET=utf8;

插入数据
为该表插入一行内容并显示:
insert into tb2(name,password,email,age,salary,ctime) values("谭sir","123","122685@qq.com",19,100000.20,"2024-12-20 10:02:02");
select * from tb2;

为表中添加多行数据并显示:
#插入数据
insert into tb2(name,password,email,age,salary,ctime)
values("江sir","1203","122685@qq.com",22,0.23,"2024-12-20 10:02:03"),
("林sir","12003","12285@qq.com",19,234.20,"2024-12-20 10:02:03"),
("徐sir","1230","1285@qq.com",18,12345.20,"2024-12-20 10:02:03"),
("张sir","1023","12685@qq.com",18,23456.20,"2024-12-20 10:02:03"),
("叶sir","1203","12265@qq.com",19,34567.20,"2024-12-20 10:02:03");
#显示
select * from tb2;

删除指定数据
(这里好不容易添加了数据,我就少删点)
#删除id为3的数据
delete from tb2 where id=3;
#显示
select * from tb2;

修改数据
修改一整列
#更新密码
update tb2 set password="12@qq.com"
#显示
select * from tb2;

带条件修改
#更新id大于1的
update tb2 set email='哈哈哈' where id>1;
#更新id大于2的年龄加一
update tb2 set age=age+1 where id>2;

查找数据
查找某两列
select id,name from tb2;

查找满足条件的数据
select id,name,password from tb2 where name="谭sir" and age>18;

本文基于控制台命令,命令繁琐,可视化不太方便,市面上也有一些控制MySQL的软件,后面可能会出新文介绍。还有Python程序存数据进库,也会更新,大家静待。。。
感谢您的三连!!!