Exception in a transaction : Constriant violation « Constraints « SQL Server / T-SQL Tutorial

Home
SQL Server / T-SQL Tutorial
1.Query
2.Insert Delete Update
3.Table
4.Table Join
5.Data Types
6.Set Operations
7.Constraints
8.Subquery
9.Aggregate Functions
10.Date Functions
11.Math Functions
12.String Functions
13.Data Convert Functions
14.Analytical Functions
15.Sequence Indentity
16.View
17.Index
18.Cursor
19.Database
20.Transact SQL
21.Procedure Function
22.Trigger
23.Transaction
24.XML
25.System Functions
26.System Settings
27.System Tables Views
28.User Role
29.CLR
SQL Server / T-SQL Tutorial » Constraints » Constriant violation 




2CREATE TABLE MySavings(
3>     AccountNum Int NOT NULL,
4>     Amount Money NOT NULL
5);
6> GO
1>
2CREATE TABLE MyChecking(
3>     AcountNum Int NOT NULL,
4>     Amount Money NOT NULL
5);
6> GO
1>
2> ALTER TABLE MyChecking ADD CONSTRAINT ckMinBalance
3> CHECK (Amount > $100.00)
4> GO
1INSERT MySavings VALUES (12345, $1000.00)
2> GO

(rows affected)
1>
2INSERT MyChecking VALUES (12345, $1000.00)
3> GO

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

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














7.12.Constriant violation
7.12.1.Trap Both Primary Key and CHECK Constraint Violations
7.12.2.Exception in a transaction
7.12.3.A NOT NULL phrase adds a constraint to restrict the input of rows with a null value.
7.12.4.Update statement and check-constraints
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.