mysql一些常用命令總結

2022-07-20 23:03:30 字數 2854 閱讀 4969

mysql時間戳轉日期格式

select from_unixtime(add_time,'%y-%m-%d %h:%i:%s') from `wh5_username` where id=23;

按in裡面的順序來排序

select * from table where id in (3,6,9,1,2,5,8,7) order by field(id,3,6,9,1,2,5,8,7);

顯示程序

show processlist

連線資料庫

mysql -uroot -ppwd.secret -s /data/mysql/mysql_3306.sock

刪除資料表

truncate table 表名

建立資料庫帶預設字符集

create database `test2` default character set utf8 collate utf8_general_ci

給某個資料庫新增資料庫使用者名稱密碼

grant all on `資料庫名`.* to '使用者名稱'@localhost identified by '密碼';

遠端授權例子:

grant all privileges on test.* to test@'127.41.93.243' identified by '1234567';

刪除使用者

use mysql

delete from user where user='test' and host='localhost';

sql匯入

source test.sql

修改遞增的值

alter table users auto_increment=0;

修改字段

alter table chatter_users modify column ip varchar(50);

修改欄位名

alter table `test_user` change `uc_uid` `type` tinyint(1) unsigned not null default '0';

sql匯出

只匯出表結構

mysqldump -uroot -pdbpasswd -d dbname test>db.sql;

匯出表結構和資料

mysqldump -uroot -pdbpasswd dbname test>db.sql;

eg:漫畫匯出語句

mysqldump -uroot -ppwd.secret -h127.0.0.1 yaoyao_test>yaoyao_test20140415.sql;

允許客戶端連線mysql操作

複製表資料和表結構

第一中方法:

複製整個表

create table new_table select * from old_table;

複製但不複製資料

create table new_table select * from old_table where 0;

以上方法存在問題: 其實只是把select語句的結果建乙個表。所以new_table這個表不會有主鍵、索引。

可以用一下方法改進:

create table new_table(id int(20) not null auto_increment primary key) select * from old_table

這個sql語句可以實現複製基本結構、表的主鍵和自動增長,這裡new_table的id會自動覆蓋old_table的id定義格式

第二種方法:

將 production 資料庫中的 mytbl 表快速複製為 mytbl_new,2個命令如下:

create table mytbl_new like production.mytbl;

insert mytbl_new select * from production.mytbl;

第乙個命令是建立新的資料表 mytbl_new ,並複製 mytbl 的資料表結構。

第二個命令是講資料表 mytbl 中的資料複製到新錶 mytbl_new 。

注:production.mytbl是指定要複製表的資料庫名稱為 production 。它是可選的。

假如沒有production. ,mysql資料庫將會假設mytbl在當前操作的資料庫。

我用的securecrt用的utf-8編碼,終端時lang=en_us.utf-8, 而經過蘇普同學提醒,這條記錄的字條應該是utf-8!

那麼強制在mysql命令列裡設定utf-8而不用預設的gbk。

set @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=28

msyql編碼問題參考**

mysql匯出部分指定列資料

select field1,field2 from mytable into outfile '/backup/outfile.txt';

mysql查詢替換sql寫法

update `m_chapter_0` set `pics` = replace (`pics`,'test.jide123.cc:8080','test.jide123.cc')

update `test_ecms_news_data_1` set `surl` = replace (`surl`,'vdata.test.org','test.yaoyao.org');

用b表的字段值更新a表的字段

update market_prize a inner join market_project_channel b on a.id=b.pid set a.interact_id = b.interact_id;

mysql一些常用命令

mysql一些常用命令 一 連線mysql。格式 mysql h主機位址 u使用者名稱 p使用者密碼 1 連線到本機上的mysql。首先開啟dos視窗,然後進入目錄mysql bin,再鍵入命令mysql u root p,回車後提示你輸密碼.注意使用者名稱前可以有空格也可以沒有空格,但是密碼前必須...

mysql一些常用命令

一 總結一下linux下使用mysql一些常用命令 1.linux下啟動mysql的命令 mysqladmin start ect init.d mysql start 前面為mysql的安裝路徑 2.linux下重啟mysql的命令 mysqladmin restart ect init.d my...

git 一些常用命令總結

主要總結git常用命令,方便自己查閱!1 初始化倉庫 git init2 關聯遠端倉庫 git remote add origin 倉庫名 git remote add origin2 轉殖倉庫 git clone 倉庫位址 git clone3 提交到git git add 告訴git,把檔案新增...