linux命令行操作mysql數(shù)據(jù)庫明細(xì)
連接數(shù)據(jù)庫==》
mysql -uroot -p
輸入root密碼
進(jìn)入mysql操作后 下面的命令不要忘了最后結(jié)尾的;
1.選擇數(shù)據(jù)庫
命令: use <數(shù)據(jù)庫名>
2.查看表的引擎類型等狀態(tài)信息
SHOW TABLE STATUS [FROMdb_name] [LIKE 'pattern']
3.當(dāng)前數(shù)據(jù)庫包含的表信息
show tables;
4.查看當(dāng)前使用的數(shù)據(jù)庫
select database();
5.刪除數(shù)據(jù)庫
命令:drop database <數(shù)據(jù)庫名>;
6.顯示所有的數(shù)據(jù)庫
命令:show databases;(注意:最后有個s)
7.創(chuàng)建數(shù)據(jù)庫
命令:create database <數(shù)據(jù)庫名>;
8.建立表
create table <表名> (<字段名1> <類型1> [,..<字段名n> <類型n>]);
補充:根據(jù)已有的表創(chuàng)建新表。
8.1 create table tab_new like tab_old; (只有表結(jié)構(gòu))
8.2 create table tab_new as select * from tab_old; (既包含表結(jié)構(gòu),又包含表數(shù)據(jù))
9.獲取表結(jié)構(gòu)
命令:
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.插入數(shù)據(jù)
命令: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.導(dǎo)出整個數(shù)據(jù)庫
命令:mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 > 導(dǎo)出的文件名
15.導(dǎo)出一個表
命令:mysqldump -u 用戶名 -p 數(shù)據(jù)庫名 表名> 導(dǎo)出的文件名
16.導(dǎo)出一個數(shù)據(jù)庫結(jié)構(gòu)
命令:mysqldump -u root -p -d --add-drop-table test > test_db.sql
-d 沒有數(shù)據(jù) --add-drop-table 在每個create 語句之前增加一個drop table
17.常用source 命令
source "路徑名"+/mytest_emp_dept.sql
show open tables;
能夠查看當(dāng)前有那些表是打開的。In_use列表示有多少線程正在使用某張表,Name_locked表示表名是否被鎖,這一般發(fā)生在Drop或Rename命令操作這張表時。
所以這條命令不能幫助解答我們常見的問題:當(dāng)前某張表是否有死鎖,誰擁有表上的這個鎖等。
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;
查看服務(wù)器狀態(tài)。
show status like '%lock%';
日志:二進(jìn)制文件記錄
show variables like 'log_bin';
記錄二進(jìn)制數(shù)據(jù)的文件具體信息
show master status;
explain命令顯示了mysql如何使用索引來處理select語句以及連接表
EXPLAIN的使用方法:
在select語句前加上explain就可以了。
顯示系統(tǒng)變量的名稱和值
show variables;
顯示服務(wù)器所支持的不同權(quán)限
show privileges;
顯示create database 語句是否能夠創(chuàng)建指定的數(shù)據(jù)庫
show create database database_name;
顯示create database 語句是否能夠創(chuàng)建指定的數(shù)據(jù)表
show create table table_name;
顯示安裝以后可用的存儲引擎和默認(rèn)引擎。
show engies;
顯示innoDB存儲引擎的狀態(tài)
show innodb status;
顯示BDB存儲引擎的日志
show logs;
顯示最后一個執(zhí)行的語句所產(chǎn)生的錯誤、警告和通知
show warnings;
只顯示最后一個執(zhí)行語句所產(chǎn)生的錯誤
show errors
18 說明:拷貝表(拷貝數(shù)據(jù),源表名:a 目標(biāo)表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;
19.說明:跨數(shù)據(jù)庫之間表的拷貝(具體數(shù)據(jù)使用絕對路徑) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘具體數(shù)據(jù)庫' where 條件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
20、說明:between的用法,between限制查詢數(shù)據(jù)范圍時包括了邊界值,not between不包括
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 數(shù)值1 and 數(shù)值2
21、說明:一條sql 語句搞定數(shù)據(jù)庫分頁
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 --------------- 完全刪除表

浙公網(wǎng)安備 33010602011771號