连续复制
一键复制
一键打包

mysql表复制

首先复制表结构

mysql>create table t3 like t1;

然后复制表数据

mysql>insert into t3 select * from t1;

注意:复制表数据的select *,星号特殊时候需要制定列!!!

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)

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列创建唯一索引,该列不能有重复值,否则不能创建成果

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;

length(string[..])//string长度
select length("hello") as myname;

ltrim(string[..]) //去除前段空格
select ltrim(" hell o ") as mynmae;

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触发器

修改定界符为 //
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);

重排auto_increment值
truncate t1;会把自增列表从新清空,从新开始

delete from tablename不能清空表,

或者情况后直接用alter命令修改表

alter table tablename auto_increment=1;