mysql表複製
首先複製表結構
mysql>create table t3 like t1;
而後複製表數據
mysql>insert into t3 select * from t1;
注意:複製表數據的select *,星號特殊時候須要制定列!!!
mysql索引mysql
ALTER方式建立索引(推薦)
ALTER TABLE table_name ADD INDEX index_name(column_list)
ALTER TABLE table_name ADD UNIQUE (column_list)
ALTER TABLE table_name ADD PRIMARY KEY (column_list)linux
create index建立索引(不能對主鍵索引操做)
create index in_name on t1(name); --對t1表的name列建立普通索引
show index from t1; --查詢索引
drop index in_name on t1; --刪除索引
create unique index un_name on t1(name); --對t1表的name列建立惟一索引,該列不能有重複值,不然不能建立成果sql
drop index
DROP INDEX index_name ON table_name
alter table table drop
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
mysql視圖
create view v_t1 as select * from t1 where id>4 and id ? view
ALTER VIEW
CREATE VIEW
DROP VIEW
查看view
mysql>show tables;
刪除view
mysql>drop view v_t1;
mysql內置函數
字符串函數
CONCAT(string2[..,..]) //連接函數
select concat("hello","world") as myname;後端
LCASE(string[...]) //轉換小寫
select lcase("HEllO") as myname;函數
UCASE(string[..])//轉大寫
select ucase("HEllO") as myname;mysql索引
LENGTH(string[..])//string長度
select length("hello") as myname;spa
LTRIM(string[..]) //去除前段空格
select ltrim(" hell o ") as mynmae;unix
RTRIM(STRING[..])//去除後端空格
select rtrim("heel l l ") as myname;索引
REPEAT(string,out) //重複count次
select repeat("string",3) as myname;事務
REPLACE(STR,search_str,replace_str) //在str中用replace_str替換search_str
select replace("string","s","=") as myname;
SUBSTRING(str,position,[length]) //在str的position開始,取length個字符
select substring("hello",2,2) as myname; 返回 el
SPACE(count) //生成count個字符
select concat(space(10),"linux") as myname;
數學函數
BIN(decimal_number) //十進制轉二進制
select bin(10); 輸出1010
CEILING(NUMBER) //向上取整
select ceiling(10.10); 輸出11
FLOOR(number) //向下取整
select floor(10.10); 輸出10
MAX()
MIN()
SQRT() //開平方
RAND() 返回0-1內的隨機數
日期函數
CURDATE() //當前日期
CURTIME() 當前時間
NOW() 當前日期時間
UNIX_TIMESTAMP(DATE) 當前date的unix時間戳
FROM_UNIXTIME() unix時間戳的日期值
WEEK(DATE) 返回參數date的一年中的第幾周
YEAR(DATE) 返回日期date的年份
DATEDIFF(EXPR,EXPR2) 返回起始expr和結束expr2間的天數
mysql預處理
新建預處理程序
prepare stmt1 from "select * from t1 where id>?";
設置?值
set @i=1;
執行stmt1
execute stmt1 using @i;
再次設置?值
set @i=2;
在執行stmt1
execute stmt1 using @i;
刪除stmt1
drop prepare stmt1;
mysql事務處理(myisam引擎不支持事務,innodb才支持。alter table t1 engine=innodb;show create table t1;)
關閉自動提交
set autocommit=0;
表t1刪除id=1的數據
delete from t1 where id=1;
作還原點p1
savepoint p1;
在刪除id=2的數據
delete from t1 where id=2;
再作還原點p2
savepoint p2;
恢復到p1,p2的還原就消失了
rollback to p1;
退回到最原始的還原點
rollback;
mysql存儲
建立一個存儲pl();
mysql>\d //
mysql> create procedure p1()
->begin
->set @i=0;
->while @iselect @i;
->set @i=@i+1;
->end while;
->end;
->//
執行存儲p1();
mysql-> \d ;
mysql->call p1();
查看procedure p1()的status信息
mysql> show procedure status \G
查看procedure p1()的具體信息
mysql>show create procedure p1 \G
mysql觸發器
修改定界符爲 //
mysql>\d //
建立一個名字爲tg1的觸發器,當向t1表中插入數據時,就行t2表插入數據
mysql> create trigger tg1 before insert into t1 for each row
begin
insert into t2(id) values(new.id);
end //
裝備兩個空表t1和t2
mysql>select * from t1;
mysql>select * from t2;
向t1表插入多條數據
mysql>insert into t1 values(1),(2),(3),(4);
如何製做刪除表t1後t2表中的記錄也會跟着刪除呢?
mysql>\d //
mysql>create trigger tg2 before delete on tl for each row
begin delete from t2 where id=old.id;
en4 //
mysql>\d ;
如何製做更改表t1後t2表中的記錄跟着更改呢?
mysql>\d //
mysql>create trigger tg3 before update on tl for each row
begin update t2 set id=new.id where id=old.id:
end //
mysql>\d ;
查看觸發器
mysql>show triggers;
重排auto_increment值 truncate t1;會把自增列表重新清空,重新開始 delete from tablename不能清空表, 或者狀況後直接用alter命令修改表 alter table tablename auto_increment=1;