MSSQL - set limit for a column value - sql

I have a table named Test with the following columns.
id PK, int, not null
amount money, not null
I want to set a limit on the amount, say, 1000. I don't want anyone to insert a value greater than 1000 in this column. Can anyone help me on how to do this?

Something like this
CREATE TABLE tablename
(
-------
--------
amount money,
CONSTRAINT chk_amount CHECK (amount <= 1000)
)

You can add a check constraint, like this:
ALTER TABLE Test
ADD CONSTRAINT chk_money CHECK (amount<=1000)

You can create check constraint for your table.
alter table dbo.Your_Table with check add constraint your_Table_Amount check (Amount <= 1000)
go
alter table dbo.Your_Table check constraint your_Table_Amount

Related

Constraint on column to set limit of possible values

I need to put a constraint on a column so that it can only contain the following range of values
Allowed values: between 1 and 10
Column Data Type: tinyint
DBMS or Docker Image: Microsoft SQL Server - mcr.microsoft.com/mssql/server:2019-latest
I believe it should be something close to this
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT chk_val_limit CHECK (Priority in (between 1 and 10))
GO
You can try this one!
ALTER TABLE [dbo].[Projects]
ADD CONSTRAINT chk_val_limit CHECK (ColumnName > 1 AND ColumnName < 10)
GO

sql current date constraint

I need to add a constraint to one table in my database. The table name is Experience. And there is a column named ToDate. Every time the select statement executes like following.
select ToDate from Experience
It should return current date.
So every time select statement executes, the ToDate column get updated with current date.
I know I can do this with some type of sql trigger but is there a way to do it by sql constraint.
like
alter table add constraint...
Any help will be appreciated.
Thanks
You can use a computed column. That's specified like colname as <expression>:
create table t1(id int, dt as getdate());
insert t1 values (1);
select * from t1;
To add contraint ...
create table tbl (id int identity, dt datetime, colval varchar(10))
ALTER TABLE dbo.tbl
ADD CONSTRAINT col_dt_def
DEFAULT GETDATE() FOR dt;
Example of inserting to the table ..
insert into dbo.tbl(colval)
select 'somevalue'
select * from dbo.tbl
The result will be ..
id dt colval
1 2014-08-19 13:31:57.577 somevalue
You cannot use a constraint, because a constraint is basically a rule on what can go in the table, how the table can relate to others, etc. It has no bearing on the data in the table once it goes into the table. Now if I am understanding you correctly, you want to update the ToDate column whenever you select that column. Now you can't use a trigger either as mentioned here and here. They suggest a stored procedure where you would use an update followed by an insert. This is probably my preferred SQL method to go with if you have to use it repeated, which you seem to have to do. Though Andomar's answer is probably better.
Try this link code make help full
http://www.sqlatoms.com/queries/how-to-use-the-getdate-function-in-sql-server-3/
CREATE TABLE ProductOrders
(
OrderId int NOT NULL PRIMARY KEY IDENTITY,
ProductName nvarchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT GETDATE()
)

SQL Table Constraint to prevent column totals exceeding 100

I have a table which contains columns like this:
SomeId, Int PK
Item1Weighting, Int
Item2Weighting, Int
Item3Weighting, Int
I want to add a constraint to the table that prevents the total of the three "weighting" columns on a single row exceeding a total value of 100.
I've done quite a bit of searching and can't find any help so any suggestions would be gratefully received.
Thanks
Kev
You can do this declaratively without resorting to triggers.
CREATE TABLE T
(
SomeId Int PRIMARY KEY,
Item1Weighting Int,
Item2Weighting Int,
Item3Weighting Int,
CONSTRAINT CK_WeightingNotOver100
CHECK ((ISNULL(Item1Weighting,0) +
ISNULL(Item2Weighting,0) +
ISNULL(Item3Weighting,0)) <= 100)
)
Or to add it retrospectively to an existing table
ALTER TABLE T
ADD CONSTRAINT CK_WeightingNotOver100
CHECK ((ISNULL(Item1Weighting,0) +
ISNULL(Item2Weighting,0) +
ISNULL(Item3Weighting,0)) <= 100)

SQL Date option

I have a little doubt, I want to create a table that had a date that can't be bigger the 2012/12/31, i searched on google but only had exemples on SELECT. I'm gonna put an example:
CREATE TABLE example(
IDExample number (8) primary key,
DateExample date // Here i want to put that condition, is it possible?
);
If you're using SQL Server you can add check contraint on the column in the following way.
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < '20130101')
If you're using Oracle, the syntax is very similar:
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < DATE '2013-01-01')

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

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 table definition and give it a meaningful name so it is obvious which column it applies to.
alter table foo
add constraint Foobar_NonNegative
check (Foobar > 0)
You can get out the text of check constraints from the system data dictionary in sys.check_constraints:
select name
,description
from sys.check_constraints
where name = 'Foobar_NonNegative'
Create a database constraint:
ALTER TABLE Table1 ADD CONSTRAINT Constraint1 CHECK (YourCol > 0)
You can have pretty sophisticated constraints, too, involving multiple columns. For example:
ALTER TABLE Table1 ADD CONSTRAINT Constraint2 CHECK (StartDate<EndDate OR EndDate IS NULL)
I believe you want to add a CONSTRAINT to the table field:
ALTER TABLE tableName WITH NOCHECK
ADD CONSTRAINT constraintName CHECK (columnName > 0)
That optional NOCHECK is used to keep the constraint from being applied to existing rows of data (which could contain invalid data) & to allow the constraint to be added.
Add a CHECK constraint when creating your table
CREATE TABLE Test(
[ID] [int] NOT NULL,
[MyCol] [int] NOT NULL CHECK (MyCol > 1)
)
you can alter your table and add new constraint like bellow.
BEGIN TRANSACTION
GO
ALTER TABLE dbo.table1 ADD CONSTRAINT
CK_table1_field1 CHECK (field1>0)
GO
ALTER TABLE dbo.table1 SET (LOCK_ESCALATION = TABLE)
GO
COMMIT