Questions tagged [check-constraints]

CHECK constraints enforce domain integrity by limiting the values that are accepted by one or more columns.

SQL CHECK constraints enforce domain integrity by limiting the values that are accepted by one or more columns.

You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators. For example, the range of values for a salary column can be limited by creating a CHECK constraint that allows for only data that ranges from $15,000 through $100,000. This prevents salaries from being entered beyond the regular salary range. The logical expression would be the following: salary >= 15000 AND salary <= 100000.

624 questions
3
votes
1 answer

Postgresql Constraint on row value based on other rows

Given the following schema CREATE TABLE test ( value text, flag bool ); is this possible, to create such constraint which would allow duplicate rows with the same value and flag = true, but would allow at most once row with the given value…
3
votes
1 answer

How to restrict overlapping datetimes in django model at DB level using CheckConstraint

I have the following fields in my Model: class Event(models.Model): starts = models.DateTimeField() ends = models.DateTimeField() I want to restrict overlapping dates (starts, ends). I have managed to do this in model validation, but…
3
votes
5 answers

MySQL require a minimum length

I'm using MySQL Workbench and I made a table called 'organizations' and want to block any try of adding a value to a column with less than 5 letters. The column name is 'namee'. I made this, but I get an error: ALTER TABLE organizations ADD…
user11786059
3
votes
2 answers

Are regex check constraints possible in SQL Server

I am trying to find a way to validate data in my columns using regex CHECK constraints but have so far only come across the LIKE keyword that has an extremely limited subset of regex syntax. I'd like to know if there is a way to use regex in check…
Tjaart
  • 3,912
  • 2
  • 37
  • 61
3
votes
1 answer

Is there a way to enforce the MySQL CHECK constraint for calculated value

Apologies if my wording is very awkward/non-fitting for a programmer - I'm just a SQL noob who's taking a beginners' class. I'm using PHPMyAdmin to design an e-commerce database in which the minimum age of a customer is 18 yo. Because age is a…
3
votes
1 answer

How to rewrite this postgres constraint to be more in line with django 2.2?

THis is the raw query that I write for postgres for the check constraint ALTER TABLE rea_asplinkage ADD CONSTRAINT asp_sub_project_positive_integer CHECK ( jsonb_typeof(linkage-> 'root' -> 'in_sub_project') is not distinct…
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
3
votes
1 answer

ORA-02290: check constraint violated

I keep getting the message "ORA-02290: check constraint violated" whenever I try to insert values into my table. Here's the code for the table STORE: CREATE TABLE STORE ( StoreID INT PRIMARY KEY, StoreName VARCHAR2(30) NOT NULL, …
Ellis Jordan
  • 59
  • 2
  • 6
3
votes
1 answer

How do I add a constraint to this code where only certain characters can be inputted?

complete novice to mysql but i'm having problems finding sources online that explain my problem. Essentially I need to add a constraint to the videoCode row where the identifier is a code in the form of XX## where the X are letters and the # are…
3
votes
1 answer

Check constraint not allowing me to add data

I have a user defined function that returns a BIT. 1 - If dates overlap 0 - If dates do not overlap I've test the UDF and it appears to be functioning correctly. This is the following check constraint that I've added: ALTER TABLE [Gizmo] WITH…
PrivateJoker
  • 781
  • 4
  • 13
  • 27
3
votes
2 answers

MySql Constraint to have particular fields value greater than zero

I have the following MySql syntax to create a table with constraint to check P_Id column have value greater than zero, but it still allows me to add values less than 0 like -1,-2, etc. CREATE TABLE Persons( P_Id int NOT NULL , LastName…
Kalpesh Jain
  • 409
  • 3
  • 10
  • 19
3
votes
1 answer

Check constraint for a condition in Postgresql

I'm trying to create a check constraint to prevent people from changing a sales_status to 3 unless the progression_status is 80. I thought it was ALTER TABLE mytable ADD CONSTRAINT sales_status_cant_be_3_for_nonprogressed CHECK…
Luffydude
  • 702
  • 14
  • 27
3
votes
1 answer

Enforcing that all columns have different values

A particular val# should be in no more than one column. Multiple columns can be empty though. (I don't want to use NULL instead of empty.) CREATE TYPE vals AS ENUM ('val1', 'val2', 'val3', 'val4', 'val5', ... 'empty'); CREATE TABLE some_table ( ... …
tom
  • 2,137
  • 2
  • 27
  • 51
3
votes
6 answers

How do I check constraints between two tables when inserting into a third table that references the other two tables?

Consider this example schema: Customer ( int CustomerId pk, .... ) Employee ( int EmployeeId pk, int CustomerId references Customer.CustomerId, .... ) WorkItem ( int WorkItemId pk, int CustomerId references…
Egil Hansen
  • 15,028
  • 8
  • 37
  • 54
3
votes
1 answer

How to create a constraint that will assure that the date entered is always >= today or a future date?

I'm created a table called Project with two columns ProjectID and StartDate. I would like to create a constraint that will assure that the date entred is always equal to today or a future date. I have tried the code below but gets a missing…
ShoeraB
  • 47
  • 2
3
votes
1 answer

PostgreSQL CHECK Constraint to Limit Specific Characters

I need to limit five special characters in my column specifically: period, comma, single-quotes, double-quotes, and dollar sign. I understand I can use the following to only allow alphanumeric characters but I need to be able to accept the other…
Smiley
  • 3,207
  • 13
  • 49
  • 66