复合主键

时间:2016-10-09 06:14:32

标签: sql sql-server

尝试学习SQL。似乎无法找到答案,或者如果我做了,我不知道足够的SQL来识别它。您是否可以在其中一个键列上创建一个包含复合主键和检查约束的表?

具体而言,这失败了:

  

INSERT语句与CHECK约束" CH_AddressType"冲突。冲突发生在数据库" tempdb",table" dbo。#t

如果我注释掉表中的检查约束,则创建并取消注释alter table,插入工作。

创建表的两种方法是......创建相同的表吗?我错过了什么?

create table #t 
(
    entityNo        int             not null,
    addressType     char(1)         not null,
    valid           char(1)         not null,

    address1        varchar(256)    not null,
    address2        varchar(256)    not null,
    city            varchar(128)    not null,
    stateCode       char(2)         not null,
    zipCode         char(5)         not null,

    dtCreate        datetime        not null,
    dtUpdate        datetime        not null

    constraint PK_Address_Entity 
         primary key (entityNo, addressType),

    constraint CH_AddressValid 
         check (valid in ('Y', 'N')),
    constraint CH_AddressType 
         check (valid in ('H', 'V'))
)
go

--alter table #t
--  add constraint CH_Address_Type check ((addressType) in ('H', 'V'))
--go

insert into #t (entityNo, addressType, valid, address1, address2, city, stateCode, zipCode, dtCreate, dtUpdate)
values (100, 'H', 'Y', '100 Some Street', '' , 'Someville', 'PA', '19335', '2016-10-08', '2016-10-08')

select * from #t
go

drop table #t
go

1 个答案:

答案 0 :(得分:2)

constraint CH_AddressType check (addressType in ('H', 'V'))

应该是

final Job.Builder builder = dispatcher.newJobBuilder() .setTag("myJob") .setService(myJobService.class) .setRecurring(true) .setTrigger(Trigger.executionWindow(30, 60));