createtable t3 like t1;
createtable t1(id int);
createtable t1(id int unsigned not null auto_increment primary key,
namevarchar(30))
insertinto t1(name) values("user1")
insertinto t1(name) values("user2")
createtable t2 like t1
insertinto t2 select * from t1
--创建索引
createindex in_name on t1(name)
--查看索引
showindex from t1;
--删除索引
dropindex in_name on t1
--唯一索引:
createunique index un_name on t1(name)
altertable t1 add index in_name(name)
--自增长必须是主键:
altertable t1 modify id int unsigned not null
altertable t1 drop primary key
desct1;
altertable t1 drop index in_name
altertable t1 add unique un_name(name)
--主键
altertable t1 add primary key (id)
--调整自增
altertable t1 modify id int unsigned not null auto_increment
insertinto t1(name)values("user1"),("user2"),("user3"),("user4"),("user5"),
("user6"),("user7"),("user8"),("user9"),("user10")
createview v_t1 as select * from t1 where id>4 and id<8
dropview v_t1
--连接字符串
selectconcat("hello" ,"world") as myname
--变小写
selectlcase("ABC") as myname;
--变大写
selectucase("abc") as myname;
--长度
selectlength("linux") as myname;
--去掉左边空格
selectltrim(" test ") as myname;
--去掉右边空格
selectrtrim(" test ") as myname;
selectreplace("123456","2","3") as myname;
--重复出现三次
selectrepeat("linux",3) ;
//从第1开始取得4个
selectsubstring("123456789",1,4);
selectconcat(space(10),"linux");
--十进制转2进制
selectbin(10)
--ceiling向上取整
selectCEILING (10.10); 11
--floor向下取整
selectFLOOR(10.10); 10
MAX(col)
MIN(col)
--平方根
selectsqrt(4)
--返回0-1随机数
RAND()
selectrand(3);
select* from t1 order by rand()
日期函数
curdate()
curtime()
now()
prepareprepare1 from 'select * from t1 where id>?';
set@i=1;
executeprepare1 using @i;
dropprepare prepare1
MySIAM不支持事务
关闭自动提交功能
setautocommit=0;
deletefrom t1 where id>5;
--回滚
rollback;
--提交
commit;
altertable t1 engine=Innodb;
insetinto t1(name) values("user")
--还原点
savepointsavepoint1;
rollbackto savepoint1;
修改语句定界符为//
\d//
createprocedure p1()
begin
set @i=14;
while @i<=100 do
insert intot1(name)values(concat("user",@i));
set @i=@i+1;
end while;
end//
--修改语句定界符为;
\d;
--查看过程
showprocedure status \G
callp1;
修改delimiter为// 语句定界符
\d//
delimiter//
创建叫trigger1的触发器,当向t1表插入数据时,也插入到t2表
--插入触发
createtrigger trigger1 before insert on t1 for each row
begin
insertinto t2(name)values(new.name);
end//
delete是一行一行删除
truncate删除表重新创建auto_increment从头开始
insertinto t1(name) values("user1")
--删除触发器
delimiter//
createtrigger trigger2 before delete on t1 for each row
begin
delete from t2 where id=old.id;
end //
--更新触发器
delimiter//
createtrigger trigger_update before update on t1 for each row
begin update t2 set name=new.name wherename=old.name;
end //
--auto_increment变成1
truncatetable table_name
--清空表再设置1,否则自增
deleterfrom t1;
altertable table_name auto_increment=1;