mysql常用語句和應(yīng)用例子_第1頁
mysql常用語句和應(yīng)用例子_第2頁
mysql常用語句和應(yīng)用例子_第3頁
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡介

1、1 一,連接mysql 二, mysql 管理與授權(quán)三,數(shù)據(jù)庫簡單操作四, 數(shù)據(jù)庫備份五,后記一,連接mysql 格式: mysql -h 遠(yuǎn)程主機地址-u 用戶名-p 回車輸入密碼進(jìn)入:mysql -u root -p 回車enter password: ,輸入密碼就可以進(jìn)入mysql 進(jìn)入了退出命令 :exit 或者 ctrl+d 二, mysql 管理與授權(quán)1.修改密碼:格式: mysqladmin -u 用戶名-p 舊密碼password 新密碼2.增加新用戶 : grant create,select,update.(授予相關(guān)的操作權(quán)限) -on 數(shù)據(jù)庫 .* - to 用戶名 登錄

2、主機identified by 密碼 操作實例:給 root 用戶添加密碼: # mysqladmin -u root password 52netseek 因為開始root 沒有密碼 ,所以 -p 舊密碼一項可以省略. 登陸測試:# mysql -u root -p 回車輸入密碼 ,成功登陸 . 將原有的mysql 管理登陸密碼52netseek改為 52china. # mysqladmin -u root -p 52netseek password 52china 創(chuàng)建數(shù)據(jù)庫添加用戶并授予相應(yīng)的權(quán)限: mysql create database phpbb; query ok, 1 ro

3、w affected (0.02 sec) mysql use phpbb; database changed mysql grant create,select,update,insert,delete,alter - on phpbb.* - to phpbbrootlocalhost identified by 52netseek; query ok, 0 rows affected (0.00 sec) 授予所有的權(quán)限: grant all privileges on bbs.* to bbsrootlocalhost identified by 52netseek 回收權(quán)限:revo

4、ke create,select,update,insert,delete,alter on phpbb.* from phpbbrootlocalhost identified by 52netseek; 完全將 phpbbroot 這個用戶刪除: use mysql delete from user where user=phpbbroot and host=localhost; flush privileges; 刷新數(shù)據(jù)庫三,數(shù)據(jù)庫簡單操作1.顯示數(shù)據(jù)庫列表: show databases; mysql test 2.使其成為當(dāng)前操作數(shù)據(jù)庫use mysql; 打開數(shù)據(jù)庫 . show

5、 tables; 顯示 mysql 數(shù)據(jù)庫中的數(shù)據(jù)表. 3.顯示數(shù)據(jù)表的表結(jié)構(gòu): describe 表名 ; describe user; 顯示 user表的表結(jié)構(gòu):4.創(chuàng)建數(shù)據(jù)庫 ,建表create database 數(shù)據(jù)庫名 ; use 數(shù)據(jù)庫名;create table 表名 (字段設(shè)定列表) 5.刪除數(shù)據(jù)庫,冊除表drop database 數(shù)據(jù)庫名 ; drop table 表名 ; 6.顯示表中的記錄; select * from 表名 ; 7.修改數(shù)據(jù)庫結(jié)構(gòu): 增加字段:alter table dbname add column 修改字段:alter table dbname c

6、hange 刪除字段 : 2 alter table dbname drop column 實例操作:create database office; use office; mysql create table personal( - member_no char(5) not null, - name char(, - birthday date, - exam_score tinyint, - primary key(member_no) - ); query ok, 0 rows affected (0.01 sec) desc personal; 顯示表結(jié)構(gòu):+-+-+-+-+-+-+

7、 | field | type | null | key | default | extra | +-+-+-+-+-+-+ | member_no | char(5) | | pri | | | | name | char( | yes | | null | | | birthday | date | yes | | null | | | exam_score | tinyint(4) | yes | | null | | +-+-+-+-+-+-+ 4 rows in set (0.00 sec) insert into personal values (001,netseek,1983-

8、03-15,95); insert into personal values (002,heihei,1982-02-24,90); insert into personal values (003,gogo,1985-05-21,85); insert into personal values (004,haha,1984-02-25,84); insert into personal values (005,linlin,1982-04-28,85); 您正在看的mysql教程是 :mysql數(shù)據(jù)庫學(xué)習(xí)筆記。insert into personal values (006,xinxin,1

9、985-03-15,75); mysql select * from personal; +-+-+-+-+ | member_no | name | birthday | exam_score | +-+-+-+-+ | 001 | netseek | 1983-03-15 | 95 | | 002 | heihei | 1982-02-24 | 90 | | 003 | gogo | 1985-05-21 | 85 | | 004 | haha | 1984-02-25 | 84 | | 005 | linlin | 1982-04-28 | 85 | | 006 | xinxin | 1

10、985-03-15 | 75 | +-+-+-+-+ 修改數(shù)據(jù)庫表:要求 : 在 birthday 這后增加一個為height 的字段 ,數(shù)據(jù)類型為 tinyint. 將字段 exam_score 改名為 scores,數(shù)據(jù)類型不變alter table personal -add column height tinyint after birthday, -change column exam_score scores tinyint; mysql select * from personal; +-+-+-+-+-+ | member_no | name | birthday | heig

11、ht | scores | +-+-+-+-+-+ | 001 | netseek | 1983-03-15 | null | 95 | | 002 | heihei | 1982-02-24 | null | 90 | | 003 | gogo | 1985-05-21 | null | 85 | | 004 | haha | 1984-02-25 | null | 84 | | 005 | linlin | 1982-04-28 | null | 85 | | 006 | xinxin | 1985-03-15 | null | 75 | +-+-+-+-+-+ 給表中插入數(shù)據(jù):updat

12、e personal set scores=95+5 where name=netseek; select scores from personal where name=netseek; +-+ | scores | +-+ | 100 | +-+ 刪除表名字為gogo所有的信息中的的: delete from personal where name=gogo; 冊除數(shù)據(jù)庫中的表: mysqldrop table if exists personal; 三,數(shù)據(jù)庫的導(dǎo)入與導(dǎo)出導(dǎo)出:使用 select into outfile filename 語句使用 mysqldump 實用程序使用 se

13、lect into outfile filename 語句1.只能處理單個表,輸出文件只有數(shù)據(jù),沒有表結(jié)構(gòu)我 們要將office, 其 中有一個表為personal,現(xiàn) 在要 把personal 卸成文本文件out.txt: use office; select * from personal into outfile out.txt; 可以看在/var/lib/mysql/office/ 目錄下有out.txt 3 select * from personal into outfile ./out.txt; 可以看在out.txt 在/var/lib/mysql/ 目錄下用out.txt 2

14、.使用 mysqldump 實用程序 (可以輕松處理多個表) # cd /var/lib/mysql 導(dǎo)出建立相關(guān)表的建表命令和插入指令# mysqldump bbs bbs.sql 將數(shù)據(jù)庫 bbs導(dǎo)入到 bbs.sql 中如果要將bbs.sql 導(dǎo)入數(shù)據(jù)庫可以使用: mysql create database bbstest; 先建立一個名為office 的數(shù)據(jù)庫 . # mysql bbstest bbscreate.sql 只想導(dǎo)出插入數(shù)據(jù)的sql 指令:# mysqldump -t bbs bbsinsert.sql 同時導(dǎo)出數(shù)據(jù)庫中建表指令和表中的數(shù)據(jù):# mysqldump -t

15、./ bbs cdb_admingroups (其中 ./表示當(dāng)前目錄 ,cdb_admingroups 為 bbs數(shù)據(jù)庫其中的一個表) #ls cdb_admingroups.sql 導(dǎo)出了建表指令cdb_admingroups.txt 導(dǎo)出了表中的數(shù)據(jù)導(dǎo)入:從文件中加載數(shù)據(jù)庫: mysqlload data infile /tmp/name.txt into table names; mysqlselect * from names; 四,數(shù)據(jù)庫備份1.手動拷貝備份: mysql數(shù)據(jù)庫的文件保存在目錄/var/lib/mysql中,數(shù)據(jù)庫為每個庫建立一個目錄,所有的數(shù)據(jù)庫文件都在這些目錄中

16、 . rootlinuxhero mysql#ls rootlinuxhero mysql#servcie mysqld stop 先停止數(shù)據(jù)庫bbs mysql mysql.sock phpbb test office 顯示其中的數(shù)據(jù)庫. 如果我們要將現(xiàn)在的數(shù)據(jù)庫目錄備份為mysql.bak . rootlinuxhero lib# cp -rf mysql mysql.bak 如果數(shù)據(jù)庫遭到了破壞,現(xiàn)在要將數(shù)據(jù)庫恢復(fù):rootlinuxhero lib# cp -rf mysql.bak/* mysql 恢復(fù)數(shù)據(jù)庫以后,var/lib/mysql 中的文件已改變了,要更改文件的所屬權(quán)限必須

17、改變mysql數(shù)據(jù)庫的用戶讀寫權(quán)限。所以我們得啟動和運行mysql,并登陸數(shù)據(jù)庫: rootlinuxhero lib# /etc/init.d/mysqld start rootlinuxhero lib# mysql 您正在看的mysql 教程是 :mysql 數(shù)據(jù)庫學(xué)習(xí)筆記。-u root -p enter password:輸入密碼成功登陸. mysql show databses; 2.利用 mysqldump 來備份數(shù)據(jù)庫rootlinuxhero mysql# mysqldump -opt bbs -u root -p bbs.sql enter password: 注:-opt 添加備份的其它選項,bb 為其中一個數(shù)據(jù)庫名, 上面的意思是: 使用重定向輸出將備份寫入到文件bb.sql中. rootlinuxhero mysql #less bbs.sql 如果要恢復(fù)bb 這個數(shù)據(jù)庫,則進(jìn)行如下操作: ro

溫馨提示

  • 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論