MySQL數據庫
數據庫(MySQL.com下載)
查看系統所有數據庫:show databases;
創建數據庫:create database 數據庫名 charset=utf8;
切換到指定數據庫:use 數據庫名
查看當前數據庫:select database();
展示創建數據庫的語句:show create database 數據庫名;
刪除數據庫:drop database 數據庫名;
創建數據表
展示當前數據庫中所有表:show tables;
創建一個表結構:create table 表名 (id int unsigned auto_increment primary key not null,name varchar(32),其他字段和屬性);?
增(0 或者null表示占位? 實際以默認或者自動增長的值? 為準)
全列插入和表結構的字段一致。例:insert into students values(0,'小李',150),(null, '小王', 180);
指定插入:insert into students (age, name) values(182, '小王');
刪
1、物理刪除
delete from students where id = 7;
刪除多行,可以使用in:delete from classmate where id in (2,4,5,6,7,8);
2、邏輯刪除-本質上是 更新數據的標記的操作
alter table students add isDelete bit default 0;
update students set isDelete = 1 where id = 1;
改
例:update students set age =0 where name='小王';
查(基本)
顯示全部信息select * from students;
指定字段查詢select name,age from students;
字段顯示別名,例:select name as '尊姓大名', age as '高壽' from students;
表起別名,例:select s.name,s.age from students s;
查(提高)
條件篩選:><=、不等于<>或者!=
使用枚舉查詢,enum是一個類,gender=enum()則是創建一個對象,而對象的返回值是內存地址,因此不能使用。枚舉(enum)本質數字構成的集合? 從1開始 ,可以直接用gender=1,來代表男性——select * from classmate where gender=1;
模糊匹配
%匹配任意多個字符:select * from students where name like '周%';
_匹配1個任意字符:select * from students where name like '周_';
查詢 姓名中含有小 的信息:select * from students where name like '%小%';
in () 判斷值是否在集合中存在
between 下限 and 上限
查詢 height字段沒有填寫的記錄,此處不能使用'=',必須使用is:select * from students where height is NULL;
排序(關鍵詞order by ——ASC 升序<默認>? DESC 降序)
select * from students order by height desc,age asc;
聚合函數
統計數量count()——select count(*) from students;
最大值select max(height) from students;
最小值select min(age) from students where gender = 1;
求和sum、求平均avg、保留小數位數round
分組
以性別分組 顯示性別 及其對應分組的平均年齡 及其 分組內每個成員的年齡——select gender, avg(age),group_concat(age) from students group by gender;
分組后過濾having——select gender, avg(age),group_concat(age) from students group by gender having count(*)>2;
分組后匯總with rollup——select gender,count(*) from students group by gender with rollup;
分頁
第n頁:limit (n-1)*每頁數,每頁數
例:第3頁的數據 每頁是3條select * from students limit 6, 3;——limit 開始位置是0
連接查詢——(笛卡爾積運算)
內連接——使用on過濾條件即兩個表中重復的字段select * from 表1 inner join 表2 on 表1.重復字段 = 表2.重復字段;
左連接—— select * from 表1 left join 表2 on 表1.重復字段 = 表2.重復字段;
可以重命名—例: select * from hero as h left join gongfu as g on h.gongfuid = g.id;
右連接——select * from 表1 right join 表2 on 表1.重復字段 = 表2.重復字段;
自連接—一個表即當表1又當表2,例:左表中的市所屬的省id即pid 需要等于右表的省份id即aid? ? (select * from areas city join areas pro on city.pid = pro.aid? where pro.atitle = '山東省'; )語句中有重命名
子查詢
標量子查詢
查詢出高于平均年齡的學生的信息select * from students where age > (select avg(age) from students);
列子查詢(結果是一個集合)—關鍵字in—格式:主查詢where條件in(列子查詢)
例:查詢已經上課的學生信息select * from students where cls_id in (select id from classes);
行子查詢(結果是一個元組)
例:查詢出班級上 年齡最大 和 身高最高的信息select * from students where (age,height) = (select max(age),max(height) from students);
展示創建表的語句:show create table students;
查看表結構:desc 表名;
修改表結構(相當于對字段的修改,而非表內容):alter table
添加一個字段—add:alter table students add age tinyint unsigned not null;
修改字段名稱、類型、約束—change:例:alter table students change birth(需要修改的字段) birthday(修改后字段) date not null(修改后字段的屬性);
只修改字段類型和約束? 不改名—modify:例:alter table students modify birthday(需要修改的字段) datetime not null(修改后屬性);
刪除字段—drop:例:alter table students drop birthday(需要刪除的字段);
和pycharm聯用
導入模塊:import pymysql
創建連接conn = pymysql.connect(host='127.0.0.1',port=3306, user='root',password='mysql',db='python_test_1',charset='utf8')
獲取游標cur = conn.cursor()
書寫sql語句
執行 數據庫操作row_count = cur.execute(sql)
提交修改conn.commit()
先關游標? cur.close()?
再關連接? conn.close()
參數化:可以有效防止sql注入—全部使用%s占位
在execute中傳入參數的列表,例:age = input('請輸入年齡:')
gender = input('請輸入性別:')? ? ? ? ? ? ? ? ??
sql = "select * from students where age>%s and gender= %s;"
row_count = cur.execute(sql,[age,gender])
MySQL高級
視圖——視圖就是一條SELECT語句執行后返回的結果集
創建視圖(建議以V-開頭),例:create view v_hebei_areas as select pro.atitle as patitle, c.id as cid, c.atitle as catitle, c.pid as cpid from areas as c join areas as pro on c.pid=pro.id where pro.atitle='河北省';
查看視圖:show tables;
使用視圖:select * from v_hebei_areas;
刪除視圖:drop view v_hebei_areas;
事務——原子性、一致性、隔離性、持久性
開啟事務:begin
提交事務:commit
回滾事務:rollback
索引
查看索引:show index from 表名;
創建索引:create index 索引名稱 on 表名(字段名稱(長度));
刪除索引:drop index 索引名稱 on 表名;
開啟時間監測:set profiling=1;
查看執行時間:show profiles;
賬戶管理——所有用戶及權限信息存儲在mysql數據庫的user表中
查看user表的結構:desc user;
查看所有用戶:select *或者(host,user,authentication_string) from user;分號前使用\G顯示全部內容
創建賬戶&授權:grant 權限列表 on 數據庫.* to '用戶名'@'訪問主機' identified by '密碼';
修改已有用戶的權限,例:grant select, create, update on students to 'xiaoyu'@'localhost' with grant option;
刷新:flush privileges;
修改已有用戶的密碼,例:update user set authentication_string=password('123456')where user='xiaoyu';
查看用戶有哪些權限,例:show grants for 'xiaoyu'@'localhost'
刪除一個用戶,例:drop user 'xiaoyu'@'localhost';