An INSERT statement that fails because a related row doesn't exist : Insert « Insert Delete Update « SQL Server / T-SQL Tutorial






28> CREATE TABLE Bankers
29> (BankerID        INT NOT NULL PRIMARY KEY,
30> BankerName       VARCHAR(50) NOT NULL)
31> GO
1>
2> --A statement that creates the foreign key table
3>
4> CREATE TABLE Billings
5> (BillingID       INT NOT NULL PRIMARY KEY,
6> BankerID         INT NOT NULL REFERENCES Bankers (BankerID),
7> BillingTotal     MONEY NULL)
8> GO
1>
2> --An INSERT statement that fails because a related row doesn't exist
3>
4> INSERT Billings VALUES (1, 99, 100)
5> GO
Msg 547, Level 16, State 1, Server J\SQLEXPRESS, Line 4
The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Billings__Banker__5F9EF494". The conflict occurred in database "master", table "dbo.Bankers", column 'BankerID'.
The statement has been terminated.
1>
2>
3> drop table Billings;
4> drop table Bankers;
5> GO








2.1.Insert
2.1.1.The syntax of the INSERT statement for inserting a single row
2.1.2.INSERT Statement
2.1.3.Inserting Data for a Subset of Table Columns
2.1.4.The insertion of values into some (but not all) of a table's columns usually requires the explicit specification of the corresponding columns.
2.1.5.The order of column names in the VALUE clause of the INSERT statement can be different from the original order of those columns.
2.1.6.An INSERT statement that adds the new row using a column list
2.1.7.INSERT statement with a column list
2.1.8.INSERT...DEFAULT VALUES
2.1.9.Use declared variables in insert statement
2.1.10.An INSERT statement that fails because a related row doesn't exist
2.1.11.eliminate the optional column list and allow SQL Server to assume we're providing something for every column
2.1.12.How to add rows to a table based on the output of a stored procedure