Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié)技術(shù)學(xué)習(xí)_第1頁(yè)
Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié)技術(shù)學(xué)習(xí)_第2頁(yè)
Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié)技術(shù)學(xué)習(xí)_第3頁(yè)
Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié)技術(shù)學(xué)習(xí)_第4頁(yè)
Mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié)技術(shù)學(xué)習(xí)_第5頁(yè)
已閱讀5頁(yè),還剩9頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、mysql數(shù)據(jù)庫(kù)學(xué)習(xí)總結(jié) 數(shù)據(jù)庫(kù)的基本操作:創(chuàng)建 刪除 查看 create database school; 用于創(chuàng)建數(shù)據(jù)庫(kù),并且數(shù)據(jù)庫(kù)的名字不可以更改 show create database; show databases; 用來(lái)查看創(chuàng)建數(shù)據(jù)庫(kù)的語(yǔ)句 drop database; 用于刪除數(shù)據(jù)庫(kù)表的基本操作:create table;用于創(chuàng)建表,table后面加表名稱(chēng) create table studentid int;name varchar(10);sex boolean; show tables; 用于顯示數(shù)據(jù)庫(kù)中的所有表 describe student;這里顯示了字段、數(shù)據(jù)類(lèi)型

2、、是否為空、主外鍵、默認(rèn)值和額外信息 show create table; 顯示創(chuàng)建表時(shí)的詳細(xì)信息 drop table student;刪除表的操作完整性約束 是對(duì)字段進(jìn)行限制,從而該字段達(dá)到我們期望的效果設(shè)置表的主鍵:主鍵能夠標(biāo)識(shí)表中的每條信息的唯一性。(primary key) 創(chuàng)建主鍵的目的在于快速查找到表中的某一條信息多字段主鍵:由多個(gè)屬性組合而成 例如:primary key(id,course_id);設(shè)置表的外鍵; 設(shè)置表的外鍵的作用在于建立與父表的聯(lián)系 比如表a中的id是外鍵,表b中的id是主鍵 那么就可以稱(chēng)表b為父表,表a為子表比如表b中id為123的學(xué)生刪除后,表a中id

3、為123的記錄也隨著消失這樣做的目的在于保證表的完整性。設(shè)置表的非空約束:設(shè)置表中的字段不為空設(shè)置表的唯一性約束 唯一性約束指表中該字段的值不能重復(fù)出現(xiàn),也就是給表中某個(gè)字段加上unique設(shè)置表的屬性值自動(dòng)增加: auto_increment 主要用于為表中插入的新紀(jì)錄自動(dòng)生成唯一id一個(gè)表中只能由一個(gè)字段使用此約束,并且該字段必須為主鍵的一部分,約束的值ibixu是整型值。設(shè)置表中屬性的默認(rèn)值在表中插入一體哦新的記錄時(shí),如果沒(méi)有為該字段賦值,那么數(shù)據(jù)庫(kù)系統(tǒng)就會(huì)為該字段附上一條默認(rèn)值。修改表修改表需要用到alter table修改表名: alter table student rename

4、person;rename 用來(lái)命名修改字段的數(shù)據(jù)類(lèi)型 alter table person modify name varchar(20);將原來(lái)的varchar(xx)修改為vaarchar(20)修改字段名 alter table person change stu_name name varchar(25)這里的stu_name是原名,name是新名,不管修不修改數(shù)據(jù)類(lèi)型,后面的數(shù)據(jù)類(lèi)型都要寫(xiě)增加無(wú)完整性約束條件的字段 alter table person add sex boolean;此處的sex 后面值跟了數(shù)據(jù)類(lèi)型,而沒(méi)有完整性約束條件增加完整性約束體條件的字段 alter ta

5、ble person add age int not null;增加了一條age字段,接著在后面加上了約束條件增加額外的完整性約束條件 alter table person add primary key first;這樣同樣也用于多字段設(shè)置在表頭添加字段 alter table person add num int primary key first;默認(rèn)情況下添加到表尾,在添加語(yǔ)句后面加上first節(jié)能添加到表頭在指定位置添加字段 alter table person add birth date after name;這里添加一條新字段在name后面刪除字段 alter table pe

6、rson drop sex;修改字段到第一個(gè)位置alte table person modify id int first修改字段到指定的位置 alter table person modify name varchar(25) after id;我們要把name字段放到id后面,此處varchar(25)要寫(xiě)全修改表的存儲(chǔ)引擎alter table user rename person;增加表的外鍵: alter table score add constraint fk foreign key(stu_id) references student(id);刪除主鍵 alter table

7、person drop primary key刪除了所有的主鍵刪除表的外鍵約束 alter table student3 drop foreign key fk由于基本的表結(jié)構(gòu)描述無(wú)法顯示外鍵,所以在進(jìn)行此操作前最好使用show create table查看表這里的fk就是剛剛設(shè)置的外鍵需要注意的是:如果想要?jiǎng)h除有關(guān)聯(lián)的表,那么必先刪除外鍵刪除外鍵后,原先的key變成普通鍵索引分類(lèi)1. 普通索引:不附加任何限制條件,可創(chuàng)建在任何數(shù)據(jù)類(lèi)型中2. 唯一性索引:使用unique參數(shù)可以設(shè)置索引為唯一性索引,在創(chuàng)建索引時(shí),限制該索引為唯一性索引,主鍵就是一種唯一性索引3. 全文索引:使用fulltex

8、t參數(shù)可以設(shè)置索引為全文索引。全文索引只能創(chuàng)建在char、varchar或text類(lèi)型的字段上。查詢(xún)數(shù)據(jù)量較大的字符串類(lèi)型字段時(shí),效果明顯。但只有myisam存儲(chǔ)引擎支持全文檢索4. 單列索引:在表中單個(gè)字段上創(chuàng)建索引5. 多列索引:在表中多個(gè)字段上創(chuàng)建的索引6. 空間索引:使用spatial參數(shù)可以設(shè)置索引為空間索引,空間索引只能建立在空間數(shù)據(jù)類(lèi)型上比如geometry,并且不能為空,目前只有myisam存儲(chǔ)引擎支持7. 創(chuàng)建普通索引mysql create table index1( - id int, - name varchar(20), - sex boolean, - index(

9、id) - );query ok, 0 rows affected (0.11 sec)此處在id字段上創(chuàng)建索引,show create table可查看創(chuàng)建唯一性索引mysql create table index2( - id int unique, - name varchar(20), - unique index index2_id(id asc) - );query ok, 0 rows affected (0.12 sec)此處使用id字段創(chuàng)建了一個(gè)名為index2_id的索引這里的id字段可以不設(shè)置唯一性約束,但這樣一來(lái)索引就沒(méi)有作用創(chuàng)建全文索引mysql create tab

10、le index3( - id int, - info varchar(20), - fulltext index index3_info(info) - )engine=myisam;query ok, 0 rows affected (0.07 sec)要注意創(chuàng)建全文索引時(shí)只能使用myisam存儲(chǔ)引擎創(chuàng)建單列索引mysql create table index4( - id int, - subject varchar(30), - index index4_st(subject(10) - );query ok, 0 rows affected (0.12 sec)此處subject字段

11、長(zhǎng)度是30,而索引長(zhǎng)度則是10這么做的目的在于提高查詢(xún)速度,對(duì)于字符型的數(shù)據(jù)不用查詢(xún)?nèi)啃畔?chuàng)建多列索引mysql create table index5( - id int, - name varchar(20), - sex char(4), - index index5_ns(name,sex) - );query ok, 0 rows affected (0.10 sec)可以看出,這里使用了name字段和sex字段創(chuàng)建索引列創(chuàng)建空間索引mysql create table index6( - id int, - space geometry not null, - spatial i

12、ndex index6_sp(space) - )engine=myisam;query ok, 0 rows affected (0.07 sec)這里需要注意空間space字段不能為空,還有存儲(chǔ)引擎在已經(jīng)存在的表上創(chuàng)建索引創(chuàng)建普通索引mysql create index index7_id on example0(id);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0這里在現(xiàn)有表的id字段上創(chuàng)建了一條名為index7_id的索引創(chuàng)建唯一性索引mysql create unique index

13、index8_id on example1(course_id);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0此處只需要在index關(guān)鍵字前面加上unique即可至于表中的course_id字段,最要也設(shè)置唯一性約束條件創(chuàng)建全文索引mysql create fulltext index index9_info on example2(info);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0full

14、text關(guān)鍵字用來(lái)設(shè)置全文引擎,此處的表必須是myisam存儲(chǔ)引擎創(chuàng)建單列索引mysql create index index10_addr on example3(address(4);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0此表中address字段的長(zhǎng)度是20,這里只查詢(xún)4字節(jié),不需要全部查詢(xún)創(chuàng)建多列索引mysql create index index11_na on example4(name,address);query ok, 0 rows affected (0.16 sec)r

15、ecords: 0 duplicates: 0 warnings: 0索引創(chuàng)建好之后,查詢(xún)中必須有name字段才能使用創(chuàng)建空間索引mysql create spatial index index12_line on example5(space);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0這里需要注意存儲(chǔ)引擎是myisam,還有空間數(shù)據(jù)類(lèi)型用alter table語(yǔ)句來(lái)創(chuàng)建索引創(chuàng)建普通索引mysql alter table example6 add index index13_n(name(2

16、0);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0創(chuàng)建唯一性索引mysql alter table example7 add unique index index14_id(id);query ok, 0 rows affected (0.20 sec)records: 0 duplicates: 0 warnings: 0創(chuàng)建全文索引mysql alter table example8 add fulltext index index15_info(info);query ok, 0 rows

17、 affected (0.08 sec)records: 0 duplicates: 0 warnings: 0創(chuàng)建單列索引mysql alter table example9 add index index16_addr(address(4);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0創(chuàng)建多列索引mysql alter table example10 add index index17_in(id,name);query ok, 0 rows affected (0.16 sec)recor

18、ds: 0 duplicates: 0 warnings: 0創(chuàng)建空間索引mysql alter table example11 add spatial index index18_space(space);query ok, 0 rows affected (0.06 sec)records: 0 duplicates: 0 warnings: 0到此,三種操作方式,每種索引類(lèi)別的建立就都列舉了對(duì)于索引,重要的是理解索引的概念,明白索引的種類(lèi)更多的是自己的使用經(jīng)驗(yàn)最后來(lái)看看索引的刪除刪除索引mysql drop index index18_space on example11;query o

19、k, 0 rows affected (0.08 sec)records: 0 duplicates: 0 warnings: 0這里是剛剛創(chuàng)建的一條索引其中index18_space是索引名,example11是表名基本查詢(xún)多字段查詢(xún): select id,name,birth from student;所有字段查詢(xún):select * from student;where指定查詢(xún)select * from student where id = 901;in指定集合查詢(xún)select * from student where birth in(1988,1990);not in 非范圍查詢(xún):se

20、lect * from student where birth not in(1990,1988);between and指定范圍查詢(xún):select * from student where bitrth between 1986 and 1988;not between and不在指定范圍的查詢(xún)select * from student where id not between 904 and 906;like字符串匹配查詢(xún)select * from student where name like ;not like不匹配查詢(xún)select * from student where name

21、not like張;null查詢(xún)select * from student where address is null;and多條件查詢(xún)select * from student where name like 張 and birth1985;or多條件查詢(xún)select * from student where id = 905 or birth=1988;distinct查詢(xún)結(jié)果不重復(fù)select distinct sex from student;order by查詢(xún)結(jié)果排序select * from order by birth;group by分組查詢(xún)select sex,group_

22、contact(name)from student group by sex;select sex,count(name)from student group by sex;正則表達(dá)式查詢(xún)select * from student where birth regexp1988|1990;limit限制查詢(xún)結(jié)果數(shù)量select * from student limit 2;函數(shù)查詢(xún)select count(*)from score;sum()求和函數(shù)select sum(grade)from score;avg()求平均值函數(shù)select avg(grade)from score where c_name=計(jì)算機(jī);max()求最大值函數(shù)select c_name,max(grade)from score where c_na

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論