共計(jì) 3365 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。
MySQL 常用基本操作都有哪些,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
MySQL 常用操作基本操作,以下都是 MySQL5.0 下測(cè)試通過首先說明下,記住在每個(gè)命令結(jié)束時(shí)加上;(分號(hào))
1. 導(dǎo)出整個(gè)數(shù)據(jù)庫
mysqldump -u 用戶名 -p –default-character-set=latin1 數(shù)據(jù)庫名 導(dǎo)出的文件名(數(shù)據(jù)庫默認(rèn)編碼是 latin1)
mysqldump -u wcnc -p smgp_apps_wcnc wcnc.sql
2. 導(dǎo)出一個(gè)表
mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 表名 導(dǎo)出的文件名
mysqldump -u wcnc -p smgp_apps_wcnc users wcnc_users.sql
3. 導(dǎo)出一個(gè)數(shù)據(jù)庫結(jié)構(gòu)
mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc d:wcnc_db.sql
-d 沒有數(shù)據(jù) –add-drop-table 在每個(gè) create 語句之前增加一個(gè) drop table
4. 導(dǎo)入數(shù)據(jù)庫
常用 source 命令
進(jìn)入 mysql 數(shù)據(jù)庫控制臺(tái),
如 mysql -u root -p
mysql use 數(shù)據(jù)庫
然后使用 source 命令,后面參數(shù)為腳本文件(如這里用到的.sql)
mysql source d:wcnc_db.sql
一、啟動(dòng)與退出
1、進(jìn)入 MySQL:?jiǎn)?dòng) MySQL Command Line Client(MySQL 的 DOS 界面),直接輸入安裝時(shí)的密碼即可。此時(shí)的提示符是:mysql
2、退出 MySQL:quit 或 exit
二、庫操作
1、、創(chuàng)建數(shù)據(jù)庫
命令:create database 數(shù)據(jù)庫名
例如:建立一個(gè)名為 xhkdb 的數(shù)據(jù)庫
mysql create database xhkdb;
2、顯示所有的數(shù)據(jù)庫
命令:show databases(注意:最后有個(gè) s)
mysql show databases;
3、刪除數(shù)據(jù)庫
命令:drop database 數(shù)據(jù)庫名
例如:刪除名為 xhkdb 的數(shù)據(jù)庫
mysql drop database xhkdb;
4、連接數(shù)據(jù)庫
命令:use 數(shù)據(jù)庫名
例如:如果 xhkdb 數(shù)據(jù)庫存在,嘗試存取它:
mysql use xhkdb;
屏幕提示:Database changed
5、當(dāng)前選擇(連接)的數(shù)據(jù)庫
mysql select database();
6、當(dāng)前數(shù)據(jù)庫包含的表信息:
mysql show tables;(注意:最后有個(gè) s)
三、表操作,操作之前應(yīng)連接某個(gè)數(shù)據(jù)庫
1、建表
命令:create table 表名 (字段名 1 類型 1 [,.. 字段名 n 類型 n
mysql create table MyClass(
id int(4) not null primary key auto_increment,
name char(20) not null,
sex int(4) not null default 0 ,
degree double(16,2));
2、獲取表結(jié)構(gòu)
命令:desc 表名,或者 show columns from 表名
mysql DESCRIBE MyClass
mysql desc MyClass;
mysql show columns from MyClass;
3、刪除表
命令:drop table 表名
例如:刪除表名為 MyClass 的表
mysql drop table MyClass;
4、插入數(shù)據(jù)
命令:insert into 表名 [(字段名 1 [,.. 字段名 n])] values (值 1)[, ( 值 n)]
例如,往表 MyClass 中插入二條記錄, 這二條記錄表示:編號(hào)為 1 的名為 Tom 的成績(jī)?yōu)?96.45, 編號(hào)為 2 的名為 Joan 的成績(jī)?yōu)?82.99,編號(hào)為 3 的名為 Wang 的成績(jī)?yōu)?96.5.
mysql insert into MyClass values(1, Tom ,96.45),(2, Joan ,82.99), (2, Wang , 96.59);
5、查詢表中的數(shù)據(jù)
1)、查詢所有行
命令:select 字段 1,字段 2,… from 表名 where 表達(dá)式
例如:查看表 MyClass 中所有數(shù)據(jù)
mysql select * from MyClass;
2)、查詢前幾行數(shù)據(jù)
例如:查看表 MyClass 中前 2 行數(shù)據(jù)
mysql select * from MyClass order by id limit 0,2;
6、刪除表中數(shù)據(jù)
命令:delete from 表名 where 表達(dá)式
例如:刪除表 MyClass 中編號(hào)為 1 的記錄
mysql delete from MyClass where id=1;
7、修改表中數(shù)據(jù):update 表名 set 字段 = 新值,… where 條件
mysql update MyClass set name= Mary where id=1;
7、在表中增加字段:
命令:alter table 表名 add 字段 類型 其他;
例如:在表 MyClass 中添加了一個(gè)字段 passtest,類型為 int(4),默認(rèn)值為 0
mysql alter table MyClass add passtest int(4) default 0
8、更改表名:
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改為 YouClass
mysql rename table MyClass to YouClass;
更新字段內(nèi)容
update 表名 set 字段名 = 新內(nèi)容
update 表名 set 字段名 = replace(字段名, 舊內(nèi)容 , 新內(nèi)容
文章前面加入 4 個(gè)空格
update article set content=concat(,content);
字段類型
1.INT[(M)] 型:正常大小整數(shù)類型
2.DOUBLE[(M,D)] [ZEROFILL] 型:正常大小(雙精密) 浮點(diǎn)數(shù)字類型
3.DATE 日期類型:支持的范圍是 1000-01-01 到 9999-12-31。MySQL 以 YYYY-MM-DD 格式來顯示 DATE 值,但是允許你使用字符串或數(shù)字把值賦給 DATE 列
4.CHAR(M) 型:定長(zhǎng)字符串類型,當(dāng)存儲(chǔ)時(shí),總是是用空格填滿右邊到指定的長(zhǎng)度
5.BLOB TEXT 類型,最大長(zhǎng)度為 65535(2^16-1)個(gè)字符。
6.VARCHAR 型:變長(zhǎng)字符串類型
5. 導(dǎo)入數(shù)據(jù)庫表
(1)創(chuàng)建.sql 文件
(2)先產(chǎn)生一個(gè)庫如 auction.c:mysqlbin mysqladmin -u root -p creat auction,會(huì)提示輸入密碼,然后成功創(chuàng)建。
(2)導(dǎo)入 auction.sql 文件
c:mysqlbin mysql -u root -p auction auction.sql。
通過以上操作,就可以創(chuàng)建了一個(gè)數(shù)據(jù)庫 auction 以及其中的一個(gè)表 auction。
6.修改數(shù)據(jù)庫
(1)在 mysql 的表中增加字段:
alter table dbname add column userid int(11) not null primary key auto_increment;
這樣,就在表 dbname 中添加了一個(gè)字段 userid,類型為 int(11)。
7.mysql 數(shù)據(jù)庫的授權(quán)
mysql grant select,insert,delete,create,drop
on *.* (或 test.*/user.*/..)
to 用戶名 @localhost
identified by 密碼;
如:新建一個(gè)用戶帳號(hào)以便可以訪問數(shù)據(jù)庫,需要進(jìn)行如下操作:
mysql grant usage
– ON test.*
– TO testuser@localhost;
Query OK, 0 rows affected (0.15 sec)
此后就創(chuàng)建了一個(gè)新用戶叫:testuser,這個(gè)用戶只能從 localhost 連接到數(shù)據(jù)庫并可以連接到 test 數(shù)據(jù)庫。下一步,我們必須指定 testuser 這個(gè)用戶可以執(zhí)行哪些操作:
mysql GRANT select, insert, delete,update
– ON test.*
– TO testuser@localhost;
Query OK, 0 rows affected (0.00 sec)
此操作使 testuser 能夠在每一個(gè) test 數(shù)據(jù)庫中的表執(zhí)行 SELECT,INSERT 和 DELETE 以及 UPDATE 查詢操作。現(xiàn)在我們結(jié)束操作并退出 MySQL 客戶程序:
mysql exit
Bye9!
看完上述內(nèi)容,你們掌握 MySQL 常用基本操作都有哪些的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!