Check the value range : Check value range « Constraints « SQL Server / T-SQL






Check the value range


22>
23> CREATE TABLE MySavings(AccountNum Int NOT NULL,
24>                        Amount Money NOT NULL)
25>
26> CREATE TABLE MyChecking(AccountNum Int NOT NULL,
27>                         Amount Money NOT NULL)
28>
29> ALTER TABLE MyChecking ADD CONSTRAINT ckMinBalance
30> CHECK (Amount > $100.00)
31>
32> INSERT MySavings VALUES (12345, $1000.00)
33>
34> INSERT MyChecking VALUES (12345, $1000.00)
35> GO

(1 rows affected)

(1 rows affected)
1> select * from mysavings
2> go
AccountNum  Amount
----------- ---------------------
      12345             1000.0000

(1 rows affected)
1>
2> select * from mychecking
3> GO
AccountNum  Amount
----------- ---------------------
      12345             1000.0000

(1 rows affected)
1>
2> BEGIN TRANSACTION
3>
4>     UPDATE MyChecking SET Amount = Amount - $990.00
5>     WHERE AccountNum = 12345
6>
7>     UPDATE MySavings SET Amount = Amount + $990.00
8>     WHERE AccountNum = 12345
9>
10> COMMIT TRANSACTION
11> GO
Msg 547, Level 16, State 1, Server JAVA2S\SQLEXPRESS, Line 4
The UPDATE statement conflicted with the CHECK constraint "ckMinBalance". The conflict occurred in database "master", table "dbo.MyChecking",
The statement has been terminated.

(1 rows affected)
1>
2>
3> drop table MySavings
4> drop table MyChecking
5> GO
1>
2>
           
       








Related examples in the same category

1.CHECK Constraints for current date
2.Add check constaint to only allow positive value