
正文
sqlserver字段不可重复,sql字段不允许重复
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
sql server中,怎样建立一个不允许重复记录的字段
建立不重复字段,有两种方法:
1、允许null值,使用UNIQUE关键字,建立唯一约束;
2、不允许null值,使用PRIMARY KEY关键字,建立主键约束。
相关问答
Q1: sqlserver存储过程防止字段插入重复
你建表的时候为什么不把那列弄成唯一约束?
create procedure insert_pig
@str varchar(6), --定义一个输入参数,就是那个是不是重复的值
@str1 char(2)
--把每一列弄成变量存入,不知你的表有几字段,这里我就以两个字段为例
as
declare @sum int
begin tran
insert into admin (列1,列2) values (@str,@str1)
select @sum=count(*) from admin where username=@str
if(@sum0)
begin
raiserror('该品种已经存在',16,8)
rollback tran --滚回事务
end
else
commit tran --提交事务
GO
--调用存储过程
exec insert_pig '张三','男'
----张三,男就是传入的参数,也就是张三赋值给了@str,男赋值给了@str1
觉得可以给加加分吧
Q2: sqlserver 怎样将所有的字段去掉重复的数据
找到最大的rowid即可。
Sql代码:
alter proc getNotDupData
as
--clear temp table
delete ODS.dbo.Agent
delete from stage.dbo.tmpDup
delete from stage.dbo.tmpRowNo
delete from stage.dbo.tmpMaxRowNo
--create dup table
insert into stage.dbo.tmpDup
select distinct AgentLogin,AgentSurName,AgentGivenName from stage.dbo.dAgentPerformanceStat
where AgentSurname is not null and agentlogin like '3%' order by AgentLogin
--add rowNo
insert into tmpRowNo
select *,ROW_NUMBER()over(order by AgentLogin) as rowno from tmpDup
--get max rowno
insert into stage.dbo.tmpMaxRowNo
select max(rowno) as 'rowno' from stage.dbo.tmpRowNo group by AgentLogin having count(*)1
--remove max rowno
delete from stage.dbo.tmpRowNo where rowno in (select * from stage.dbo.tmpMaxRowNo)
--insert into ods
insert into ODS.dbo.Agent select AgentLogin,AgentSurName,AgentGivenName from stage.dbo.tmpRowNo
关于sqlserver字段不可重复和sql字段不允许重复的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








