1. 程式人生 > >MySQL 操作命令

MySQL 操作命令

連線資料庫

mysql -u使用者名稱 -p密碼 -h主機地址

1.連線資料庫 命令: use <資料庫名> 2.查看錶的引擎型別等狀態資訊 SHOW TABLE STATUS [FROMdb_name] [LIKE 'pattern'] 3.當前資料庫包含的表資訊 show tables; 4.檢視當前使用的資料庫 select database(); 5.刪除資料庫 命令:drop database <資料庫名>; 6.顯示所有的資料庫 命令:show databases;(注意:最後有個s) 7.建立資料庫 命令:create database <資料庫名>; 8.建立表 create table <表名> (<欄位名1> <型別1> [,..<欄位名n> <型別n>]); 補充:根據已有的表建立新表。 8.1 create table tab_new like tab_old; (只有表結構) 8.2 create table tab_new as select * from tab_old; (既包含表結構,又包含表資料) 9.獲取表結構 命令: desc 表名; or show columns from 表名; 10. 刪除表 命令:drop table <表名>; 11.更改表名 命令:rename table 原表名 to 新表名; 12.在表中增加欄位 命令:alter table 表名 add 欄位 型別 其他; 例如:alter table myclass add passtest int(4) default '0'; 13.插入資料 命令:insert into <表名> [( <欄位名1>[,..<欄位名n > ])] values ( 值1 )[, ( 值n )]; 例如: insert into myclass (id, name, sex, degree, passtest) values(1, 'david', 1, 80.56, 78); insert into myclass values(2, 'sandy', 0, 100, 90); insert into myclass (id, name, sex, degree) values(3, 'renee', 0, 90.34); 14.匯出整個資料庫 命令:mysqldump -u 使用者名稱 -p 資料庫名 > 匯出的檔名 15.匯出一個表 命令:mysqldump -u 使用者名稱 -p 資料庫名 表名> 匯出的檔名 16.匯出一個數據庫結構 命令:mysqldump -u root -p -d --add-drop-table test > test_db.sql -d 沒有資料 --add-drop-table 在每個create 語句之前增加一個drop table 17.常用source 命令 source "路徑名"+/mytest_emp_dept.sql show open tables; 能夠檢視當前有那些表是開啟的。In_use列表示有多少執行緒正在使用某張表,Name_locked表示表名是否被鎖,這一般發生在Drop或Rename命令操作這張表時。 所以這條命令不能幫助解答我們常見的問題:當前某張表是否有死鎖,誰擁有表上的這個鎖等。 show open tables from database; show OPEN TABLES where In_use > 0;

SELECT * FROM information_schema.`PROCESSLIST`; 新增索引 ALTER TABLE t_cms_home ADD INDEX IDX_SID(C_SID);

查看錶索引索引 show INDEX from t_cms_home; show keys from t_u_basic; SHOW PROCESSLIST顯示哪些執行緒正在執行 show processlist;只列出前100條,如果想全列出請使用show full processlist; 檢視伺服器狀態。 show status like '%lock%'; 日誌:二進位制檔案記錄 show variables like 'log_bin'; 記錄二進位制資料的檔案具體資訊 show master status; explain命令顯示了mysql如何使用索引來處理select語句以及連線表 EXPLAIN的使用方法: 在select語句前加上explain就可以了。 顯示系統變數的名稱和值 show variables; 顯示伺服器所支援的不同許可權 show privileges; 顯示create database 語句是否能夠建立指定的資料庫 show create database database_name; 顯示create database 語句是否能夠建立指定的資料表 show create table table_name; 顯示安裝以後可用的儲存引擎和預設引擎。 show engies; 顯示innoDB儲存引擎的狀態 show innodb status; 顯示BDB儲存引擎的日誌 show logs; 顯示最後一個執行的語句所產生的錯誤、警告和通知 show warnings; 只顯示最後一個執行語句所產生的錯誤 show errors 18 說明:拷貝表(拷貝資料,源表名:a 目標表名:b) (Access可用) insert into b(a, b, c) select d,e,f from b;

19.說明:跨資料庫之間表的拷貝(具體資料使用絕對路徑) (Access可用) insert into b(a, b, c) select d,e,f from b in ‘具體資料庫' where 條件 例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where.. 20、說明:between的用法,between限制查詢資料範圍時包括了邊界值,not between不包括 select * from table1 where time between time1 and time2 select a,b,c, from table1 where a not between 數值1 and 數值2 21、說明:一條sql 語句搞定資料庫分頁 select top 10 b.* from (select top 20 主鍵欄位,排序欄位 from 表名 order by 排序欄位 desc) a,表名 b where b.主鍵欄位 = a.主鍵欄位 order by a.排序欄位 22、 select * from table1, table2 where table1.id *= table2.id -------- 左外部連線, table1 中有的而 table2 中沒有得以 null表示 table1.id =* table2.id -------- 右外部連線 23、delete from table_name where Stockid = 3 truncate table_name ----------- 刪除表中所有行,仍保持表的完整性 drop table table_name --------------- 完全刪除表