mac os mysql 命令_Mac环境下MySQL的安装和基本命令的使用
MySQL是一種關(guān)系數(shù)據(jù)庫管理系統(tǒng),關(guān)系數(shù)據(jù)庫將數(shù)據(jù)保存在不同的表中,而不是將所有數(shù)據(jù)放在一個大倉庫內(nèi),這樣就增加了速度并提高了靈活性。
MySQL所使用的SQL語言是用于訪問數(shù)據(jù)庫的最常用標(biāo)準(zhǔn)化語言。
由于其體積小、速度快、總體擁有成本低,尤其是開放源碼這一特點,許多中小型網(wǎng)站為了降低網(wǎng)站總體擁有成本而選擇了MySQL作為網(wǎng)站數(shù)據(jù)庫。
MySQL是一個多用戶、多線程的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)。 工作模式是基于客戶機/服務(wù)器結(jié)構(gòu)。目前它可以支持幾乎所有的操作系統(tǒng)
簡單的來說,MySQL是一個開放的、快速的、多線程的、多用戶的SQL數(shù)據(jù)庫服務(wù)器
一. MySQL的安裝
工欲善其事必先利其器, 要研究MySQL我們首先要安裝MySQL, 這里只介紹Mac環(huán)境下的安裝和數(shù)據(jù)庫操作
1. 下載MySQL
直接打開MySQL官網(wǎng)下載頁, 選擇mac OS系統(tǒng)后, 選擇DMG格式下載軟件
接著, 會跳轉(zhuǎn)到如下頁面, 你只需要選擇不登錄,直接下載即可(當(dāng)然也可以選擇注冊并登錄)
下載好后, 按照dmg里面的pkg文件一路安裝即可, 但是需要注意的是
除了倒數(shù)第二步之外按照默認(rèn)一路安裝即可
倒數(shù)第二步會有一個設(shè)置管理員密碼的過程, 設(shè)置好后, 一定要牢記該密碼, 后期鏈接數(shù)據(jù)庫會需要
最后打開系統(tǒng)偏好設(shè)置, 最后會有一個MySQL的圖標(biāo)
打開MySQL會看到默認(rèn)是開啟的(安裝的時候按照默認(rèn)設(shè)置安裝的情況下)
到這里MySQL就已經(jīng)基本安裝完成了, 不需要再修改什么配置了
安裝Navicat for MySQL
Navicat for MySQL是一套專為MySQL設(shè)計的高性能數(shù)據(jù)庫管理及開發(fā)工具
它可以用于任何版本3.21或以上的 MySQL數(shù)據(jù)庫服務(wù)器
支持大部份MySQL最新版本的功能,包括觸發(fā)器、存儲過程、函數(shù)、事件、視圖、管理用戶等
正版下載地址, 不過正版只有14天的試用時間
安裝后, 按照下圖完善配置即可, 其中連接名隨意, 密碼即為安裝MySQL環(huán)境的時候設(shè)置的密碼
二. MySQL的基本命令
1. 基本命令
首先要打開終端(Windows中是cmd), 以下命令均是在終端運行
啟動/停止服務(wù)只有在Windows系統(tǒng)中才需要運行, Mac環(huán)境下不需要
1-1. 啟動/停止服務(wù)
// 啟動服務(wù)
格式:net start 服務(wù)名稱
示例:net start titansql
// 停止服務(wù)
格式:net stop 服務(wù)名稱
示例:net stop titansql
復(fù)制代碼
1-2. 連接數(shù)據(jù)
格式:mysql -u 用戶名 -p
示例:mysql -u root -p
// 此處會提示你輸入密碼(安裝時設(shè)置的)
復(fù)制代碼
1-3. 遠(yuǎn)程連接
鏈接他人或其他服務(wù)器的數(shù)據(jù)庫
格式:mysql -h ip地址 -u 用戶名 -p
輸入對方mysql密碼
1-4. 其他命令
需要注意的是:
以下所有命令中如過結(jié)尾有分號(;)的一定不能省略, 否則不是一條完整的命令, 系統(tǒng)會提示你繼續(xù)輸入命令
// 查看版本(連接后可以執(zhí)行)
select version();
//顯示當(dāng)前時間(連接后可以執(zhí)行)
select now();
//退出登錄(斷開連接)
quit或exit
復(fù)制代碼
2. 數(shù)據(jù)庫操作
// 1、創(chuàng)建數(shù)據(jù)庫
格式:create database 數(shù)據(jù)庫名 charset=utf8;
示例:create database titansql charset=utf8;
// 2、刪除數(shù)據(jù)庫
格式:drop database 數(shù)據(jù)庫名;
示例:drop database titansql;
// 3、切換數(shù)據(jù)庫
格式:use 數(shù)據(jù)庫名;
示例:use titansql;
// 4、查看當(dāng)前選擇的數(shù)據(jù)庫
select database();
復(fù)制代碼
創(chuàng)建完成記得刷新Navicat for MySQL
3. 表操作
// 1、查看當(dāng)前數(shù)據(jù)庫中所有表
show tables;
// 2、創(chuàng)建表
格式:create table 表名(列及類型);
說明:
//id, name, age: 等為字段名
//auto_increment: 表示自增長
//primary key: 表示主鍵
//int, varchar(20): 等為數(shù)據(jù)類型, 20為可存儲的字節(jié)數(shù)
//not null: 表示不為空
//default: 為設(shè)置默認(rèn)值
示例:create table student(id int auto_increment primary key, name varchar(20) not null, age int not null, gender bit default 1, address varchar(20), isDelete bit default 0);
// 3、刪除表
格式:drop table 表名;
示例:drop table student;
// 4、查看表結(jié)構(gòu)
格式:desc 表名;
示例:desc student;
// 5、查看建表語句
格式:show create table 表名;
示例:show create table student;
// 6、重命名表名
格式:rename table 原表名 to 新表名;
示例:rename table car to newCar;
// 7、修改表
格式:alter table 表名 add|change|drop 列名 類型;
示例:alter table newcar add isDelete bit default 0
復(fù)制代碼
4. 數(shù)據(jù)操作
1、增
a、全列插入
格式:insert into 表名 values(...);
說明:主鍵列是自動增長,但是在全列插入時需要占位,通常使用0,插入成功以后以實際數(shù)據(jù)為準(zhǔn)
示例:insert into student values(0, "tom", 19, 1, "北京", 0);
b、缺省插入
格式:insert into 表名(列1,列2,……) values(值1,值2,……);
示例:insert into student(name, age, address) values("titan", 19, "上海");
c、同時插入多條數(shù)據(jù)
格式:insert into 表名 values(...),(...),……
示例:insert into student values(0, "jun", 18, 0, "北京", 0), (0, "poi", 22, 1, "海南", 0), (0, "coder", 20, 0, "石家莊", 0);
2、刪
格式:delete from 表名 where 條件;
示例:delete from student where id=4;
注意:沒有條件是全部刪除,慎用
3、改
格式:update 表名 set 列1=值1,列2=值2,…… where 條件;
示例:update student set age=16 where id=7;
注意:沒有條件是全部列都修改,慎用
4、查
說明:查詢表中的全部數(shù)據(jù)
格式:select * from 表名;
示例:select * from student;
復(fù)制代碼
5. 查詢數(shù)據(jù)
1、基本語法
格式:select * from 表名;
說明:
from關(guān)鍵字后面是表名,表示數(shù)據(jù)來源于這張表
select后面寫表中的列名,如果是*表示在結(jié)果集中顯示表中的所有列
在select后面的列名部分,可以使用as為列名起別名,這個別名顯示在結(jié)果集中
如果要查詢多個列,之間使用逗號分隔
示例:
//查詢所有數(shù)據(jù)
select * from student;
//查詢某列數(shù)據(jù)
select name, age from student;
//以別名顯示搜索結(jié)果
select name as a, age from student;
復(fù)制代碼
消除重復(fù)行
在select后面列前面使用distinct可以消除重復(fù)的行
示例:
select gender from student;
select distinct gender from student;
復(fù)制代碼
條件查詢
// 1、語法
select * from 表名 where 條件
// 2、比較運算符
等于 =
大于 >
小于 <
大于等于 >=
小于等于 <=
不等于 !=或<>
需求:查詢id值大于8的所有數(shù)據(jù)
示例:select * from student where id>8;
// 3、邏輯運算符
and 并且
or 或者
not 非
需求:查詢id值大于7的女同學(xué)
示例:select * from student where id>7 and gender=0;
// 4、模糊查詢(like)
%: 表示任意多個任意字符
_: 表示一個任意字符
需求:查詢姓習(xí)的同學(xué)
示例:
select * from student where name like "習(xí)%";
select * from student where name like "習(xí)_";
// 5、范圍查詢
in 表示在一個非連續(xù)的范圍內(nèi)
between...and... 表示在一個連續(xù)的范圍內(nèi)
需求:查詢編號為8、10、12的學(xué)生
示例:select * from student where id in (8,10,12);
需求:查詢編號為6到8的學(xué)生
示例:select * from student where id between 6 and 8;
// 6、空判斷
注意:null與""是不同的
判斷空:is null
判斷非空: is not null
需求:查詢沒有地址的同學(xué)
示例:select * from student where address is null;
需求:查詢有地址的同學(xué)
示例:select * from student where address is not null;
// 7、優(yōu)先級
小括號,not 比較運算符,邏輯運算符
and比or優(yōu)先級高,如果同時出現(xiàn)并希望先選or,需要結(jié)合()來使用
復(fù)制代碼
聚合操作
為了快速等到統(tǒng)計數(shù)據(jù),提供了5個聚合函數(shù)
count(*): 表示計算總行數(shù),括號中可以寫*和列名
max(列): 表示求此列的最大值
min(列): 表示求此列的最小值
sum(列): 表示求此列的和
avg(列): 表示求此列的平均值
//需求:查詢學(xué)生總數(shù)
select count(*) from student;
//需求:查詢女生的編號最大值
select max(id) from student where gender=0;
//需求:查詢女生的編號最小值
select min(id) from student where gender=0;
//需求:查詢所有學(xué)生的年齡和
select sum(age) from student;
//需求:查詢所有學(xué)生的年齡平均值
select avg(age) from student;
復(fù)制代碼
分組
按照字段分組,表示此字段相同的數(shù)據(jù)會被放到一個集合中。
分組后,只能查詢出相同的數(shù)據(jù)列,對于有差異的數(shù)據(jù)列無法顯示在結(jié)果集中
可以對分組后的數(shù)據(jù)進(jìn)行統(tǒng)計,做聚合運算
語法:
select 列1,列2,聚合…… from 表名 group by 列1,列2,列3,……;
需求:查詢男女生總數(shù)
示例:
select gender,count(*) from student group by gender;
select name,gender,count(*) from student group by gender,age;
復(fù)制代碼
分組后的數(shù)據(jù)篩選:
select 列1,列2,聚合…… from 表名 group by 列1,列2,列3,…… having 列1,……聚合……;
示例:select gender,count(*) from student group by gender having gender;
復(fù)制代碼
where與having的區(qū)別:
where是對from后面指定的表進(jìn)行篩選,屬于對原始數(shù)據(jù)的篩選
having是對group by的結(jié)果進(jìn)行篩選
排序
語法:select * from 表名 order by 列1 asc|desc,列2 asc|desc , ……;
說明:
將數(shù)據(jù)按照列1進(jìn)行排序,如果某些列1的值相同,則按照列2進(jìn)行排序
默認(rèn)按照從小到大的順序排序
asc: 升序
desc: 降序
//需求:將沒有被刪除的數(shù)據(jù)按年齡排序
select * from student where isDelete=0 order by age desc;
select * from student where isDelete=0 order by age desc, id desc;
復(fù)制代碼
分頁
語法:select * from 表名 limit start,count;
說明:start索引從0開始
示例:
select * from student limit 0,3;
select * from student limit 3,3;
select * from student where gender=1 limit 0,3;
復(fù)制代碼
關(guān)聯(lián)
// 建表語句:
1、create table class(id int auto_increment primary key, name varchar(20) not null, stuNum int not null);
2、create table students(id int auto_increment primary key, name varchar(20) not null, gender bit default 1, classid int not null, foreign key(classid) references class(id));
// 查詢所有數(shù)據(jù)
select * from students;
/* 關(guān)聯(lián)查詢:
分類:
1、表A inner join 表B:
表A與表B匹配的行會出現(xiàn)在結(jié)果集中
2、表A left join 表B:
表A與表B匹配的行會出現(xiàn)在結(jié)果集中,外加表A中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)使用null填充
3、表A right join 表B:
表A與表B匹配的行會出現(xiàn)在結(jié)果集中,外加表B中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)使用null填充
*/
select students.name,class.name from class inner join students on class.id=students.classid;
select students.name,class.name from class left join students on class.id=students.classid;
select students.name,class.name from class right join students on class.id=students.classid;
復(fù)制代碼
至此, MySQL中一些常用的命令行也基本介紹完了, 下面看一些MySQL和Python是如何進(jìn)行交互的
MySQL和Python的交互
Python要對MySQL數(shù)據(jù)庫進(jìn)行操作, 需要引入pymysql模塊
pymsql是Python中操作MySQL的模塊, 并且pymysql支持python3.x版本
首先要先安裝pymysql, 終端執(zhí)行一下語句
pip3 install pymysql
復(fù)制代碼
1. 創(chuàng)建數(shù)據(jù)庫連接
# 鏈接數(shù)據(jù)庫
# 參數(shù)1:mysql服務(wù)所在主機的IP(可以是IP地址, 本機鏈接可以是localhost)
# 參數(shù)2:用戶名
# 參數(shù)3:密碼
# 參數(shù)4:要連接的數(shù)據(jù)庫名
db = pymysql.connect('localhost', 'root', 'titanjun', 'titansql')
# 創(chuàng)建游標(biāo), 查詢數(shù)據(jù)默認(rèn)為元組類型
cursor = db.cursor()
# 創(chuàng)建sql語句
sql = "select version()"
# 執(zhí)行sql語句
cursor.execute(sql)
# 獲取返回的信息
data = cursor.fetchone()
print(data)
# 關(guān)閉游標(biāo)
cursor.close()
# 關(guān)閉數(shù)據(jù)庫
db.close()
復(fù)制代碼
2. 創(chuàng)建表
import pymysql
db = pymysql.connect('localhost', 'root', 'jun.0929', 'titansql')
# 創(chuàng)建游標(biāo), 查詢數(shù)據(jù)默認(rèn)為元組類型
cursor = db.cursor()
# 建表
# 在建表之前要檢查表是否存在, 如果存在則刪除
cursor.execute("drop table if exists userinfo")
# 創(chuàng)建表
try:
sql = "create table userinfo(id int auto_increment primary key, age int not null)"
cursor.execute(sql)
print('創(chuàng)建成功')
except:
print('創(chuàng)建表失敗')
cursor.close()
db.close()
復(fù)制代碼
3. 在表中插入數(shù)據(jù)
import pymysql
db = pymysql.connect('localhost', 'root', 'jun.0929', 'titansql')
cursor = db.cursor()
# 插入數(shù)據(jù)的字符串命令
sql = 'insert into userinfo values'
for i in range(10, 20):
ageStr = "(0, %d)" % i
addsql = sql + ageStr
try:
cursor.execute(addsql)
# 提交到數(shù)據(jù)庫, 不然無法保存新建或者修改的數(shù)據(jù)
db.commit()
print('插入數(shù)據(jù)成功')
except:
# 如果提交失敗則回滾到上一次的提交, 否則下一次提交可能會沖突
db.rollback()
print('插入數(shù)據(jù)失敗')
cursor.close()
db.close()
復(fù)制代碼
4. 修改/更新/刪除數(shù)據(jù)
import pymysql
db = pymysql.connect('localhost', 'root', 'jun.0929', 'titansql')
cursor = db.cursor()
# 修改/更新數(shù)據(jù)命令字符串
sql = 'update userinfo set age=30 where id=4'
# 刪除數(shù)據(jù)命令字符串
# sql = 'delete from userinfo where age=16'
try:
cursor.execute(sql)
db.commit()
print('數(shù)據(jù)更新成功')
except:
db.rollback()
print('數(shù)據(jù)更新失敗')
cursor.close()
db.close()
復(fù)制代碼
5. 查詢數(shù)據(jù)
fetchone: 獲取下一個查詢結(jié)果集,結(jié)果集是一個對象
fetchall: 接收全部的返回的行
rowcount: 是一個只讀屬性,返回execute()方法影響的行數(shù)
import pymysql
db = pymysql.connect('localhost', 'root', 'jun.0929', 'titansql')
cursor = db.cursor()
# 查詢數(shù)據(jù)字符串
sql = 'select * from userinfo where age>16'
try:
cursor.execute(sql)
# 獲得一條查詢數(shù)據(jù)
print(cursor.fetchone())
print('查詢到-%d-條數(shù)據(jù)' % cursor.rowcount)
result = cursor.fetchall()
for row in result:
print('%d--%d' % (row[0], row[1]))
print('數(shù)據(jù)查詢成功')
except:
print('數(shù)據(jù)查詢失敗')
cursor.close()
db.close()
復(fù)制代碼至此, Python和MySQL交互的最基本最簡單的使用也介紹完了, 如有不足之處還望告知
總結(jié)
以上是生活随笔為你收集整理的mac os mysql 命令_Mac环境下MySQL的安装和基本命令的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker里面装mysql_docke
- 下一篇: ldap odbc mysql_Mysq