check-constraints

In SQL Server 2005, how do I set a column of integers to ensure values are greater than 0?

时光怂恿深爱的人放手 提交于 2019-12-01 04:17:48
This is probably a simple answer but I can't find it. I have a table with a column of integers and I want to ensure that when a row is inserted that the value in this column is greater than zero. I could do this on the code side but thought it would be best to enforce it on the table. Thanks! I was in error with my last comment all is good now. You can use a check constraint on the column. IIRC the syntax for this looks like: create table foo ( [...] ,Foobar int not null check (Foobar > 0) [...] ) As the poster below says (thanks Constantin), you should create the check constraint outside the

In SQL Server 2005, how do I set a column of integers to ensure values are greater than 0?

本秂侑毒 提交于 2019-12-01 01:32:36
问题 This is probably a simple answer but I can't find it. I have a table with a column of integers and I want to ensure that when a row is inserted that the value in this column is greater than zero. I could do this on the code side but thought it would be best to enforce it on the table. Thanks! I was in error with my last comment all is good now. 回答1: You can use a check constraint on the column. IIRC the syntax for this looks like: create table foo ( [...] ,Foobar int not null check (Foobar >

Add constraint to existing SQLite table

孤街醉人 提交于 2019-11-30 18:24:27
I'm using SQLite, which doesn't support adding a constraint to an existing table. So I can't do something like this (just as an example): ALTER TABLE [Customer] ADD CONSTRAINT specify_either_phone_or_email CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL)); Are there any workarounds for this scenario? I know: I can add a constraint for a new table, but it isn't new (and it's generated by my ORM, EF Core) I can do a "table rebuild" (rename table, create new one, copy old data, drop temp table) but that seems really complex Ideas Can I somehow make a copy of the table into a new table, with

SQL can I have a “conditionally unique” constraint on a table?

折月煮酒 提交于 2019-11-30 08:17:25
问题 I've had this come up a couple times in my career, and none of my local peers seems to be able to answer it. Say I have a table that has a "Description" field which is a candidate key, except that sometimes a user will stop halfway through the process. So for maybe 25% of the records this value is null, but for all that are not NULL, it must be unique. Another example might be a table which must maintain multiple "versions" of a record, and a bit value indicates which one is the "active" one.

Can an SQL constraint be used to prevent a particular value being changed when a condition holds?

吃可爱长大的小学妹 提交于 2019-11-30 04:18:31
问题 I know that SQL constraints can force data to meet validity criteria. However, what about criteria such as "Student's grade can only be updated when the 'finalised' flag is false"? Do such update criteria have to be handled by the application? 回答1: Short answer: No, SQL constraints cannot in themselves prevent a change to column Grade when Finalized is 'true' (but allow a change otherwise). There are several kinds of SQL constraints: CHECK, DEFAULT, NOT NULL, UNIQUE, Primary Key, and Foreign

PostgreSQL check constraint for foreign key condition

佐手、 提交于 2019-11-30 04:02:31
I have a table of users eg: create table "user" ( id serial primary key, name text not null, superuser boolean not null default false ); and a table with jobs: create table job ( id serial primary key, description text ); the jobs can be assigned to users, but only for superusers. other users cannot have jobs assigned. So I have a table whereby I see which job was assigned to which user: create table user_has_job ( user_id integer references "user"(id), job_id integer references job(id), constraint user_has_job_pk PRIMARY KEY (user_id, job_id) ); But I want to create a check constraint that

Add constraint to existing SQLite table

泪湿孤枕 提交于 2019-11-30 01:42:27
问题 I'm using SQLite, which doesn't support adding a constraint to an existing table. So I can't do something like this (just as an example): ALTER TABLE [Customer] ADD CONSTRAINT specify_either_phone_or_email CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL)); Are there any workarounds for this scenario? I know: I can add a constraint for a new table, but it isn't new (and it's generated by my ORM, EF Core) I can do a "table rebuild" (rename table, create new one, copy old data, drop temp

How can I create a CHECK constraint on a VARCHAR column in SQL Server specifying a minimum data length?

不打扰是莪最后的温柔 提交于 2019-11-29 09:15:20
I have a VARCHAR(30) column in a Microsoft SQL Server database. I'd like to add a CHECK constraint that does not allow a value in the column to be less than 3 characters. What is the expression I must use? use: ALTER TABLE [dbo].[YOUR_TABLE] ADD CONSTRAINT [MinLengthConstraint] CHECK (DATALENGTH([your_column]) > 2) Reference: DATALENGTH vs LEN SQL SERVER – 2005 Constraint on VARCHAR(MAX) Field To Limit It Certain Length 来源: https://stackoverflow.com/questions/4426005/how-can-i-create-a-check-constraint-on-a-varchar-column-in-sql-server-specifying

Simple CHECK Constraint not so simple

妖精的绣舞 提交于 2019-11-27 23:23:34
2nd Edit : The source code for the involved function is as follows: ALTER FUNCTION [Fileserver].[fn_CheckSingleFileSource] ( @fileId INT ) RETURNS INT AS BEGIN -- Declare the return variable here DECLARE @sourceCount INT ; -- Add the T-SQL statements to compute the return value here SELECT @sourceCount = COUNT(*) FROM Fileserver.FileUri WHERE FileId = @fileId AND FileUriTypeId = Fileserver.fn_Const_SourceFileUriTypeId() ; -- Return the result of the function RETURN @sourceCount ; END Edit : The example table is a simplification. I need this to work as a Scaler Function / CHECK CONSTRAINT

SQL Server: How to make server check all its check constraints?

你离开我真会死。 提交于 2019-11-27 18:47:50
It seems that some scripts generated by Enterprise Manager* (or not, it doesn't matter) created check constraints WITH NOCHECK . Now when anyone modifies the table, SQL Server is stumbling across failed check constraints , and throwing errors. Can i make SQL go through all its check constraints, and check them? Running: sp_msforeachtable 'ALTER TABLE ? CHECK CONSTRAINT all' only enables previously disabled check constraints, it doesn't actually check them. Footnotes * SQL Server 2000 Nathan DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS won't actually make your constraints trusted. It will report