check-constraints

The values of one column cannot be greater than another

﹥>﹥吖頭↗ 提交于 2019-12-03 15:28:21
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 gives me the following error: Msg 8141, Level 16, State 0, Line 1 Column CHECK constraint for column

How do I have a check constraint that refers to another table?

故事扮演 提交于 2019-12-03 07:21:47
I have the following tables in a SQL Server 2008 db: tblItem , which has an ItemID field; tblGoodItem , which also has an ItemID field, and has a foreign key pointing to tblItem; tblBadItem , which also has an ItemID field, and also has a foreign key pointing to tblItem. An item cannot be both a good item and a bad item; it must either be the one or the other. However, whether the item is good or bad, it must be an item. My question is this: how do I add a constraint to the ItemID fields in both tblGoodItem and tblBadItem so that an ItemID value cannot exist in both tables ? I've read some

How to use oracle check constraints to limit number of registration?

邮差的信 提交于 2019-12-02 17:13:04
问题 I've a user table with unique user_id. User can register using there id. Now I want to limit the max. registration per user using CHECK constraints. so I use this: .... CHECK(select count(user_id) from user where ... ...) But it's show subquery cannot use in check constraints. Can anyone tell me how can I add this condition? 回答1: Under certain conditions, you can enforce table restrictsion with materialized views : create table tq84_t ( user_id number, foo varchar2(10), constraint pk_tq84_t

SQL Server - CHECK constraint on a column where values come from another table

為{幸葍}努か 提交于 2019-12-02 13:10:33
问题 How does one put a CHECK constraint on a column such that its range of acceptable values come from another table, without hardcoding? Here's a simplified example: OneManyTable RoleID TaskID 10 Val1 10 Val2 20 Val1 20 Val2 MetaDataTable pkID Class Value 1 A Val1 2 A Val2 3 B Val3 4 B Val4 I want to put a CHECK Constraint on OneManyTable.TaskID column such that acceptable values come from another tables's column, i.e. from MetadataTable.Value where MetadataTable.class= 'A' I already tried

Constraint for phone number in SQL Server

强颜欢笑 提交于 2019-12-02 04:33:42
问题 Constraint for phone number to be of 7 digits. How to check if it is of 7 digits in SQL Server? CREATE TABLE Customer ( C_ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, C_Name VARCHAR(255) NOT NULL, Phone INT ); 回答1: Do not store phone numbers as integers. Some valid numbers, for instance, could start with a 0 -- if not today, perhaps in the future. To do the validation check, you can use like : CREATE TABLE Customer ( C_ID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY, C_Name VARCHAR(255) NOT NULL,

sql CHECK constraint not working properly [duplicate]

落花浮王杯 提交于 2019-12-02 03:19:24
This question already has an answer here: CHECK constraint in MySQL is not working 8 answers I have created a table schedule with a check constraint: mysql> create table schedule(order_date date, dely_date date check(dely_date>order_date)); Query OK, 0 rows affected (0.50 sec) When I insert a value which violates check constraint, sql reports no error. mysql> insert into schedule values('2015-11-20','2014-12-25'); Query OK, 1 row affected (0.10 sec) mysql> select * from schedule; +------------+------------+ | order_date | dely_date | +------------+------------+ | 2015-11-20 | 2014-12-25 | +---

Constraint for phone number in SQL Server

血红的双手。 提交于 2019-12-01 23:21:27
Constraint for phone number to be of 7 digits. How to check if it is of 7 digits in SQL Server? CREATE TABLE Customer ( C_ID INT NOT NULL IDENTITY(1,1) PRIMARY KEY, C_Name VARCHAR(255) NOT NULL, Phone INT ); Do not store phone numbers as integers. Some valid numbers, for instance, could start with a 0 -- if not today, perhaps in the future. To do the validation check, you can use like : CREATE TABLE Customer ( C_ID INT NOT NULL IDENTITY(1, 1) PRIMARY KEY, C_Name VARCHAR(255) NOT NULL, Phone CHAR(7), -- you might not want to have such a precise length CONSTRAINT chk_phone CHECK (phone not like

Oracle SQL - can CASE be used in a check constraint to determine data attributes?

坚强是说给别人听的谎言 提交于 2019-12-01 21:25:15
问题 I'm using Oracle 10g and I want to apply a constraint to a table where the value entered for one column determines whether another column IS NULL or IS NOT NULL. Column1 can only contain 1 or 0; Column2 is VARCHAR2(255). I know that the following works: CONSTRAINT ck_1 CHECK ((col1=1 AND col2 IS NOT NULL) OR (col1=0 AND col2 IS NULL)); However, I was wondering if it is possible to use CASE to perform this constraint and set the attribute NOT NULL on col2, or can CASE only be used to define

CHECK CONSTRAINT in Oracle SQL

非 Y 不嫁゛ 提交于 2019-12-01 06:03:48
问题 I have the following table Goods_In_Wagon(Goods_ID,Wagon_ID,Total_Weight). I need to create a number of if statement check constraint that says "IF WAGON_ID is between 90 and 99 THEN Total_Weight must be greater than 10." AND "IF WAGON_ID is between 100 and 110 THEN Total_Weight must be greater than 20." AND "IF WAGON_ID is between 111 and 120 THEN Total_Weight must be greater than 30." is this possible in SQL Oracle, if it is how can I implement it 回答1: Use an out-of-line constraint: CREATE

Does MySQL support check constraint? [duplicate]

半世苍凉 提交于 2019-12-01 05:24:51
问题 This question already has answers here : CHECK constraint in MySQL is not working (8 answers) Closed 9 months ago . Does MySQL support check constraint? I am able to execute the following script in MySQL without error. ALTER TABLE EMP_DB_DESIGN_EXCEL ADD ( CONSTRAINT CHK_EMP_IS_ACTIVE CHECK (IS_ACTIVE IN ('Y','N'))); But it is not reflected if I query: SELECT * FROM information_schema.TABLE_CONSTRAINTS T where t.table_name='EMP_DB_DESIGN_EXCEL'; 回答1: So far as I can tell from past usage and