mysql的小知识点

时间:2022-11-23 02:18:38

新建表:create table tb1(id int primary key auto_increment,name varchar(20),age tinyint default '10');

1、如果插入记录的时候报错,则自增的id是被占用的,比如现在记录是10,此时插入记录报错,则11是被占用的,再次成功插入记录的话,id是12;如果是申明插入id是11的话,是可以将被占用的id重新使用;

2、insert into tb2 values(default,'a',default);可以插入数据,id是默认的自增长,age是默认值;

3、insert (into) tb2 set age = 11;

不带参数的函数:

create function fun() returns varchar(50) return date_format(now(),'%Y-%m-%d %H-%i-%s');

带参数的函数:

create function fun(num1 int,num2 int) returns int return num1 + num2;

复杂点的:

(以/作为结束符)

delimiter //

create function fun(username varchar(20))

returns int

begin

insert into user(username) values(username);

return last_insert_id();

end//


存储过程:

create procedure sp1() select version();

create procedure delById(in userid int)

begin

delete from user where id = userid;

end//(入参名称不能和参数一样)


create procedure delUserAndReturnCount(in userid int,out userNum int)

begin

delete from user where id = userid;

select count(id) from user into usernum;

END//


call delUserAndReturnCount(10,@nums)//

select @nums//