MYSQL命令列常用命令

來源:互聯網
上載者:User
MySQL常用操作基本操作,以下都是MySQL5.0下測試通過首先說明下,記住在每個命令結束時加上;(分號)
1.匯出整個資料庫
mysqldump -u 使用者名稱 -p --default-character-set=latin1 資料庫名 > 匯出的檔案名稱(資料庫預設編碼是latin1)
mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
2.匯出一個表
mysqldump -u 使用者名稱 -p 資料庫名 表名> 匯出的檔案名稱
mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
3.匯出一個資料庫結構
mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:wcnc_db.sql
-d 沒有資料 –add-drop-table 在每個create語句之前增加一個drop table
4.匯入資料庫
常用source 命令
進入mysql資料庫控制台,
如mysql -u root -p
mysql>use 資料庫
然後使用source命令,後面參數為指令檔(如這裡用到的.sql)
mysql>source d:wcnc_db.sql

一、啟動與退出
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 ( [,.. ]);

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、擷取表結構
命令: 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 [( [,.. ])] 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 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)先產生一個庫如auction.c:mysqlbin>mysqladmin -u root -p creat auction,會提示輸入密碼,然後成功建立。
   (2)匯入auction.sql檔案
   c:mysqlbin>mysql -u root -p auction 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), sex CHAR(1));
6:顯示表的結構:
mysql> DESCRIBE MYTABLE;
7:往表中加入記錄
mysql> insert into MYTABLE values ("hyq","M");
8:用文本方式將資料裝入資料庫表中(例如D:/mysql.txt)
mysql> LOAD DATA LOCAL INFILE "D:/mysql.txt" INTO TABLE MYTABLE;
9:匯入.sql檔案命令(例如D:/mysql.sql)
mysql>use database;
mysql>source d:/mysql.sql;
10:刪除表
mysql>drop TABLE MYTABLE;
11:清空表
mysql>delete from MYTABLE;
12:更新表中資料
mysql>update MYTABLE set sex="f" where name=''hyq'';

以下是無意中在網路看到的使用MySql的管理心得,
摘自:http://www1.xjtusky.com/article/htmldata/2004_12/3/57/article_1060_1.html

在windows中MySql以服務形式存在,在使用前應確保此服務已經啟動,未啟動可用net start mysql命令啟動。而Linux中啟動時可用“/etc/rc.d/init.d/mysqld start"命令,注意啟動者應具有管理員權限。

來源:http://www.diybl.com/course/7_databases/mysql/Mysqljs/200855/113189.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.