请教如何避免重复记录的insert

会飞的小洋洋 2007-10-17 05:38:38
一个表含以下三列
id oid date
1 12 2007-10-02 16:03:40.797
2 12 2007-10-02 16:03:40.920 ------该条记录不允许
3 12 2007-10-03 16:03:40.920 ------该条记录允许

id为自增列,oid为编号,date为插入记录的时间

现在希望在同一天内,不插入oid相同的记录。


...全文
1235 43 打赏 收藏 转发到动态 举报
写回复
用AI写文章
43 条回复
切换为时间正序
请发表友善的回复…
发表回复
会飞的小洋洋 2007-10-23
  • 打赏
  • 举报
回复
谢谢各位
我们最后的方式是,加入约束,整理旧数据
love_cutezhou 2007-10-22
  • 打赏
  • 举报
回复
xuexi
fcuandy 2007-10-20
  • 打赏
  • 举报
回复
希望在同一天内,不插入oid相同的记录

fcuandy 是不是漏掉了oid?


我在30楼的语句,只是为了说明,如何通过新加字段,设默认值,然后创建约束来实现,对dattetime型数据按年月日进行唯一约束. 你实际使用时,给oid 的约束加进去就可以了
zzlb0224 2007-10-19
  • 打赏
  • 举报
回复
开始新生活 写的好
ojuju10 2007-10-19
  • 打赏
  • 举报
回复
就用老乌龟的方法写个触发器,呵呵
welove1983 2007-10-19
  • 打赏
  • 举报
回复
多谢fc 学习了 - - 一直不知 还可以这样defalut
多谢...
deansroom 2007-10-19
  • 打赏
  • 举报
回复
学习 UP 感觉最好的方式还是直接用时间和OID联合做主键就OK 然后去除主键ID列
这样 若有相同的记录肯定无法插进去
kofkyo 2007-10-19
  • 打赏
  • 举报
回复
希望在同一天内,不插入oid相同的记录

fcuandy 是不是漏掉了oid?
GRLD8888 2007-10-19
  • 打赏
  • 举报
回复
我用触发器实现的,我想应该是你需要的结果吧!

建立测试环境:

create table test
(id int identity,oid int,date1 datetime)
---------
创建触发器:

create trigger tr1
on test
after insert
as
if exists(select convert(char(10),date1,120) from test
group by convert(char(10),date1,120) having
count(convert(char(10),date1,120))>1 and count(oid)>1)
begin
rollback tran
print 'no'
end
else
begin
commit tran
print 'yes'
end
sunhonglei2004 2007-10-19
  • 打赏
  • 举报
回复
试写一个,我是这样理解的,在同一天当中oid不能插入重复记录.
declare @a int,
declare @b int,
@nowdate varchar(100)
set @a=12
set @nowdate=datename(year,getdate())+'-'+datename(month,getdate())+'-'+datename(day,getdate())
set @b=oid from t where (convert(varchar(10),getdate(),20))=@nowdate and oid=@a
if @b is null
insert into t (oid,date) values (@a,getdate())
else
print '对不起,今天已有相同记录'
wolaile27 2007-10-19
  • 打赏
  • 举报
回复
在数据的表中进行限制
pt1314917 2007-10-19
  • 打赏
  • 举报
回复
正确答案太多了`
ojuju10 2007-10-19
  • 打赏
  • 举报
回复

Create table aa(id int identity,oid int,date datetime)

alter trigger Insert_aa on aa
instead of insert
as
begin
if not exists(select 1 from inserted a where exists(select 1 from aa where a.oid=oid
and convert(varchar(10),a.date,120)=convert(varchar(10),date,120)))
begin

insert into aa(oid,date) select oid ,date from inserted

end

end


insert into aa select 12,getdate()
insert into aa select 12,getdate()
insert into aa select 12,getdate()
insert into aa select 12,getdate()

select * from aa

id oid date
----------- ----------- -----------------------
1 12 2007-10-19 00:00:00.000

会飞的小洋洋 2007-10-18
  • 打赏
  • 举报
回复
如果新建个字段,如何将date的值同时insert到那个新字段上?

除了程序insert的时候也insert这个字段
wwqna 2007-10-18
  • 打赏
  • 举报
回复
你把日期作一个字段,时间做一个字段,再把ID和时间作联合主键就行了,建触发器影响揷入的速度
cnmmbd 2007-10-18
  • 打赏
  • 举报
回复
mark
cxmcxm 2007-10-18
  • 打赏
  • 举报
回复
笨办法,再加一字段保存日期时间的日期部分,再对id与日期字段设为主键

或写一插入触发器

或在插入前先用语句直接检查,检查前开始事务,且查询时锁表( from 表名 with (holdlock)) , 插入记录后再提交事务

以上方法,如果可增加字段,加一字段保存日期应该是插入效率最快的.


friendlyFour 2007-10-18
  • 打赏
  • 举报
回复
检查约束
q卡卡p 2007-10-18
  • 打赏
  • 举报
回复
插入前先判断一下,IF NOT EXISTS
如果有就不执行,没有就插入
fcuandy 2007-10-18
  • 打赏
  • 举报
回复
我指的如果你要新加字段时, 那么这个字段可以设置默认值,而不用在程序里加对这个新字段的操作.

设创建表 t, 字段 dttime(datetime), dt(varchar(10))
dttime就是dateime那个列, dt就是你要用来做约束的新字段

比如:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[t]
GO

CREATE TABLE [dbo].[t] (
[dttime] [datetime] NOT NULL,
dt as convert(varchar(10),dttime ,120),

CONSTRAINT [IX_T] UNIQUE
(
dt ASC
)
) ON [PRIMARY]


insert t select getdate() --可以成功插入
insert t select getdate() --被dt的唯一值要求所约束
select * from t --结果,只插入了一条记录


/*
违反了 UNIQUE KEY 约束 'IX_T'。不能在对象 't' 中插入重复键。
语句已终止。
dttime dt
------------------------------------------------------ ----------
2007-10-18 21:37:03.373 2007-10-18
*/

加载更多回复(23)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧