




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、回顧前面的基礎(chǔ)命令語句修改數(shù)據(jù)表添加字段:alter table 表名 add 字段名 列類型 not null|nullprimary keyuniqueauto_incrementdefault valuealter table 表名 add 字段定義 after ar_id;刪除字段:alter table 表名 drop 字段名修改字段:alter table 表名 modify 字段名 字段新類型完整修改字段:alter table 表名 change 舊字段名稱 新字段定義修改表名稱alter table 表名 rename 新名字刪除表drop table if (not) ex
2、ists 表名;表中行的操作insertinsert into 數(shù)據(jù)表名稱 (字段列表) values|value (表達(dá)式|null|default,.),(表達(dá)式|null|default,.)insert into 數(shù)據(jù)表名稱 set 字段名稱=值,.insert與insert.set的區(qū)別是后者可以帶有子查詢。update - 單表update 數(shù)據(jù)表名稱 set 字段名稱=值,. where 條件如果省略WHERE條件將更新全部記錄。刪除記錄 - 單表delete from 數(shù)據(jù)表名稱 where 條件如果省略where條件,將刪除全部記錄selectselect 字段列表 from
3、 數(shù)據(jù)表 as 別名 where 條件別名: 數(shù)據(jù)表 as 別名select AA.money,BB.namefrom product_offer_instance_object_xxx as AA , product_offer_instance_object_ZZZ bbwhere AA.id = BB.id字段名稱 as別名Select product_offer_instance_object_id as ID, product_offer_instance_object_name name,coumn33 金額From tableselect語句返回零條或多條記錄;屬于記錄讀操作in
4、sert、update、delete只返回此次操作影響的記錄數(shù);屬于寫操作數(shù)據(jù)類型整型tinyint,占1字節(jié),有符號:-128127,無符號位:0255smallint,占2字節(jié),有符號:-3276832767,無符號位:065535mediumint,占3字節(jié),有符號:-83886088388607,無符號位:016777215int,占4字節(jié),有符號:-21474836482147483647,無符號位:04284967295bigint,占8字節(jié)bool 等價于tinyint(1) 布爾型浮點型float(m,d) 占4字節(jié),1.17E-383.4E+38double(m,d) 占8字
5、節(jié)decimal(m,d) 以字符串形式表示的浮點數(shù)字符型char(m):定長的字符,占用m字節(jié)varchar(m):變長的字符,占用m+1字節(jié),大于255個字符:占用m+2tinytext,255個字符(2的8次方)text,65535個字符(2的16次方)mediumtext,16777215字符(2的24次方)longtext,(2的32次方)enum(value,value,.)占1/2個字節(jié) 最多可以有65535個成員set(value,value,.)占1/2/3/4/8個字節(jié),最多可以有64個成員常用select命令使用select命令查看mysql數(shù)據(jù)庫系統(tǒng)信息:- 打印當(dāng)前的
6、日期和時間select now();- 打印當(dāng)前的日期select curdate();- 打印當(dāng)前的時間select curtime();- 打印當(dāng)前數(shù)據(jù)庫select database();- 打印MySQL版本select version();- 打印當(dāng)前用戶select user();-查看系統(tǒng)信息show variables;show global variables;show global variables like %version%;show variables like %storage_engine%; 默認(rèn)的存儲引擎like模糊搜索還可用戶where字句,例如selec
7、t * from students where stname like %l%1%2%3%;除了like 還有not likeshow engines;查看支持哪些存儲引擎-查看系統(tǒng)運行狀態(tài)信息show status;show global status like Thread%;多使用help導(dǎo)出,導(dǎo)入數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)庫前必須創(chuàng)建一個空數(shù)據(jù)庫create database book;mysql -uroot -p123456 book use book;mysql show tables;+-+| Tables_in_book |+-+| books | category |+-+方
8、法2:create database book;mysql use book;mysql source /root/book.sql #sql腳本的路徑mysql show tables;+-+| Tables_in_book |+-+| books | category |+-+導(dǎo)出數(shù)據(jù)庫導(dǎo)出數(shù)據(jù)庫:mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 導(dǎo)出的文件名mysqldump -u system -p123456 bookbook2.sql如何把一個select的結(jié)果導(dǎo)出到文本select * into outfile /tmp/123.txt from books; 此處有個文件訪問權(quán)
9、限問題,mysql用戶是可以訪問/tmp路徑的,所以這里放到tmp下select name,ctfid,birthday,mobile,tel,email from info where ctfid like 130000% into outfile /tmp/fuping-of-rujia;其實就是備份數(shù)據(jù)庫Sql查詢語句進階在我們剛導(dǎo)入的book數(shù)據(jù)庫進行測試查看表的內(nèi)容:mysql select * from category;mysql select * from books;mysql select * from booksG查看字段類型:desc 表名mysql desc book
10、s;邏輯運算符:and or notand 且or 或not 非選擇出書籍價格為(30,40,50,60)的記錄,只顯示書籍名稱,出版社,價格mysql select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;+-+-+-+| bName | publishing | price |+-+-+-+| Illustrator 10完全手冊 | 科學(xué)出版社 | 50 | FreeHand 10基礎(chǔ)教程 | 北京希望電子出版 | 50 | 網(wǎng)站設(shè)計全程教程 | 科學(xué)出版社
11、| 50 | ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 人民郵電出版社 | 60 | Delphi 5程序設(shè)計與控件參考 | 電子工業(yè)出版社 | 60 | ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 人民郵電出版社 | 60 |+-+-+-+算術(shù)運算符:=等于不等于 !=大于=大于等于 select bName,price from books where price60;找出價格為60的mysql select bName,price from books where price=60;找出價格不等于60的mysql select bName,price from books where price60;找出
12、價格是60,50,70的記錄mysql select bName,price from books where price in (50,60,70);找出價格不是60,50,70的記錄mysql select bName,price from books where price not in (50,60,70);排序:升序:order by “排序的字段” asc 默認(rèn)降序:oredr by “排序的字段” desc mysql select bName,price from books where price in (50,60,70) order by price asc;+-+-+|
13、 bName | price |+-+-+| Illustrator 10完全手冊 | 50 | FreeHand 10基礎(chǔ)教程 | 50 | 網(wǎng)站設(shè)計全程教程 | 50 | ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 60 | Delphi 5程序設(shè)計與控件參考 | 60 | ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 60 |mysql select bName,price from books where price in (50,60,70) order by price desc;+-+-+| bName | price |+-+-+| ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 60 | Delphi 5程序設(shè)
14、計與控件參考 | 60 | ASP數(shù)據(jù)庫系統(tǒng)開發(fā)實例導(dǎo)航 | 60 | Illustrator 10完全手冊 | 50 | FreeHand 10基礎(chǔ)教程 | 50 | 網(wǎng)站設(shè)計全程教程 | 50 |范圍運算:notbetween .and.Between and 可以使用大于小于的方式來代替,并且使用大于小于意義表述更明確查找價格不在30到60之間的書名和價格mysql select bName,price from books where price not between 30 and 60 order by price desc;注:這里的查詢條件有三種:between。and,or
15、和 in模糊匹配查詢:字段名 notlike 通配符 -% 任意多個字符查找書名中包括程序字樣記錄mysql select bName from books where bName like %程序%;不含有mysql select bName from books where bName not like %程序%;MySQL子查詢:概念:在select 的where條件中又出現(xiàn)了select查詢中嵌套著查詢選擇 類型名為“網(wǎng)絡(luò)技術(shù)”的圖書:mysql select bName,bTypeId from books where bTypeId=(select bTypeId from cat
16、egory where bTypeName=網(wǎng)絡(luò)技術(shù));選擇類型名稱為“黑客”的圖書;mysql select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=黑客);Limit限定顯示的條目:SELECT*FROMtableLIMIToffset,rows 偏移量 行數(shù)LIMIT 子句可以被用于強制 SELECT 語句返回指定的記錄數(shù)。LIMIT 接受一個或兩個數(shù)字參數(shù)。參數(shù)必須是一個整數(shù)常量。如果給定兩個參數(shù),第一個參數(shù)指定第一個返回記錄行的偏移量,第二個參數(shù)指定返回記
17、錄行的最大數(shù)目。初始記錄行的偏移量是 0(而不是 1): 比如select * from table limit m,n語句表示其中m是指記錄開始的index,從0開始,表示第一條記錄n是指從第m+1條開始,取n條。查出category表中第2條到第6行的記錄。首先2到6行有2,3,4,5,6總共有5個數(shù)字,從2開始,偏移量為1mysql select * from category limit 1,5;+-+-+| bTypeId | bTypeName |+-+-+| 2 | 網(wǎng)站 | 3 | 3D動畫 | 4 | linux學(xué)習(xí) | 5 | Delphi學(xué)習(xí) | 6 | 黑客 |+-+-
18、+查看所有書籍中價格中最低的三條記錄我們對所有記錄排序以升序排列,取出前面3個來mysql select bName,price from books order by price asc limit 0,3;+-+-+| bName | price |+-+-+| 網(wǎng)站制作直通車 | 34 | 黑客與網(wǎng)絡(luò)安全 | 41 | 網(wǎng)絡(luò)程序與設(shè)計asp | 43 |我們將子查詢和限制條目,算術(shù)運算結(jié)合起來查詢顯示字段bName ,price ;條件:找出價格比電子工業(yè)出版社出版的書中最便宜還便宜。針對這種查詢,我們一步步的來,先找出電子工業(yè)出版社出版中最便宜的書mysql select bName,
19、price from books where publishing=電子工業(yè)出版社 order by price asc limit 0,1;mysql select bName,price from books where price select bName,price from books where price create database school;mysql create table student( - sid int(4) primary key auto_increment, - name varchar(50)- );mysql insert into student
20、 values(1,張三),(2,李四),(3,王二麻子),(4,HA),(5,Tom);mysql create table grade(id int(4) primary key auto_increment, score varchar(20), sid int(4);mysql insert into grade (score,sid) values (1567,2),(1245,3),(1231,4),(1234,5),(1243,6);查詢有的成績的人的信息.mysql select student.*,grade.* from student a,grade where stud
21、ent.sid=grade.sid;mysql select student.*,grade.* from student inner join grade on student.sid=grade.sid;mysql select student.*,grade.* from student join grade on student.sid=grade.sid;使用表別名,簡寫SQL語句mysql select s.*,g.* from student as s inner join grade as g on s.sid=g.sid;select b.score, from
22、grade b,student a where a.sid = b.sid外連接左連接: select 字段 from a表 left join b表 on 連接條件a表是主表,都顯示。b表從表主表內(nèi)容全都有,從表內(nèi)沒有的顯示null。右連接:select 字段 from a表 right join b表 on 條件 a表是從表,b表主表,都顯示。mysql select * from student as s right join grade as g on s.sid=g.sid; +-+-+-+-+-+| sid | name | id | score | sid |+-+-+-+-+-
23、+| 2 | 李四 | 1 | 1567 | 2 | 3 | 王二麻子 | 2 | 1245 | 3 | 4 | HA | 3 | 1231 | 4 | 5 | Tom | 4 | 1234 | 5 | NULL | NULL | 5 | 1243 | 6 |右連接,可以多表連接mysql select * from student as s left join grade as g on s.sid=g.sid; +-+-+-+-+-+| sid | name | id | score | sid |+-+-+-+-+-+| 1 | 張三 | NULL | NULL | NULL | 2 |
24、 李四 | 1 | 1567 | 2 | 3 | 王二麻子 | 2 | 1245 | 3 | 4 | HA | 3 | 1231 | 4 | 5 | Tom | 4 | 1234 | 5 |+-+-+-+-+-+聚合函數(shù)函數(shù):執(zhí)行特定功能的代碼塊。算數(shù)運算函數(shù):Sum()求和顯示所有圖書單價的總合mysql select sum(price) from books;+-+| sum(price) |+-+| 10048 |+-+avg()平均值:求書籍Id小于3的所有書籍的平均價格mysql select avg(price) from books where bId select bName
25、,max(price) from books; 這種方法是錯誤的+-+-+| bName | max(price) |+-+-+| 網(wǎng)站制作直通車 | 7500 |+-+-+mysql select bName,price from books where price=(select max(price) from books);+-+-+| bName | price |+-+-+| Javascript與Jscript從入門到精通 | 7500 |+-+-+min()最小值:求所有圖書中價格便宜的書籍mysql select bName,price from books where pri
26、ce=(select min(price) from books);+-+-+| bName | price |+-+-+| 網(wǎng)站制作直通車 | 34 |+-+-+count()統(tǒng)計記錄數(shù):統(tǒng)計價格大于40的書籍?dāng)?shù)量mysql select count(*) from books where price40;+-+| count(*) |+-+| 43 |+-+Count()中還可以增加你需要的內(nèi)容,比如增加distinct來配合使用算數(shù)運算:+ - * / 給所有價格小于40元的書籍,漲價5元mysql update books set price=price+5 where price update books set price=price*0.8 where price70;字符串函數(shù):substr(string ,start,len) 截?。簭膕tart開始,截取len長.start 從1開始算起。mysql select substr(bTypeName,1,6)from category where bTypeId=10;+-+| substr(bTypeName,1
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T/ZHCA 003-2018化妝品影響經(jīng)表皮水分流失測試方法
- 重慶科瑞制藥(集團)有限公司招聘筆試題庫2025
- T/YNIA 022-2024閃蒸法非織造布
- 2025年智能制造與工業(yè)互聯(lián)網(wǎng)知識測試試題及答案
- 2025年現(xiàn)代物流與供應(yīng)鏈管理專業(yè)研究生入學(xué)考試試卷及答案
- 2025年網(wǎng)絡(luò)安全技術(shù)知識考試試卷及答案
- 2025年生物技術(shù)與金融職業(yè)資格考試試題及答案
- 2025年生命科學(xué)專業(yè)知識與技能測試題及答案
- 2025年企業(yè)文化建設(shè)與管理試題及答案
- 2025年法律文書寫作能力考試試題及答案
- 2024年海南省高考地理試卷(含答案)
- 《排球正面雙手墊球 移動墊球》教案
- 《菊次郎的夏天》電影賞析
- 課件:《中華民族共同體概論》第十五講:新時代與中華民族共同體建設(shè)
- 汽車剎車片與剎車盤檢測考核試卷
- 2024年海南省中考?xì)v史試題
- 2024年中考語文記述文閱讀題答題模板及練習(xí):人稱及其作用分析(原卷版)
- 高空吊板作業(yè)專項方案
- 事業(yè)單位員工保密協(xié)議書范本(2024版)
- 化工設(shè)備機械基礎(chǔ)試題庫(附參考答案)
- JG-T+502-2016環(huán)氧樹脂涂層鋼筋
評論
0/150
提交評論