Mysql 如何通过存储过程应用ID应以“E”开头且其后有2个数字的约束?
只有当EID以“E”开头且其后有2个数字时,才应插入EID。。。。 我试图写一个程序,目的如下:Mysql 如何通过存储过程应用ID应以“E”开头且其后有2个数字的约束?,mysql,sql,stored-procedures,procedure,Mysql,Sql,Stored Procedures,Procedure,只有当EID以“E”开头且其后有2个数字时,才应插入EID。。。。 我试图写一个程序,目的如下: create proc emp_checks(@eid varchar(20), @name varchar(20),@age int, @salary int, @city varchar(20)) AS Begin Declare @citycount int= (select count(*) from employee where @city=city) if (@citycount &l
create proc emp_checks(@eid varchar(20), @name varchar(20),@age int, @salary int, @city varchar(20))
AS
Begin
Declare @citycount int= (select count(*) from employee where @city=city)
if (@citycount < 2 and @eid like 'E--%')
insert into employee values(@eid,@name,@age,@salary,@city)
else
print 'city exceeded'
End
这里的问题是-或空格也会插入非数字
这里,这段代码还包括一些额外的内容*只是家庭作业:为什么要编写存储过程?MySQL现在支持检查约束:
alter table employee add constraint chk_employee_eid
check (eid regexp '^E[0-9]{2}')
请注意,这允许在前3个字符之后添加任何内容。如果您不想要任何东西,则向模式中添加$:
alter table employee add constraint chk_employee_eid
check (eid regexp '^E[0-9]{2}$')