2

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-> \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);

重排auto_increment值
truncate t1;会把自增列表从新清空,从新开始
delete from tablename不能清空表,
或者情况后直接用alter命令修改表
alter table tablename auto_increment=1;


甄城
1.2k 声望35 粉丝