check-constraint

SQL Constraint that one column value cannot be greater than another in a different table

狂风中的少年 提交于 2019-12-24 14:38:51
问题 This probably isn't a very nice question as its blending business logic with DB structure, but its not my decision so: Is it possible to define a constraint the infers the value of one column (Table A, Column X) cannot be greater than the value of another (Table B, Column Y) referenced via a foreign key: TABLE_A ID (Primary Key) X (int value) TABLE_B A_ID (Foreign Key to TABLE_A) Y (int value) i.e. I want to enforce that for all values of Y, Y < L where L is a value from X where TABLE_B.A_ID

How to constrain the number of records allowed in an SQL table?

£可爱£侵袭症+ 提交于 2019-12-06 14:48:54
问题 Say I have two tables, Parent and Child. Parent has a MaxChildren (int) field and Child has an Enabled (bit) field and a ParentID (int) field linking back to the parent record. I'd like to have a constraint such that there can't be more than MaxChildren records for each parent where Enabled = 1. This would mean that any attempt to insert or update any record in the Child table will fail if it goes over the applicable MaxChildren value, or any attempt to lower MaxChildren to below the current

How to constrain the number of records allowed in an SQL table?

▼魔方 西西 提交于 2019-12-04 19:08:17
Say I have two tables, Parent and Child. Parent has a MaxChildren (int) field and Child has an Enabled (bit) field and a ParentID (int) field linking back to the parent record. I'd like to have a constraint such that there can't be more than MaxChildren records for each parent where Enabled = 1. This would mean that any attempt to insert or update any record in the Child table will fail if it goes over the applicable MaxChildren value, or any attempt to lower MaxChildren to below the current number of applicable Child records will fail. I'm using MS SQL Server, but I'm hoping there's a

CHECK CONSTRAINT on multiple columns

随声附和 提交于 2019-11-27 21:59:32
I use SQL Server 2008 I use a CHECK CONSTRAINT on multiple columns in the same table to try to validate data input. I receive an error: Column CHECK constraint for column 'AAAA' references another column, table 'XXXX'. CHECK CONSTRAINT does not work in this way. Any other way to implement this on a single table without using FK? Thanks Here an example of my code CREATE TABLE dbo.Test ( EffectiveStartDate dateTime2(2) NOT NULL, EffectiveEndDate dateTime2(2) NOT NULL CONSTRAINT CK_CmsSponsoredContents_EffectiveEndDate CHECK (EffectiveEndDate > EffectiveStartDate), ); Yes, define the CHECK

CHECK CONSTRAINT on multiple columns

回眸只為那壹抹淺笑 提交于 2019-11-26 20:35:08
问题 I use SQL Server 2008 I use a CHECK CONSTRAINT on multiple columns in the same table to try to validate data input. I receive an error: Column CHECK constraint for column 'AAAA' references another column, table 'XXXX'. CHECK CONSTRAINT does not work in this way. Any other way to implement this on a single table without using FK? Thanks Here an example of my code CREATE TABLE dbo.Test ( EffectiveStartDate dateTime2(2) NOT NULL, EffectiveEndDate dateTime2(2) NOT NULL CONSTRAINT CK