【发布时间】:2014-12-20 17:26:33
【问题描述】:
我想避免在具有自动增量 ID (Viewer_ID) 的表中插入重复值。我在 jsp 页面上工作,我写了准备好的声明。我怎样才能做到这一点??在我的表中创建索引或编写存储过程或通过jsp页面中的特定查询解决这个问题更好吗???如果我想编写存储过程,如何在我的 jsp 页面中使用它???谢谢
PreparedStatement pst = con.prepareStatement( Insert into Viewer (F_Name, L_Name, Competition_ID, City ,Phone, E-mail, Reserve_ID) values (?,?,?,?,?,?,?);
编辑:
if not exists (select F_Name, L_Name, City, Phone
, [E-mail] from Viewer where F_Name = " + fname + "
and L_Name = " + lname + " and City = " + city + " and Phone
= " + phone + " and [E-mail] = " + email + ") begin Insert
into Viewer (F_Name, L_Name, Competition_ID, City ,Phone, [E-mail], Reserve_ID) values (?,?,?,?,?,?,?) end
请告诉我为什么这个查询不起作用???
出现此错误:无效的列名'????'
【问题讨论】:
-
哪一列不应该重复?只需将其定义为
uniqueat table。 -
@Riad 除了 Competition_ID 和 Reserve_ID 是其他表中的外键并且自动递增之外的所有这些!
-
然后制作一个复合键...使用
alter table add unique ..... -
你能提供更多关于编写查询的细节吗?
标签: sql sql-server jsp stored-procedures