# MySQL
卸載
? 安裝版
? ? 電腦管家/360/控制面板卸載mysql服務即可
? ? 刪除ProgramData中的MySQL目錄
? 解壓版
? win+r 輸入 services.msc 打開服務管理。查看是否存在MySQL,如果存在則刪除
? 注冊表 ?win+R ?regedit ?打開注冊表
? ? 計算機\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services 查看是否存在MySQL目錄
安裝
? 安裝版安裝
? ? 雙擊安裝包
? ?? ? 安裝比較傻瓜式,下一步,執行即可
? ? 如果服務不能成功開啟,則需要進行下述操作
? ? ? win+R ?services.msc
? ? ? 找打MySQL服務,右鍵編輯屬性
? ? ? 啟動服務,發現啟動成功
? 配置MySQL
? ? 環境變量。因為需要使用命令行操作MySQL
? 解壓版的安裝
? ? 下載地址:https://downloads.mysql.com/archives/community/
? ? 解壓的路徑不要存在中文和空格
? ? 創建my.ini文件 ?mysql的核心配置文件
? ? 創建data文件夾
? ? 在bin目錄中 cmd ?執行 mysqld --initialize-insecure 初始化數據庫,且會把root用戶的初始化密碼設置為空
? ? mysqld install
? ? net start MySQL 啟動mysql服務
在dos中進入mysql
mysql -u用戶名 -p密碼
查看當前數據庫系統的所有數據庫
? show databases;
使用數據庫
? use dbName;
查看當前庫中有那些表
? show tables
查看字符集
? show variable like '%char%';
修改字符集
? set 設置項名稱=字符集; 這種修改不是永久修改
? 永久性修改需要修改my.ini文件
修改用戶密碼
??alter user 'root'@"localhost" identified with mysql_native_password by "admin";
創建用戶并授權
? create user '用戶名'@'ip地址 %任意' identified by '密碼';
? grant all on dbName.tableName to '用戶名'@'地址';
? flush privileges;
# 創建數據庫
同一個數據庫軟件中,數據庫的名字不能重復
create database [if not exists] dbName;
create database newsdb;
create database if not exists newsdb;
刪除數據庫
如果數據庫不存在,同樣也不可以刪除
drop database [if exists] dbName;
drop database newsdb;
drop database if exists newsdb;
查看當前數據庫
使用數據庫
use newsdb;
use mysql;
select database();
select 10 / 3;
select 10 div 3;
select not (2 > 3 || 2 < 3);
select 2 = 2 && 1 < 2;
select 2 = 2;
不等于
select 2 != 3;
select 2 <> 3;
-- between and
select 9 between 9 and 11;
select 9 >= 9 && 9 <= 11;
in 分組
select 5 in(1,2,3);
is 一般和null聯合使用
select null is not null;
like 搜索 ?_代表一位字符 % 代表多位
select 'name' like '__a%';
select concat('hello','world')
abs 絕對值
select abs(-100)
余數
select mod(10,3);
隨機數
select rand();
sqrt 平方根
select sqrt(9);
向上取整
select ceil(10.1)
select floor(10.5)
select round(10.5)
select floor(rand() * 500)
判斷數組是等于0 大于0 小于0
select sign(-10.2)
left 從左側截取指定長度
select left("hello",3)
right 從右側截取
select right("hello",3)
substring(字符串,開始位置,截取長度) ?索引從1開始。
select substring("hello",2,3)
ltrim() 去除左側空格
select ltrim(" ???hello")
select length(ltrim(" ???hello"))
select length(trim("hello ???"));
日期 current_date[()] / curdate()
select current_date
select curdate();
時間 current_time[()] / curtime()
select current_time();
select current_time;
select curtime();
日期時間
select current_timestamp()
select now();
select year("2025-09-05")
select month("2025-09-05");
select day("2025-09-05")
select year(now())
select adddate(now(),5)
select adddate(now(),interval 5 year)
select adddate(now(),interval 5 minute);
select date_add(now(),interval 5 year);
select addtime(now(),"00:30");
select datediff("2025-09-05","2025-09-05")
select greatest(12,2,3);
select least(13,3)