check-constraints

An IF inside a check constraint SQL

馋奶兔 提交于 2019-12-13 19:11:43
问题 I have this table.. CREATE TABLE [dbo].[Tipo_Servicios_Info]( [TSI_TS_Id] [int] NOT NULL, [TS_Tar_Id] [int] NOT NULL, [TS_PDI_Id] [int] NOT NULL, [TSI_Descripcion] varchar(100), [TSI_FechaIni] date not null, [TSI_FechaFin] date not null, [TSI_HoraMin] time, [TSI_HoraMax] time, [TSI_Duracion] varchar(50) not null, [TSI_Unidad] varchar(50) not null, [TSI_Cantidad] int not null, CONSTRAINT [PK_TIPO_SERVICIOS_INFO] PRIMARY KEY CLUSTERED ( [TSI_TS_Id] ASC, [TS_Tar_Id] ASC, [TS_PDI_Id] ASC )WITH

Check constraint on date

不打扰是莪最后的温柔 提交于 2019-12-12 02:47:17
问题 I am creating a table with date column. I would like to add a check constraint to validate if the date is in future. create table test_table ( schedule_date date not null, check (schedule_date >= TODAY) ); Above sql gives me a syntax error. 09:43:37 [CREATE - 0 row(s), 0.000 secs] [Error Code: -201, SQL State: 42000] A syntax error has occurred. ... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec [0 successful, 0 warnings, 1 errors] How do I add a constraint on

Constraint on a column based on another column

一曲冷凌霜 提交于 2019-12-12 02:38:13
问题 I have a table "Table" that contains an ID, Col1 and Col2, Col3. Col2 can either be 0 or 1. I want Col2 in rows where Col1 has the same value to be the same. Ex I want something like this +----+-------+------+-----------+ | ID | Col1 | Col2 | Col3 | +----+-------+------+-----------+ | 1 | "One" | 0 | "Yeah" | | 2 | "One" | 0 | "Meh" | | 3 | "One" | 0 | "Why Not" | | 4 | "Two" | 1 | "Huh"! | +----+-------+------+-----------+ And not +----+-------+------+-----------+ | ID | Col1 | Col2 | Col3 |

Function to update a status flag for validity of other column?

喜欢而已 提交于 2019-12-12 02:25:42
问题 How to create a function that compares card_id with allowed_cards array elements? If it's in this array, column status must be updated to TRUE . CREATE TABLE client_1 ( id bigint NOT NULL, "time" timestamp without time zone DEFAULT now(), status boolean, card_id character varying(10), CONSTRAINT client_1_pkey PRIMARY KEY (id) ); CREATE TABLE allowed_cards ( allowed_cards character varying(10)[] ); INSERT INTO allowed_cards VALUES ('{DD3234,FF2342}'); 回答1: Enforce valid card_id at all times

Generically identify the rowid causing a constraint conflict in SQLite

亡梦爱人 提交于 2019-12-11 19:48:31
问题 I need to identify the row that caused a constraint check to fail after an INSERT OR IGNORE statement. I need this for a tool that generates SQL statements, so I can't tie this to a specific use case with a particular table. The challenge here is that I can't know which constraints are set for the particular table that I will insert into. It may have foreign key or UNIQUE constraints, or even a CHECK clause attached. So, all I can learn from the INSERT is that it has failed. Now, how to I

Oracle SQL Check constraint between 2 tables

删除回忆录丶 提交于 2019-12-11 14:31:40
问题 I got 2 tables, Persons and Relationships . The Persons table got only 2 fields for now: ID and Age . Relationships have 3 fields: Person_ID , Relative_ID and Relation What I wanna do is simple: on insertion\update to Relationships I want to check the following: if Relation == 'child' and Persons[Person_ID].Age < Persons[Relative_ID].Age: Throw Exception Thanks 回答1: You should create trigger. Try this CREATE OR REPLACE TRIGGER my_trg BEFORE INSERT OR UPDATE ON Relationships FOR EACH ROW

Check constraints that ensures the values in a column of tableA is less the values in a column of tableB

邮差的信 提交于 2019-12-11 10:18:21
问题 I'm creating a database and i have the tables ProjectDetails.TimeCardExp and ProjectDetails.Project. The TimeCardExp table has a column ExpenseDate, and in the table Project, there is column EndDate; (they both accept the datetime data type). What I want to do is to implement a constraint on the ExpenseDate column of the TimeCardExp table that ensures that the values entered in the ExpenseDate column of table TimeCardExp are less than the values in the EndDate column of the Project table, for

How much cost check constraints in Postgres 9.x?

折月煮酒 提交于 2019-12-09 23:31:39
问题 I'd like to know if there are some benchmark to compare how much cost insert some check constraints on a table of 60 columns where on 20 i'd like to insert a constraints of NotEmpty and on 6 rows NotNull. My case is that i have on my table Empty values and Null values (that in my case means always "no data"). I'd like to unify that data values with just one. That's why I'm thinking to insert NotEmpty constraints on columns, because as i have read null value are not heavy (in byte size) as

SQL Server 2012 CHECK constraint not firing on UPDATE or DELETE statements

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 07:06:40
问题 I'm trying to perform data validation on a table, and I need to check across multiple rows and columns. Basically I want to make sure that for any given tool, at least one version exists that is flagged as either the Active version, or the Beta version. The only way I could think to check this is through a user-defined scalar function, and a CHECK constraint that calls the function. I was able to create the function that returns what I expect (0 if OK, 1 if not) and it works... but only on an

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