Skip to main content
Commonmark migration
Source Link

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

 

Msg 1750, Level 16, State 0, Line 21
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

 

Msg 1750, Level 16, State 0, Line 21
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Msg 1750, Level 16, State 0, Line 21
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Tweeted twitter.com/StackDBAs/status/903695445683306496
edited title
Source Link
Hannah Vernon
  • 71.1k
  • 22
  • 178
  • 325

Preferred design to avoid circular/multiple update paths

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer JosephJoseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keysonly natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
  
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
  
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Msg

Msg 1750, Level 16, State 0, Line 21
  
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascadeI want updates and deletes to cascade.

Preferred design to avoid circular update paths

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
  Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
  Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Msg 1750, Level 16, State 0, Line 21
  Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Preferred design to avoid circular/multiple update paths

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21 
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths. 
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

Msg 1750, Level 16, State 0, Line 21 
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server?

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Farmer Joe owns land. Farmer Ted owns land. Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields. If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table so Farmer Ted knows who owns that combine harvester he's been using.Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys;keys, but I'm trying to determine how this would work if you're using only natural keysonly natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
  
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths. Specify
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
  

Msg 1750, Level 16, State 0, Line 21
  
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server? FYI, 

I realize I can just remove the ON UPDATE xxx clauses, but that's not really the point. I want updates and deletes to cascadeI want updates and deletes to cascade.

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Farmer Joe owns land. Farmer Ted owns land. Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields. If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys; I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21
  Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
  Msg 1750, Level 16, State 0, Line 21
  Could not create constraint. See previous errors.

How should this model be expressed in SQL Server? FYI, I realize I can just remove the ON UPDATE xxx clauses, but that's not really the point. I want updates and deletes to cascade.

Consider the following design:

CREATE TABLE dbo.Farmers ( FarmerName varchar(10) NOT NULL PRIMARY KEY ); CREATE TABLE dbo.FarmEquipment ( FarmEquipmentName varchar(10) NOT NULL PRIMARY KEY , CreatorFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE ); CREATE TABLE dbo.Fields ( FieldName varchar(10) NOT NULL , OwnerFarmer varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.Farmers(FarmerName) ON UPDATE CASCADE ON DELETE CASCADE , FarmEquipment varchar(10) NOT NULL FOREIGN KEY REFERENCES dbo.FarmEquipment(FarmEquipmentName) ON UPDATE CASCADE ON DELETE CASCADE ); 

Use case:

  • Farmer Joe owns land.
  • Farmer Ted owns land.
  • Farmer Joe builds a combine-harvester, and loans it to Farmer Ted for use on one of Farmer Ted's fields.
  • If Farmer Joe changes his name to Farmer Joseph, I want that to reflect in the Fields table, so Farmer Ted knows who owns that combine harvester he's been using.

I realize this is the exact use-case for surrogate keys, but I'm trying to determine how this would work if you're using only natural keys.

In SQL Server, you cannot actually create this structure, since the ON UPDATE CASCADE and ON UPDATE DELETE clauses in dbo.Fields produce the following error message:

Msg 1785, Level 16, State 0, Line 21 
Introducing FOREIGN KEY constraint 'FK__Fields__FarmEqui__3AD6B8E2' on table 'Fields' may cause cycles or multiple cascade paths.
Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 

Msg 1750, Level 16, State 0, Line 21 
Could not create constraint. See previous errors.

How should this model be expressed in SQL Server? 

I realize I can just remove the ON UPDATE clauses, but that's not really the point. I want updates and deletes to cascade.

Added tag that I consider relevant.
Source Link
MDCCL
  • 8.5k
  • 3
  • 32
  • 65
Loading
added 139 characters in body
Source Link
Hannah Vernon
  • 71.1k
  • 22
  • 178
  • 325
Loading
added 316 characters in body
Source Link
Hannah Vernon
  • 71.1k
  • 22
  • 178
  • 325
Loading
Source Link
Hannah Vernon
  • 71.1k
  • 22
  • 178
  • 325
Loading