公司網(wǎng)站注意事項產(chǎn)品推廣詞
本篇是在已下載Mysql的情況下進行的,若還未下載或未創(chuàng)建Mysql服務,請轉到這篇:
2024 年 MySQL 8.0.40 安裝配置、Workbench漢化教程最簡易(保姆級)_mysql8.0.40下載安裝教程-CSDN博客
本文對于mysql的操作均使用控制臺sql原生代碼編寫
目錄
一、MySQL背景介紹
二、Mysql基本操作
1、運行Mysql
2、修改密碼
3、顯示、創(chuàng)建和刪除數(shù)據(jù)庫
顯示
創(chuàng)建數(shù)據(jù)庫
刪除數(shù)據(jù)庫
三、數(shù)據(jù)庫的基本語句
1、基礎數(shù)據(jù)類型
2、創(chuàng)建表:
簡單的創(chuàng)建:
查看數(shù)據(jù)庫下創(chuàng)建的表:
查看表屬性:
查看表內容:
添加內容
?編輯?
查看表屬性:
刪除表:
3、表內的增刪改查
增加數(shù)據(jù)
刪除數(shù)據(jù)
修改數(shù)據(jù)
查找數(shù)據(jù)
小結
四、Mysql綜合實例
創(chuàng)建表:
插入數(shù)據(jù)
刪除指定數(shù)據(jù)
修改數(shù)據(jù)
查找數(shù)據(jù)
一、MySQL背景介紹
MySQL 是一種廣泛使用的關系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),以其高性能、可靠性和易用性而聞名。作為開源軟件,MySQL 遵循 GNU 通用公共許可證(GPL),提供免費和商業(yè)版本,適用于各種規(guī)模的應用程序開發(fā)。它支持多種操作系統(tǒng),如 Windows、Linux 和 macOS,具有跨平臺的靈活性。MySQL 的核心優(yōu)勢在于其高效的查詢處理能力,特別適合高并發(fā)讀寫操作,并且內置了豐富的安全特性,如用戶權限管理和 SSL 支持,確保數(shù)據(jù)傳輸?shù)陌踩?。此?#xff0c;MySQL 提供多種存儲引擎選擇,如 InnoDB 和 MyISAM,以滿足不同的應用場景需求。它的廣泛應用涵蓋了從簡單的網(wǎng)站到復雜的企業(yè)級應用,包括內容管理系統(tǒng)(CMS)、電子商務平臺和大數(shù)據(jù)分析等?;钴S的社區(qū)支持和豐富的生態(tài)系統(tǒng)使得 MySQL 成為開發(fā)者首選的數(shù)據(jù)庫解決方案之一。Oracle 公司自2010年起擁有 MySQL,但其開源版本繼續(xù)受到全球開發(fā)者的青睞和支持。
二、Mysql基本操作
1、運行Mysql
方法一:
直接軟件包中自帶登錄客戶端
輸入之前下載mysql時輸入的密碼,點擊Enter(設置密碼可以看之前這篇:
2024 年 MySQL 8.0.40 安裝配置、Workbench漢化教程最簡易(保姆級)_mysql8.0.40下載安裝教程-CSDN博客)
最左邊出現(xiàn)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、顯示、創(chuàng)建和刪除數(shù)據(jù)庫
顯示
在mysql>目錄下輸入
show databases;
可以顯示目前該數(shù)據(jù)庫服務上所安裝的數(shù)據(jù)庫文件,當然,大家的頁面和我肯定不一樣:
創(chuàng)建數(shù)據(jù)庫
在mysql目錄下輸入:
create database test default character set utf8;
(default character set設置默認編碼關鍵字,test是數(shù)據(jù)庫名稱)
再次顯示數(shù)據(jù)庫,發(fā)現(xiàn)test數(shù)據(jù)庫已加入:
刪除數(shù)據(jù)庫
輸入以下命令即可(test替換成自己要刪除的數(shù)據(jù)庫名稱):
drop database test;
三、數(shù)據(jù)庫的基本語句
數(shù)據(jù)庫,我的理解是在一個文件夾中放了很多excel表
首先,進入到一個數(shù)據(jù)庫中:
use test;
查看該數(shù)據(jù)庫中的所有數(shù)據(jù)表:
show tables;
可以看到現(xiàn)在還是空的
1、基礎數(shù)據(jù)類型
整數(shù):
小數(shù):
decimal是精確小數(shù),double和float和decimal用法相似
字符串:
時間類型:
2、創(chuàng)建表:
設置主鍵一般為非空,并且可以自增(默認也是非空,可以不寫not null)
MySQL還有很多其他的數(shù)據(jù)類型,例如:set、enum、TinyBlob、Blob、MediumBlob、LongBlob等,詳細見官方文檔:
MySQL :: MySQL 8.4 Reference Manual
簡單的創(chuàng)建:
create table tb1(id int not null primary key auto_increment,name varchar(64))DEFAULT CHARSET=utf8;
這里的primary key auto_increment是設置主鍵,并且自動加一
這樣創(chuàng)建可視性不高,也可以分行,只要不加";"就不會運行:
create table tb2(age int,salary tinyint)DEFAULT CHARSET=utf8;
查看數(shù)據(jù)庫下創(chuàng)建的表:
show tables;
查看表屬性:
desc tb1;
(這里的desc全稱是descript)
查看表內容:
select * from tb1;
(*號表示全部)
現(xiàn)在還是空表
添加內容
insert into 表名(列名,...) values(值,...);
也可以一次存入多組數(shù)據(jù)
insert into 表名(列名,...) values(值,...),(值,...),(值,...)...;
顯示:
由于前面設置了主鍵為auto_increment,序號會自動加一
查看表屬性:
desc 表名
刪除表:
drop table 表名;
3、表內的增刪改查
增加數(shù)據(jù)
#在表中為某兩列插入一行數(shù)據(jù)
insert into 表名(列名,列名)values(值,值);#在表中為某幾列插入任意行數(shù)據(jù)
insert into 表名(列名,列名...)values(值,值...),(值,值...),(值,值...),
(值,值...)...;
刪除數(shù)據(jù)
#刪除整張表
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);
修改數(shù)據(jù)
#更新某一列為某值
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;
查找數(shù)據(jù)
#查詢整張表
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";
小結
由上文很容易看出,創(chuàng)建數(shù)據(jù)庫和數(shù)據(jù)表結構,使用原生代碼很容易實現(xiàn),需要提前通過工具+命令創(chuàng)建。
但是,為表中添加數(shù)據(jù),若數(shù)據(jù)量很大時,一行一行添加很不現(xiàn)實,這種情況下一般使用程序來實現(xiàn)增刪改查。
四、Mysql綜合實例
綜合使用所有數(shù)據(jù)類型創(chuàng)建一個表
創(chuàng)建表:
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;
插入數(shù)據(jù)
為該表插入一行內容并顯示:
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;
為表中添加多行數(shù)據(jù)并顯示:
#插入數(shù)據(jù)
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;
刪除指定數(shù)據(jù)
(這里好不容易添加了數(shù)據(jù),我就少刪點)
#刪除id為3的數(shù)據(jù)
delete from tb2 where id=3;#顯示
select * from tb2;
修改數(shù)據(jù)
修改一整列
#更新密碼
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;
查找數(shù)據(jù)
查找某兩列
select id,name from tb2;
查找滿足條件的數(shù)據(jù)
select id,name,password from tb2 where name="譚sir" and age>18;
本文基于控制臺命令,命令繁瑣,可視化不太方便,市面上也有一些控制MySQL的軟件,后面可能會出新文介紹。還有Python程序存數(shù)據(jù)進庫,也會更新,大家靜待。。。
感謝您的三連!!!