日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python(数据库之表操作)

發布時間:2025/4/16 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python(数据库之表操作) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、修改表 1. 修改表名 ALTER TABLE 表名 RENAME 新表名; #mysql中庫名、表名對大小寫不敏感2. 增加字段 ALTER TABLE 表名ADD 字段名 數據類型 [完整性約束條件…], ADD 字段名 數據類型 [完整性約束條件…];ALTER TABLE 表名ADD 字段名 數據類型 [完整性約束條件…] FIRST; #插第一行 ALTER TABLE 表名ADD 字段名 數據類型 [完整性約束條件…] AFTER 字段名; #插在某一條記錄之后3. 刪除字段 ALTER TABLE 表名 DROP 字段名;4. 修改字段 ALTER TABLE 表名 MODIFY 字段名 數據類型 [完整性約束條件…]; #modify不能改字段名 ALTER TABLE 表名 CHANGE 舊字段名 新字段名 舊數據類型 [完整性約束條件…]; ALTER TABLE 表名 CHANGE 舊字段名 新字段名 新數據類型 [完整性約束條件…]; #change可以改字段名 #修改表通常改的是字段,而不修改數據類型(前后沖突則報錯) 二、復制表 a、復制表結構+記錄 (key不會復制: 主鍵、外鍵和索引) mysql> create table new_service select * from service;b、只復制表結構 mysql> select * from service where 1=2; //條件為假,查不到任何記錄 Empty set (0.00 sec) mysql> create table new1_service select * from service where 1=2; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> create table t4 like employees; #表結構一摸一樣 三、刪除表 DROP TABLE 表名; 單表查詢 一、語法順序 select distinct 查詢字段1,查詢字段2,。。。 from 表名where 分組之前的過濾條件group by 分組依據having 分組之后的過濾條件order by 排序字段limit 顯示的條數;二、執行順序 def from(dir,file):open('%s\%s' %(dir,file),'r')return fdef where(f,pattern):for line in f:if pattern:yield linedef group_by():passdef having():passdef distinct():passdef order_by():passdef limit():passdef select():res1=from() #在硬盤中找到表res2=where(res1,pattern) #拿著where指定的約束條件,去文件/表中取出一條條記錄,在內存中得到一張虛擬的表, 如果沒有where,默認全Trueres3=group_by(res2,) #將取出的一條條記錄進行分組group by,如果沒有group by,默認整體作為一組res4=having(res3) #將分組的結果進行having過濾,如果沒有having,默認全Trueres5=distinct(res4) #去重, 如果沒有distinct,默認不去重res6=order_by(res5) #將結果按條件排序limit(res6) #限制結果的顯示條數 三、按照優先級的級別寫SQL語句 a、先確定是哪張表 from db39.emp b、是否有過濾條件 where name like '%i%' 。。。 z、放功能 select四、where過濾 where字句中可以使用: 1. 比較運算符:> < >= <= <> != #不等于用 != 不用 <>select id,name from db39.emp where id >= 3 and id <= 62. between 80 and 100select * from db39.emp where id between 3 and 6; # >=3 and <=63. in(80,90,100) 值是80或90或100select * from emp where salary in (20000,18000,17000); # select * from emp where salary = 20000 or salary = 18000 or salary = 17000;4. like 'egon%', pattern可以是%或_, %表示任意多字符, _表示一個字符select name,salary from db39.emp where name like '%i%' #要求:查詢員工姓名中包含i字母的員工姓名與其薪資select name,salary from db39.emp where name like '____'; #要求:查詢員工姓名是由四個字符組成的的員工姓名與其薪資select name,salary from db39.emp where char_length(name) = 4; #結果與上一條一致5. 邏輯運算符:在多個條件直接可以使用邏輯運算符 and or notselect * from db39.emp where id not between 3 and 6;select * from emp where salary not in (20000,18000,17000);要求:查詢崗位描述為空的員工名與崗位名select name,post from db39.emp where post_comment is NULL; #針對NULL必須用is,不能用=select name,post from db39.emp where post_comment is not NULL;#NULL指的是不占任何存儲空間,在mysql中空字符串也是占存儲空間的,即不為空(NULL) 五、group by分組如果不設置成only_full_group_by模式,分完組后用*默認取出的是組內的第一個人的數據。但分完組后單獨取組內的某個元素是沒有意義的,所以,分組前,一般會對模式做如下處理#設置sql_mode為only_full_group_by,意味著以后但凡分組,只能取到分組的依據mysql> set global sql_mode="strict_trans_tables,only_full_group_by";#聚合函數 group function(一般與分組連用)select post,max(salary) from emp group by post; #取不出組內的元素name, age..,只能取組名(分組依據)或用聚合函數 select post,min(salary) from emp group by post;select post,avg(salary) from emp group by post;select post,sum(salary) from emp group by post;select post,count(id) from emp group by post;#group_concat(分組之后用):把想要用的信息取出;字符串拼接操作select post,group_concat(name) from emp group by post;select post,group_concat(name,"_SB") from emp group by post;select post,group_concat(name,": ",salary) from emp group by post;select post,group_concat(salary) from emp group by post;# 補充concat(不分組時用):字符串拼接操作select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪資 from emp;# 補充as語法:為字段或表取別名select name as 姓名,salary as 薪資 from emp; # as可省略mysql> select emp.id,emp.name from emp as t1; # 報錯mysql> select t1.id,t1.name from emp as t1; # 同 mysql> select id,name from emp as t1;# 查詢四則運算select name,salary*12 as annual_salary from emp;#分組練習select post,group_concat(name) from emp group by post; #查詢崗位名以及崗位包含的所有員工名字 select post,count(id) from emp group by post; #查詢崗位名以及各崗位內包含的員工個數 select sex,count(id) from emp group by sex; #查詢公司內男員工和女員工的個數 select post,avg(salary) from emp group by post; #查詢崗位名以及各崗位的平均薪資 select sex,avg(salary) from emp group by sex; #查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資 select post,avg(salary) from emp where age >= 30 group by post; #統計各部門年齡在30歲以上的員工平均工資 六、having過濾 (一定要用組名(分組依據)或聚合函數)having的語法格式與where一模一樣,只不過having是在分組之后進行的進一步過濾即where不能用聚合函數,而having是可以用聚合函數,這也是他們倆最大的區別#統計各部門年齡在30歲以上的員工平均工資,并且保留平均工資大于10000的部門select post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;#強調:having必須在group by后面使用 (不認默認分組)select * from emp having avg(salary) > 10000; #報錯 七、distinct去重 (在having之后執行,和post,name等屬于同一執行級別) select distinct post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;八、order by 排序 (默認升序) select * from emp order by salary asc; #默認升序排 select * from emp order by salary desc; #降序排 select * from emp order by age desc; #降序排 select * from emp order by age desc,salary asc; #先按照age降序排,再按照薪資升序排# 統計各部門年齡在10歲以上的員工平均工資,并且保留平均工資大于1000的部門,然后對平均工資進行排序 select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);九、limit 限制顯示條數;分頁 select * from emp limit 3; select * from emp order by salary desc limit 1; #顯示薪資最高人的信息 select * from emp limit 0,5; #分頁, 從0開始,取5條(1-5) select * from emp limit 5,5; #分頁, 從5開始,取5條(6-10) 十、正則表達式 select * from emp where name regexp '^jin.*(n|g)$'; #調正則;正則表達式通用 多表連接查詢 一、笛卡爾積from emp,dep,dep2,...二、內連接:把兩張表有對應關系的記錄連接成一張虛擬表 select * from emp inner join dep on emp.dep_id = dep.id;#應用:select * from emp,dep where emp.dep_id = dep.id and dep.name = "技術"; # 不推薦;不要用where做連表的活select * from emp inner join dep on emp.dep_id = dep.id where dep.name = "技術"; #邏輯與上一條一致 三、左連接:在內連接的基礎上,保留左邊沒有對應關系的記錄 select * from emp left join dep on emp.dep_id = dep.id;四、右連接:在內連接的基礎上,保留右邊沒有對應關系的記錄 select * from emp right join dep on emp.dep_id = dep.id;五、全連接:在內連接的基礎上,保留左、右邊沒有對應關系的記錄 select * from emp left join dep on emp.dep_id = dep.id union #去重 select * from emp right join dep on emp.dep_id = dep.id;六、多表連接可以是單表不斷地與虛擬表連接 #查找各部門最高工資 select t1.* from emp as t1 inner join (select post,max(salary) as ms from emp group by post) as t2 #把虛擬表提成t2 on t1.post = t2.post where t1.salary = t2.ms ;select t1.* from emp as t1 inner join (select post,max(salary) as ms from emp group by post) as t2 on t1.salary = t2.ms ;

?

轉載于:https://www.cnblogs.com/zhaijihai/p/9648327.html

總結

以上是生活随笔為你收集整理的Python(数据库之表操作)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。