MySql常用的語句

2021-06-07 01:56:44 字數 2496 閱讀 4557

1.複製表結構和資料,但是不複製索引和外來鍵:

create

table a select

*from b;

2.複製表結構和索引和外來鍵,不複製資料:

create

table a like b;

3.只複製資料:

insert

into a select

*from b;

4.移動表到另乙個庫

rename table a.t to b.t;

5.刪除重覆記錄

--新建乙個臨時表 

create

table tmp as

select

*from youtable group by name ;

--刪除原來的表 

drop

table youtable ;

--重新命名表 

alter table tmp rename youtable;

--新建乙個臨時表 

create

table tmp like youtable;

--取出不重複的資料 

insert

into

select

*from youtable group by name;

--清空原來的表 

truncate youtable;

--插入原來的表 

insert

into youtable select

*from tmp;

--重新命名表 

drop

table tmp;

6.重新命名資料庫

到/var/lib/mysql/

修改對應資料庫的資料夾名即可

7.時間格式

select

from_unixtime

( 1249488000,

'%y-%m-%d %h:%i:%s');

select

date_format

('1997-10-04 22:23:00'

,'%y-%m-%d %h:%i:%s');

select

unix_timestamp

('2009-08-06');

8.mysql日誌

--檢視日誌

show

binary logs;

show master logs;

--清除日誌

purge master logs to

'mysql-bin.000035'

;--手動刪除10天前的mysql binlog日誌

purge master logs before date_sub(

current_date

, interval 10 day);

9.獲得更新記錄數

select

row_count()

;

10.獲得找到的記錄數

select

found_rows()

;

11.獲得插入的id

select

last_insert_id()

;

12.建立特殊表名

set sql_mode='ansi_quotes'

;create

table

"a-b"

(a int);

13.插入不重複資料

insert

into node (name)

select

'a'where no exists(

select id from node where id=2 and name=

'a')

14.uuid

select

replace

(uuid()

,'-',''

);

15.新增乙個遠端使用者,名為username密碼為password

grant all privileges on *.*

to username@"%

" identified by 'password' with grant option;

16.從檔案匯入資料

load data infile '/tmp/result100.txt'

into

table analy_ip_file2 fields terminated by ',' enclosed by '"' lines terminated by '\n';

17.新增主鍵

alter table userconfig add id int

(4)auto_increment primary key;

18.檢視mysql引數

show variables like

'%max%';

end

常用mysql語句 常用MySql語句

新建資料表 drop table if exists ga game way create table ga game way id int 11 unsigned not null auto increment comment id primary key id using btree,主鍵 un...

常用的mysql語句

delimiter 宣告乙個分隔符 結束時也要呼叫多一次 拼接字串,concat ws 分隔符,第乙個字串,第二個字串 例子 concat ws i,mp4?x oss process video snapshot,t 1000,m fast,w 220,h 300 獲取當前時間戳,unix tim...

常用MySQL語句

1.庫的操作 2.表的操作 3.索引的操作 4.檢視的操作 5.資料的操作 建立資料庫 create database database name 檢視資料庫 show databases 選擇資料庫 use database name 刪除資料庫 drop database database na...