一、MySQL啟動與停止
1.啟動MySQL服務(wù)
net start mysql

?2.停止MySQL服務(wù)
net stop mysql

?二、登錄MySQL
1.命令行MySQL的路徑
先跳到mysql放置的盤中,比如我的放在d盤,所以我要先輸入d:

cd mysql-8.0.25-winx64\bin或者如下圖,分兩次輸入也可以

2.登錄MySQL數(shù)據(jù)庫
mysql -u root -p

三、數(shù)據(jù)庫操作
1>.創(chuàng)建數(shù)據(jù)庫
1.創(chuàng)建數(shù)據(jù)庫test_db
CREATE DATABASE test_db

2.創(chuàng)建數(shù)據(jù)庫如果不存在
CREATE DATABASE IF NOT EXISTS test_db

3.創(chuàng)建數(shù)據(jù)庫時指定字符集和校對規(guī)則
CREATE DATABASE test_db
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_0900_ai_ci

如果不確定字符集有哪些,可以用show variables like '%character%'來查看

如果不確定校驗規(guī)則有哪些,可使用show variables like ‘collation%’來查看

2>.查看數(shù)據(jù)庫
1.查看所有數(shù)據(jù)庫
SHOW DATABASES

2.查看名為test_db的數(shù)據(jù)庫
SHOW DATABASES LIKE 'test_db'

3.查看名字中包含test的數(shù)據(jù)庫
SHOW DATABASES LIKE '%test%'

4.查看名字以test開頭的數(shù)據(jù)庫
SHOW DATABASES LIKE 'test%'

5.查看名字以db結(jié)尾的數(shù)據(jù)庫
SHOW DATABASES LIKE '%db'

3>.修改數(shù)據(jù)庫
1.修改數(shù)據(jù)庫
ALTER DATABASE test_db
DEFAULT CHARACTER SET gb2312
DEFAULT COLLATE gb2312_chinese_ci

4>.刪除數(shù)據(jù)庫
1.刪除數(shù)據(jù)庫
DROP DATABASE test_db

2.如果有test_db數(shù)據(jù)庫,則刪除
DROP DATABASE IF EXISTS test_db

5>.選擇數(shù)據(jù)庫
1.指定數(shù)據(jù)庫
USE test_db

6>.MySQL注釋
1.單行注釋
#注釋內(nèi)容
或
-- 注釋內(nèi)容(注意內(nèi)容前的空格)
2.多行注釋
/*
第一行
第二行
*/
7>.大小寫規(guī)范
mysql語句的關(guān)鍵字和函數(shù)名沒有大小寫之分,select和SELECT和SeLect都是一樣的,
但是表的別名、字段名都是區(qū)分大小寫的,操作數(shù)據(jù)庫過程中要注意字段名、表名等是否一致,
8>.備份數(shù)據(jù)庫
mysqldump -u root -p 123456 db > db.sql
mysqldump -u root -p 123456 -A > all.sql
9>.恢復(fù)數(shù)據(jù)
mysql -u root -p 123456 db < db.sql
四、用戶管理:
1.新建用戶:
CREATE user name
2.更改密碼:
set password for name=111111
五、數(shù)據(jù)表基本操作
1>.創(chuàng)建數(shù)據(jù)表
/*
字段名:id 數(shù)據(jù)類型:int(11) 備注:員工編號
字段名:name 數(shù)據(jù)類型:varchar(25) 備注:員工名稱
字段名:deptid 數(shù)據(jù)類型:int(11) 備注:部門編號
字段名:salary 數(shù)據(jù)類型:float 備注:工資
*/
CREATE TABLE table1
(
id int(11), -- 創(chuàng)建字段id,int類型,限制11位數(shù)
name VARCHAR(25),
phone int(11),
age int,
sex varchar(25),
coid int(11),
coname varchar(25)
)

2>.增加字段
1.增加列g(shù)rade,類型VARCHAR(25)
ALTER TABLE student ADD grade VARCHAR(25)

2.在student表的第一列增加stuId字段
ALTER TABLE student ADD stuId INT(4) FIRST

3.在name字段后添加stuno字段
ALTER TABLE student ADD stuno INT(11) AFTER name

3>.修改數(shù)據(jù)表
1.修改表名
ALTER TABLE student RENAME TO stu

2.修改表中字段名deptid改為deid
ALTER TABLE tb1 CHANGE deptid deid int(8)

3.修改字段數(shù)據(jù)類型
ALTER TABLE stu MODIFY sex varchar(8)

5.刪除字段
ALTER TABLE stu DROP coname

6.修改表字符集
ALTER TABLE tb1 CHARACTER SET gb2312 DEFAULT COLLATE gb2312_chinese_ci

4>.刪除數(shù)據(jù)表
1.刪除數(shù)據(jù)表
DROP TABLE stu

2.刪除關(guān)聯(lián)的表
①.創(chuàng)建關(guān)聯(lián)表
create table stu
(
id int(11) primary key,
name varchar(22),
sex varchar(8),
age int,
phone int(11),
coid int(4),
index(coid)
);
create table course
(
id int(4) primary key,
name varchar(25),
teacher varchar(22),
constraint id foreign key (id) references stu(coid)
);

②.解除tb_2的外鍵約束
ALTER TABLE course DROP FOREIGN KEY id

③.解除關(guān)聯(lián)關(guān)系后,就可以使用drop table 語句刪除stu
DROP TABLE stu
5>.查看表結(jié)構(gòu)
1.以表格形式展示表的字段信息
DESC student

2.以sql語句形式展示表結(jié)構(gòu)
SHOW CREATE TABLE student

六、常用增刪查改
1>.增加表中數(shù)據(jù)
1.增加數(shù)據(jù)包含表中全部字段內(nèi)容
insert into stu values (1,'hjk','男',23,11111,1000)

2.增加數(shù)據(jù)包含表中部分字段數(shù)據(jù)
INSERT INTO student (name,sex,age,phone) values ('jh','男',23,10000)

2>.查找表中數(shù)據(jù)
1.無條件的查找表中數(shù)據(jù)
select * from student

2.查找表中部分?jǐn)?shù)據(jù)
select name,phone from student

3.查詢表中第一到三條數(shù)據(jù)
select * from stu limit 1,3(第一條數(shù)據(jù)是從0開始算的,所以limit后的1實際是第二條)

4.單條件的查找表中數(shù)據(jù)
select * from stu where id=1

5.多條件查詢表中數(shù)據(jù)
select * from student where name='' and sex='男'

6.模糊查詢名字中含j的學(xué)生信息
select * from student where name like '%j%'

7.模糊查詢學(xué)生姓名為h開頭的兩字名 -- 下劃線_表示任意一個單字符,h后面只有一個字
select * from stu where name like 'h_'

8.給表起別名,as可省略
select a.name as '學(xué)生姓名',a.phone '學(xué)生電話' from stu as a where a.id=1

9.查找名字在這列表中的學(xué)生信息
select * from stu where name in ('hj','hjk','jk')

10.查找年齡在23-24之間的學(xué)生信息
select * from stu where age between 23 and 24

11.查找到的信息根據(jù)id升序,age降序
select * from stu order by id asc,money desc

12.去重查找(此前特意增加一條數(shù)據(jù))
select distinct a.name from student a

13.統(tǒng)計符合要求的人數(shù)
select count(*) from stu where age>10

14.查找最大值
select max(age) from stu

15.查找最小值
select min(age) from stu

16.查找平均值
select avg(age) from stu

17.查詢結(jié)果分組? --GROUP_CONCAT() 函數(shù)會把每個分組的字段值都顯示出來。
select age,sex,group_concat(name) from stu group by age,sex

18.多條件查找
select * from stu where age=23 and sex='nan'

19.多個字段進行分組
select * from stu group by sex,age? ? -- group by后還有條件必須要用having子句

20.合并兩條select語句的結(jié)果,字段數(shù)和數(shù)據(jù)類型都要一致
select name,phone from stu union select name,phone from teacher -- 還有union all,它和union的區(qū)別在于它不會去重

21.兩表關(guān)聯(lián)無條件查詢
select * from stu s, course c where s.coid=c.id

22.兩表關(guān)聯(lián)含條件查詢
select * from stu s, course c where
s.coid = c.id
and s.age = 23

23.左連接查詢
select * from stu s left join course c on s.coid=c.id

24.右連接查詢
select * from stu s right join course c on s.coid=c.id

25.內(nèi)連接查詢
select * from stu s inner join course c on s.coid=c.id

26.子查詢,把子查詢的結(jié)果當(dāng)做另一條sql的條件
select * from course c where c.id = (select coid from stu where name='hjk')

27.子查詢,把子查詢的結(jié)果當(dāng)做一個表
select c.teacher,m.name,m.id from course c,(select name,id,coid from stu where sex='nan')as m where c.id=m.coid
