check-constraints

Unique value constraint across multiple columns

纵然是瞬间 提交于 2021-01-05 11:50:02
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm

Unique value constraint across multiple columns

落爺英雄遲暮 提交于 2021-01-05 11:49:05
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm

Custom error message for Postgresql CHECK IN list constraint

感情迁移 提交于 2020-07-19 23:07:50
问题 I would like to create a more specific error message for Postgres CHECK IN violations. So for example a violation of the following CHECK constraint on a column: management_zone varchar(15) NOT NULL CHECK (management_zone IN ('Marine', 'Terrestrial') ), should return a custom error message such as ie.: "Hint: Check spelling. Only allowed inputs are: 'Marine', 'Terrestrial'. The best solution I have seen so far solves it by using the error message as the name of the check constraint, ie ADD

Custom error message for Postgresql CHECK IN list constraint

冷暖自知 提交于 2020-07-19 23:07:22
问题 I would like to create a more specific error message for Postgres CHECK IN violations. So for example a violation of the following CHECK constraint on a column: management_zone varchar(15) NOT NULL CHECK (management_zone IN ('Marine', 'Terrestrial') ), should return a custom error message such as ie.: "Hint: Check spelling. Only allowed inputs are: 'Marine', 'Terrestrial'. The best solution I have seen so far solves it by using the error message as the name of the check constraint, ie ADD

Using a case statement in a check constraint

青春壹個敷衍的年華 提交于 2020-02-28 07:00:23
问题 i'v been learning SQL for the last week but I am unsure how to correctly add a case statement within a check constraint. Can anybody give me any pointers? I have the following grade table: CREATE TABLE Grade ( salary_grade char(1) NOT NULL CHECK (salary_grade = UPPER(salary_grade)), CONSTRAINT ck_grade_scale CHECK( CASE WHEN salary_grade = '[A-D]' THEN salary_scale = 'S1' WHEN salary_grade = '[D-G]' THEN salary_scale = 'S2' END) salary_scale char(2) DEFAULT 'S1' NOT NULL, CONSTRAINT pk_grade

why is t-sql allowing me to violate a check constraint that uses a UDP?

China☆狼群 提交于 2020-02-04 04:21:05
问题 I have a check constraint on a table in my DB. My understanding of a check is it sets a logical condition that must be true for records in a table. USE [myDB] GO ALTER TABLE [dbo].[myTable] WITH CHECK ADD CONSTRAINT [oneProgramPerTest] CHECK (([dbo].[howManyProgPerTest]([TestId])<(2))) GO ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT [oneProgramPerTest] GO But I am able to do an update to the table that breaks the constraint. After the update, this query returns 9 records: select COUNT (*)

Oracle DB - Set Input Number to exact length

∥☆過路亽.° 提交于 2020-01-15 12:27:28
问题 I am trying to set a constraint where user are only allowed to input 11 digits into the Number Data Type (Phone Number). I've tried alter table "booyeah" add constraint "booyeah_CC1" check ( LENGTH("PHONE_NO") = 11) / and alter table "booyeah" add constraint "booyeah_CC1" check ( PRECISION("PHONE_NO") = 11) / but got an error. For the first one, I kept getting error because it's not detecting the character length, while the second one, gave me an invalid identifier. Thanks for the help! 回答1:

Check constraint does not work on bulk insert for more than 250 records

此生再无相见时 提交于 2020-01-13 08:16:44
问题 My query : INSERT into PriceListRows (PriceListChapterId,[No]) SELECT TOP 250 100943 ,N'2' FROM #AnyTable This query works fine and the following exception raises as desired: The INSERT statement conflicted with the CHECK constraint "CK_PriceListRows_RowNo_Is_Not_Unqiue_In_PriceList". The conflict occurred in database "TadkarWeb", table "dbo.PriceListRows". but with changing SELECT TOP 250 to SELECT TOP 251 (yes! just changing 250 to 251!) the query runs successfully without any check

The values of one column cannot be greater than another

喜夏-厌秋 提交于 2020-01-12 07:45:12
问题 I am trying to create a table where the values in one column can't be greater than the next column over. For example, I am creating the following table. CREATE TABLE Price ( PriceID INT PRIMARY KEY IDENTITY (1,1), OriginalPrice FLOAT NOT NULL, CurrentPrice FLOAT NOT NULL, Discount FLOAT, ShippingCost FLOAT NOT NULL, Tax FLOAT NOT NULL); And Current Price cannot be greater than OriginalPrice. So what I tried doing was CurrentPrice FLOAT CHECK (CurrentPrice <= OriginalPrice) NOT NULL, But this

check constraint won't work mysql [duplicate]

南楼画角 提交于 2020-01-11 11:08:32
问题 This question already has answers here : CHECK constraint in MySQL is not working (8 answers) Closed 11 months ago . check constraint won't work CREATE TABLE IF NOT EXISTS supervisor ( sup_id INT(3) NOT NULL, sup_name VARCHAR(30) NOT NULL, gen VARCHAR(1) NOT NULL CHECK (gen='M' or gen='F'), dep_id INT(4), PRIMARY KEY (sup_id), INDEX (dep_id), FOREIGN KEY (dep_id) REFERENCES department(dep_id) ON UPDATE CASCADE ON DELETE RESTRICT ); i also tried: CONSTRAINT chk_supervisor_gen CHECK ('M' or 'F'