mysql常用操作基本操作,以下都是mysql5.0下測試通過首先說明下,記住在每個命令結束時加上;(分號)
1.匯出整個資料庫
mysqldump -u 使用者名稱 -p --default-character-set=latin1 資料庫名 > 匯出的檔名(資料庫預設編碼是latin1)
mysqldump -u wcnc -p smgp_apps_wcnc >
2.匯出乙個表
mysqldump -u 使用者名稱 -p 資料庫名錶名》 匯出的檔名
mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_
3.匯出乙個資料庫結構
mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:wcnc_
-d 沒有資料 –add-drop-table 在每個create語句之前增加乙個drop table
4.匯入資料庫
常用source 命令
進入mysql資料庫控制台,
如mysql -u root -p
mysql>use 資料庫
然後使用source命令,後面引數為指令碼檔案(如這裡用到的.sql)
mysql>source d:wcnc_
一、啟動與退出
1、進入mysql:啟動mysql command line client(mysql的dos介面),直接輸入安裝時的密碼即可。此時的提示符是:mysql>
2、退出mysql:quit或exit
二、庫操作
1、、建立資料庫
命令:create database 《資料庫名》
例如:建立乙個名為xhkdb的資料庫
mysql> create database xhkdb;
2、顯示所有的資料庫
命令:show databases (注意:最後有個s)
mysql> show databases;
3、刪除資料庫
命令:drop database 《資料庫名》
例如:刪除名為 xhkdb的資料庫
mysql> drop database xhkdb;
4、連線資料庫
命令: use 《資料庫名》
例如:如果xhkdb資料庫存在,嘗試訪問它:
mysql> use xhkdb;
螢幕提示:database changed
5、當前選擇(連線)的資料庫
mysql> select database();
6、當前資料庫包含的表資訊:
mysql> show tables; (注意:最後有個s)
三、表操作,操作之前應連線某個資料庫
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,
> *** int(4) not null default '0',
> degree double(16,2));
2、獲取表結構
命令: desc 表名,或者show columns from 表名
mysql>describe myclass
mysql> desc myclass;
mysql> show columns from myclass;
3、刪除表
命令:drop table 《表名》
例如:刪除表名為 myclass 的表
mysql> drop table myclass;
4、插入資料
命令:insert into 《表名》 [( 《欄位名1>[,..《欄位名n > ])] values ( 值1 )[, ( 值n )]
例如,往表 myclass中插入二條記錄, 這二條記錄表示:編號為1的名為tom的成績為96.45, 編號為2 的名為joan 的成績為82.
99,編號為3 的名為wang 的成績為96.5.
mysql> insert into myclass values(1,'tom',96.45),(2,'joan',82.99), (2,'wang', 96.59);
5、查詢表中的資料
1)、查詢所有行
命令: select 《欄位1,欄位2,...> from < 表名 > where < 表示式 >
例如:檢視表 myclass 中所有資料
mysql> select * from myclass;
2)、查詢前幾行資料
例如:檢視表 myclass 中前2行資料
mysql> select * from myclass order by id limit 0,2;
6、刪除表中資料
命令:delete from 表名 where 表示式
例如:刪除表 myclass中編號為1 的記錄
mysql> delete from myclass where id=1;
7、修改表中資料:update 表名 set 字段=新值,… where 條件
mysql> update myclass set name='mary' where id=1;
7、在表中增加字段:
命令:alter table 表名 add欄位型別其他;
例如:在表myclass中新增了乙個欄位passtest,型別為int(4),預設值為0
mysql> alter table myclass add passtest int(4) default '0'
8、更改表名:
命令:rename table 原表名 to 新錶名;
例如:在表myclass名字更改為youclass
mysql> rename table myclass to youclass;
更新字段內容
update 表名 set 欄位名 = 新內容
update 表名 set 欄位名 = replace(欄位名,'舊內容','新內容');
文章前面加入4個空格
update article set content=concat(' ',content);
字段型別
1.int[(m)] 型: 正常大小整數型別
2.double[(m,d)] [zerofill] 型: 正常大小(雙精密)浮點數字型別
3.date 日期型別:支援的範圍是1000-01-01到9999-12-31。mysql以yyyy-mm-dd格式來顯示date值,但是允許你使用字串或數字把值賦給date列
4.char(m) 型:定長字串型別,當儲存時,總是是用空格填滿右邊到指定的長度
5.blob text型別,最大長度為65535(2^16-1)個字元。
6.varchar型:變長字串型別
5.匯入資料庫表
(1)建立.sql檔案
(2)先產生乙個庫如》mysqladmin -u root -p creat auction,會提示輸入密碼,然後成功建立。
(2)匯入檔案
c:mysqlbin>mysql -u root -p auction <
通過以上操作,就可以建立了乙個資料庫auction以及其中的乙個表auction。
6.修改資料庫
(1)在mysql的表中增加字段:
alter table dbname add column userid int(11) not null primary key auto_increment;
這樣,就在表dbname中新增了乙個欄位userid,型別為int(11)。
7.mysql資料庫的授權
mysql>grant select,insert,delete,create,drop
on *.* (或test.*/user.*/..)
to 使用者名稱@localhost
identified by '密碼';
如:新建乙個使用者帳號以便可以訪問資料庫,需要進行如下操作:
mysql> grant usage
-> on test.*
-> to testuser@localhost;
query ok, 0 rows affected (0.15 sec)
此後就建立了乙個新使用者叫:testuser,這個使用者只能從localhost連線到資料庫並可以連線到test 資料庫。下一步,我們必須指定testuser這個使用者可以執行哪些操作:
mysql> grant select, insert, delete,update
-> on test.*
-> to testuser@localhost;
query ok, 0 rows affected (0.00 sec)
此操作使testuser能夠在每乙個test資料庫中的表執行select,insert和delete以及update查詢操作。現在我們結束操作並退出mysql客戶程式:
mysql> exit
bye9!
1:使用show語句找出在伺服器上當前存在什麼資料庫:
mysql> show databases;
2:2、建立乙個資料庫mysqldata
mysql> create database mysqldata;
3:選擇你所建立的資料庫
mysql> use mysqldata; (按回車鍵出現database changed 時說明操作成功!)
4:檢視現在的資料庫中存在什麼表
mysql> show tables;
5:建立乙個資料庫表
mysql> create table mytable (name varchar(20), *** char(1));
6:顯示表的結構:
mysql> describe mytable;
7:往表中加入記錄
mysql> insert into mytable values ("hyq","m");
MySql常用命令總結
mysql常用操作基本操作,以下都是mysql5.0下測試通過首先說明下,記住在每個命令結束時加上 分號 1.匯出整個資料庫 mysqldump u 使用者名稱 p default character set latin1 資料庫名 匯出的檔名 資料庫預設編碼是latin1 mysqldump u ...
MySql常用命令總結
mysql常用操作基本操作,以下都是mysql5.0下測試通過首先說明下,記住在每個命令結束時加上 分號 1.匯出整個資料庫 mysqldump u 使用者名稱 p default character set latin1 資料庫名 匯出的檔名 資料庫預設編碼是latin1 mysqldump u ...
MySql常用命令總結
mysql常用操作基本操作,以下都是mysql5.0下測試通過首先說明下,記住在每個命令結束時加上 分號 1.匯出整個資料庫 mysqldump u 使用者名稱 p default character set latin1 資料庫名 匯出的檔名 資料庫預設編碼是latin1 mysqldump u ...