SQL Server Foreign Key references invalid Table 'employees' - sql-server

I try to run this SQL Server query:
USE DB_UBB;
CREATE TABLE dept_emp (
emp_no INT NOT NULL,
dept_no CHAR(4) NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees(emp_no) ON DELETE CASCADE, -- Error here
FOREIGN KEY (dept_no) REFERENCES departments(dept_no) ON DELETE CASCADE, -- And here
PRIMARY KEY (emp_no, dept_no)
);
CREATE INDEX (emp_no);
CREATE INDEX (dept_no);
and I get these errors:
Foreign key 'FK__dept_emp__8bc6840bee39d6cef4bd' references invalid table 'employees'.
Foreign key 'fk__dept_emp__99bc0b2304d3f32059a9' references invalid table 'departments'.
even though I have these tables:
What do I do wrong?
EDIT:
Added Whole DB:

SQL ‘hides’ the columns from the Key of the Clustered Index in Nonclustered Indexes.
You have created a composite primary on both emp_no,dept_no
cluster index of primary will hide both columns from indexes in following queries and will generate error
CREATE INDEX (emp_no);
CREATE INDEX (dept_no);

If you are specifying the foreign key after the column specifications, try using the CONSTRAINT clause instead:
to_date DATE NOT NULL,
CONSTRAINT fk_dept_emp_dept FOREIGN KEY (emp_no) REFERENCES employees(emp_no) ON DELETE CASCADE,
CONSTRAINT fk_dept_emp_emp FOREIGN KEY (dept_no) REFERENCES departments(dept_no) ON DELETE CASCADE,

Aparently, I even though the tables were created, it didn't recognize them.
I added an <if not exist, create tables>
I also removed the CREATE INDEX (emp_no); and CREATE INDEX (dept_no); as #Muhammad Nasir said, but it didn't fix the referencing issue.
Solution:
USE DB_UBB;
IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE name='employees' and xtype='U')
CREATE TABLE employees (
emp_no INT NOT NULL,
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender VARCHAR(1) NOT NULL CHECK (gender IN('M', 'F')),
hire_date DATE NOT NULL,
PRIMARY KEY (emp_no)
);
GO
IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE name='departments' and xtype='U')
CREATE TABLE departments (
dept_no CHAR(4) NOT NULL,
dept_name VARCHAR(40) NOT NULL,
PRIMARY KEY (dept_no),
UNIQUE (dept_name)
);
GO
IF NOT EXISTS (SELECT * FROM SYSOBJECTS WHERE name='dept_emp' and xtype='U')
CREATE TABLE dept_emp (
emp_no INT NOT NULL,
dept_no CHAR(4) NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
FOREIGN KEY (emp_no) REFERENCES employees(emp_no) ON DELETE CASCADE,
FOREIGN KEY (dept_no) REFERENCES departments(dept_no) ON DELETE CASCADE,
PRIMARY KEY (emp_no, dept_no)
);
GO

Related

Can't create tables with Foreign Key of each other

Is there a way to create both tables while being a foreign key of one another? I am using SQL Server Management Studio
--DROP TABLE orders
CREATE TABLE orders
(
orderId bigint PRIMARY KEY IDENTITY(880001, 1) NOT NULL,
receiptNo bigint FOREIGN KEY REFERENCES receipt(receiptId),
productId bigint FOREIGN KEY REFERENCES productServices(productId),
quantity int,
dateOrdered datetime DEFAULT CURRENT_TIMESTAMP
)
--DROP TABLE receipt
CREATE TABLE receipt
(
receiptNo bigint PRIMARY KEY IDENTITY(900001, 1) NOT NULL,
employeeId bigint FOREIGN KEY REFERENCES employeeInfo(employeeId),
customerId bigint FOREIGN KEY REFERENCES customerInfo(customerId),
orderId bigint FOREIGN KEY REFERENCES orders(orderId),
paymentMethod varchar(4),
dateOfPurchase datetime DEFAULT CURRENT_TIMESTAMP
)
The requirement always is: a table must exist before you can create a foreign key reference to it. So in your case, obviously, you cannot create both table with the full FK references in place, since they reference each other.
What you need to do is:
CREATE TABLE orders without the FK reference to Receipt (since that table doesn't exist yet at this time)
CREATE TABLE receipt - here you can include the FK reference to orders - that table has been created and exists
Alter your table orders to add the FK reference to receipt:
ALTER TABLE dbo.orders
ADD CONSTRAINT FK_Orders_Receipt
FOREIGN KEY (receiptNo) REFERENCES dbo.receipt(receiptId);
Of course, the same applies to all the other FK constraints you have - you cannot reference a table that doesn't exist yet. You must first create the tables, then you can add the FK constraints.
For you Case, you change the order of creating the Table.
1.) Before adding a Foreign Key References to a new table, the table to be refereed as a foreign key must be already exists.
2.) Also a add a batch Separator GO statement while single Execution of creating tables.
--DROP TABLE receipt
CREATE TABLE receipt
(
receiptNo bigint PRIMARY KEY IDENTITY(900001, 1) NOT NULL,
employeeId bigint FOREIGN KEY REFERENCES employeeInfo(employeeId),
customerId bigint FOREIGN KEY REFERENCES customerInfo(customerId),
orderId bigint FOREIGN KEY REFERENCES orders(orderId),
paymentMethod varchar(4),
dateOfPurchase datetime DEFAULT CURRENT_TIMESTAMP
)
GO
--DROP TABLE orders
CREATE TABLE orders
(
orderId bigint PRIMARY KEY IDENTITY(880001, 1) NOT NULL,
receiptNo bigint FOREIGN KEY REFERENCES receipt(receiptId),
productId bigint FOREIGN KEY REFERENCES productServices(productId),
quantity int,
dateOrdered datetime DEFAULT CURRENT_TIMESTAMP
)
GO
This is hard because it's wrong. There's no need for two FKs and they are actively harmful as you could have an order whose receipt points to a different order. The right design here is to just have one FK, and enforce uniqueness on the FK column. Thus an order may be created first, and later at most one receipt can be added for that order.
eg:
create table employeeInfo(employeeid bigint primary key)
create table customerInfo(customerID bigint primary key)
create table productServices(productId bigint primary key)
CREATE TABLE orders
(
orderId bigint PRIMARY KEY IDENTITY(880001, 1) NOT NULL,
--receiptNo bigint FOREIGN KEY REFERENCES receipt(receiptId),
productId bigint FOREIGN KEY REFERENCES productServices(productId),
quantity int,
dateOrdered datetime DEFAULT CURRENT_TIMESTAMP
)
CREATE TABLE receipt
(
receiptNo bigint PRIMARY KEY IDENTITY(900001, 1) NOT NULL,
employeeId bigint FOREIGN KEY REFERENCES employeeInfo(employeeId),
customerId bigint FOREIGN KEY REFERENCES customerInfo(customerId),
orderId bigint FOREIGN KEY REFERENCES orders(orderId) unique,
paymentMethod varchar(4),
dateOfPurchase datetime DEFAULT CURRENT_TIMESTAMP
)

Can't create the tables because the primary keys used as foreign keys in another table

Create Table Customer
(
Cus_id Varchar2(10) NOT NULL Primary Key,
Cus_FirstName Varchar2(20) NOT NULL,
Cus_LastName Varchar2 (20) NOT NULL,
Cus_Address Varchar2(25) NOT NULL,
Cus_City_State_Zip Varchar2(30) NOT NULL
);
Create Table Order_
(
Order_id Varchar2(10) NOT NULL Primary Key,
Order_Date Varchar2(12) NOT NULL,
Order_Status Varchar(12) NOT NULL
);
Create Table Item
(
Item_id Varchar2(10) NOT NULL Primary Key,
Item_Name Varchar2(20) NOT NULL,
Item_Price Varchar2(10) NOT NULL
);
Create Table CustomerOrderItem
(
Quantity Varchar2(8) NOT NULL,
Cus_id Varchar(10) NOT NULL Primary Key,
Order_id Varchar(10) NOT NULL Primary Key,
Item_id Varchar(10) NOT NULL Primary Key,
Foreign Key(Cus_id) References Customer(Cus_id),
Foreign Key(Oreder_id) References Order_(Order_id),
Foreign Key(Item_id) References Item(Item_id)
);
This code is a customer table with repeated orders and multiple items for one order. When I try to insert values into the table and create them. I created the fourth table due to their dependency and put three primary keys there as primary keys and foreign keys. It does not let me create the tables saying because the primary keys used as foreign keys in another table, therefore I need to drop them first to create the tables.
Your data schema neither matches what you describe, nor is a correct syntax for SQL Server. Putting aside the bad design, this is what you are trying to do:
Create Table Customer ( Cus_id Varchar(10) NOT NULL Primary Key,
Cus_FirstName Varchar(20) NOT NULL,
Cus_LastName Varchar (20) NOT NULL,
Cus_Address Varchar(25) NOT NULL,
Cus_City_State_Zip Varchar(30) NOT NULL );
Create Table Order_ ( Order_id Varchar(10) NOT NULL Primary Key,
Order_Date Varchar(12) NOT NULL,
Order_Status Varchar(12) NOT NULL );
Create Table Item ( Item_id Varchar(10) NOT NULL Primary Key,
Item_Name Varchar(20) NOT NULL,
Item_Price Varchar(10) NOT NULL );
Create Table CustomerOrderItem ( Quantity Varchar(8) NOT NULL,
Cus_id Varchar(10) NOT NULL,
Order_id Varchar(10) NOT NULL,
Item_id Varchar(10) NOT NULL,
constraint PK_CustomerOrderItem primary key (cus_id, order_id, item_id),
constraint FK_COI_Customer Foreign Key(Cus_id) References Customer(Cus_id),
constraint FK_COI_Order Foreign Key(Order_id) References Order_(Order_id),
constraint FK_COI_Item Foreign Key(Item_id) References Item(Item_id) );
Check Northwind sample database for a better sample, and for even a better one check AdventureWorks sample database as starters.
the Error message you got ORA-02449 is due to trying to drop a table that is already referenced as primary key in CustomerOrderItem table ,to solve this issue drop the CustomerOrderItem table first then drop the other tables afterwards.
Good luck.

Enabling cascading deletes on either side of a join table

I have one User model with a primary key of id. I am currently modeling Supervisor > User relationships with a join table of sorts called user_supervisors which has only two columns, user_id and supervisor_id, both of which are foreign keys referencing the primary key of User record.
Is it possible that when I delete a user (whether they are a supervisor or not), any records containing that user in the user_supervisors table is also deleted?
I have been attempting to make use of sql server's cascading deletes but it doesn't seem to be possible to have cascading deletes on both the user_id and supervisor_id foreign keys at the same time. I always encounter a message along the lines of:
Knex:warning - migration failed with error: CREATE TABLE [user_supervisors] ([user_id] int, [supervisor_id] int, CONSTRAINT [user_supervisors_pkey] PRIMARY KEY ([user_id], [supervisor_id]), CONSTRAINT [user_supervisors_user_id_foreign] FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE CASCADE, CONSTRAINT [user_supervisors_supervisor_id_foreign] FOREIGN KEY ([supervisor_id]) REFERENCES [users] ([id]) ON DELETE CASCADE) - Could not create constraint or index. See previous errors.
For the record I am using knex to run my database migrations
Below is the SQL used when creating each table mentioned above:
CREATE TABLE [users]
(
[id] int identity(1,1) not null primary key
, [created_at] datetime
, [updated_at] datetime
, [email] nvarchar(255) not null
, [first_name] nvarchar(255)
, [last_name] nvarchar(255)
, [password] nvarchar(255) not null
, [company_id] int
, CONSTRAINT [users_email_unique] UNIQUE ([email])
, CONSTRAINT [users_company_id_foreign] FOREIGN KEY ([company_id]) REFERENCES [companies] ([id]) ON DELETE CASCADE
)
CREATE TABLE [user_supervisors]
(
[user_id] int
, [supervisor_id] int
, CONSTRAINT [user_supervisors_pkey] PRIMARY KEY ([user_id], [supervisor_id])
, CONSTRAINT [user_supervisors_user_id_foreign] FOREIGN KEY ([user_id]) REFERENCES [users] ([id]) ON DELETE CASCADE
, CONSTRAINT [user_supervisors_supervisor_id_foreign] FOREIGN KEY ([supervisor_id]) REFERENCES [users] ([id])
)

How to solve multiple cascade paths?

In my database, I have a Person and Member table. The Member table has 2 foreign keys which both reference the same column in the People Table. The constraints look like this:
CONSTRAINT [FK_Members_People_1]
FOREIGN KEY ([PersonID])
REFERENCES [People].[People]([ID])
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT [FK_Members_People_2]
FOREIGN KEY ([EnquiryTakenBy])
REFERENCES [People].[People]([ID])
ON DELETE SET NULL
ON UPDATE NO ACTION
The error I get is as follows:
Introducing FOREIGN KEY constraint 'FK_Members_People_2' on table 'Members' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
How would I go about solving this?
Basically this is the behaviour I'm trying to get:
When the record referenced by PersonID is removed. I need it to delete all child records in other tables. (There's 8 child tables)
When the record referenced by EnquiryTakenBy is deleted. I need EnquiryTakenBy to be set to null.
EDIT: Table structure is as follows:
CREATE TABLE [People].[People]
(
[ID] INT NOT NULL IDENTITY,
[PersonType] INT NOT NULL,
[Forename] VARCHAR(16) NOT NULL,
[Surname] VARCHAR(32) NOT NULL,
[Gender] CHAR(1) NOT NULL,
[DateOfBirth] DATE NULL,
[HobbiesAndInterests] VARCHAR(256) NULL,
[AdditionalInformation] VARCHAR(512) NULL,
[LocalCentre] INT NOT NULL DEFAULT 0,
CONSTRAINT [PK_People] PRIMARY KEY ([ID]),
CONSTRAINT [FK_People_PersonType]
FOREIGN KEY ([PersonType])
REFERENCES [Lookups].[PersonTypes]([ID])
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT [FK_People_Centres]
FOREIGN KEY ([LocalCentre])
REFERENCES [Lookups].[Centres]([ID])
ON DELETE CASCADE
ON UPDATE CASCADE
)
CREATE TABLE [People].[Members]
(
[PersonID] INT NOT NULL,
[IsActive] BIT NOT NULL DEFAULT 0,
[Issues] VARCHAR(500) NULL,
[InTreatment] BIT NOT NULL DEFAULT 0,
[ProblemSubstance] VARCHAR(64) NOT NULL,
[WantsHelpWith] VARCHAR(128) NULL,
[EnquiryTakenBy] INT NOT NULL,
[IsVolunteer] BIT NOT NULL DEFAULT 0,
CONSTRAINT [PK_Members] PRIMARY KEY ([PersonID]),
CONSTRAINT [FK_Members_People_1]
FOREIGN KEY ([PersonID])
REFERENCES [People].[People]([ID])
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT [FK_Members_People_2]
FOREIGN KEY ([EnquiryTakenBy])
REFERENCES [People].[People]([ID])
ON DELETE SET NULL
ON UPDATE NO ACTION
)
Ok, found a solution. Apparently my problem was something to do with SQL Server didn't know which constraint took precedence, so I replaced the second foreign key constraint with this trigger:
CREATE TRIGGER Member_UpdateEnquiryID ON People.People
AFTER DELETE
AS
BEGIN
DECLARE #id int = (select id from deleted);
UPDATE People.Members
SET EnquiryTakenBy = NULL
where EnquiryTakenBy = #id
END

Can't find where multiple cascade paths or cycles are in SQL Code?

I keep getting the following error
Msg 1785, Level 16, State 0, Line 99
Introducing FOREIGN KEY constraint 'FK_linkUserGroup_tblStudent' on table >'linkUserGroup' 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 99
Could not create constraint or index. See previous errors.
I cannot understand how a cycle or multiple cascade paths are possible? I also cannot understand how a FK constraints to the TeacherID are not causing the same problem? My understanding is that should the parent StudentID be deleted then by specifying that On Delete No Action in the child table should ensure that the record is not deleted from the child table. I don't understand how their is any other path or possible cycle? Please help
CREATE TABLE tblUserAccount (
uaUserAccountID INT IDENTITY (1,1) CONSTRAINT PK_tblUserAccount PRIMARY KEY CLUSTERED,
uaUserAccountTitle NVARCHAR (50) NOT NULL,
uaUserAccountFirstName NVARCHAR (50) NOT NULL,
uaUserAccountSurname NVARCHAR (50) NOT NULL,
uaUserAccountUserName NVARCHAR (50) NOT NULL,
uaUserAccountPassword NVARCHAR (50) NOT NULL,
uaDateTimeModified DATETIME
CONSTRAINT DF_tblUserAccount_DateTimeModified DEFAULT SYSDATETIME()
);
CREATE TABLE tblRole (
rRoleID INT IDENTITY (1, 1) CONSTRAINT PK_tblRole PRIMARY KEY CLUSTERED,
rRole NVARCHAR (50),
rRoleDescription NVARCHAR (MAX) NULL,
rDateTimeModified DATETIME
CONSTRAINT DF_tblRole_DateTimeModified DEFAULT SYSDATETIME()
);
CREATE TABLE linkUserAccountRole (
uarUserAccountID INT,
uarRoleID INT,
uarDateTimeModified DATETIME
CONSTRAINT DF_linkUserAccountRole_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT PK_linkUserAcccountRole
PRIMARY KEY CLUSTERED (uarUserAccountID , uarRoleID)
,CONSTRAINT FK_linkUserAcccountRole_tblRole
FOREIGN KEY (uarRoleID)
REFERENCES tblRole (rRoleID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserAcccountRole_tblUserAccount
FOREIGN KEY (uarUserAccountID)
REFERENCES tblUserAccount (uaUserAccountID)
ON DELETE NO ACTION
ON UPDATE CASCADE
);
CREATE TABLE tblTeacher (
tTeacherID INT CONSTRAINT PK_tblTeacher PRIMARY KEY CLUSTERED,
tUserAccountID INT,
tDateTimeModified DATETIME
CONSTRAINT DF_tblTeacher_DateTimeModified DEFAULT SYSDATETIME(),
CONSTRAINT FK_tblTeacher_tblUserAccount
FOREIGN KEY (tUserAccountID)
REFERENCES tblUserAccount(uaUserAccountID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE tblStudent (
sStudentID INT CONSTRAINT PK_tblStudent PRIMARY KEY CLUSTERED,
sUserAccountID INT,
sDateTimeModified DATETIME
CONSTRAINT DF_tblStudent_DateTimeModified DEFAULT SYSDATETIME(),
CONSTRAINT FK_tblStudent_tblUserAccount
FOREIGN KEY (sUserAccountID)
REFERENCES tblUserAccount(uaUserAccountID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE tblKeyStage (
ksKeyStageGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblKeyStage PRIMARY KEY CLUSTERED,
ksKeyStageTitle NVARCHAR (50) NOT NULL,
ksKeyStageDescription NVARCHAR(50),
ksDateTimeModified DATETIME
CONSTRAINT DF_tblKeyStage_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblKeyStage_KeyStageTitle UNIQUE (ksKeyStageTitle)
);
CREATE TABLE tblYearGroup (
ygYearGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblYearGroup PRIMARY KEY CLUSTERED,
ygYearGroupTitle NVARCHAR (50) NOT NULL,
ygYearGroupDescription NVARCHAR(50),
ygDateTimeModified DATETIME
CONSTRAINT DF_tblYearGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblYearGroup_YearGroupTitle UNIQUE (ygYearGroupTitle)
);
CREATE TABLE tblClassGroup (
cgClassGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblClassGroup PRIMARY KEY CLUSTERED,
cgClassGroupTitle NVARCHAR (50) NOT NULL,
cgClassGroupDescription NVARCHAR (50),
cgDateTimeModified DATETIME
CONSTRAINT DF_tblClassGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblClassGroup_ClassGroupTitle UNIQUE (cgClassGroupTitle)
);
CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED,
lgLearningGroupTitle NVARCHAR (50) NOT NULL,
lgLearningGroupDescription NVARCHAR (50),
lgDateTimeModified DATETIME
CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle)
);
CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED,
lgLearningGroupTitle NVARCHAR (50) NOT NULL,
lgLearningGroupDescription NVARCHAR (50),
lgDateTimeModified DATETIME
CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle)
);
CREATE TABLE linkUserGroup (
ugTeacherID INT,
ugStudentID INT,
ugKeyStageGroupID INT,
ugYearGroupID INT,
ugClassGroupID INT,
ugLearningGroupID INT,
ugDateTimeModified DATETIME
CONSTRAINT DF_linkUserGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT PK_linkUserGroup
PRIMARY KEY CLUSTERED (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID)
,CONSTRAINT FK_linkUserGroup_tblTeacher
FOREIGN KEY (ugTeacherID)
REFERENCES tblTeacher (tTeacherID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblStudent
FOREIGN KEY (ugStudentID)
REFERENCES tblStudent (sStudentID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblKeyStage
FOREIGN KEY (ugKeyStageGroupID)
REFERENCES tblKeyStage (ksKeyStageGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblYearGroup
FOREIGN KEY (ugYearGroupID)
REFERENCES tblYearGroup (ygYearGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblClassGroup
FOREIGN KEY (ugClassGroupID)
REFERENCES tblClassGroup (cgClassGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblLearningGroup
FOREIGN KEY (ugLearningGroupID)
REFERENCES tblLearningGroup (lgLearningGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT UQ_linkUserGroup_PK_YearGroupID
UNIQUE (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID, ugYearGroupID)
);
Multiple cascade paths exist between tblUserAccount and linkUserGroup:
tblUserAccount -> tblTeacher -> linkUserGroup
tblUserAccount -> tblStudent -> linkUserGroup
It is not the FK constraint itself which is not allowed, but the action: ON UPDATE CASCADE.
You can either specify ON UPDATE CASCADE for FK_linkUserGroup_tblTeacher, or ON UPDATE CASCADE for FK_linkUserGroup_tblStudent, but not for both.
Your ON DELETE is already marked as NO ACTION, so it is fine. It is ON UPDATE which you should decide what to do.
There are 2 options:
1) Forbid updating the key which participates in a FK constraint.
To do that specify ON UPDATE NO ACTION, or just remove the ON UPDATE clause altogether, because this is the default behaviour of a foreign key constraint.
Considering that updating a primary key is usually not a good idea (whether it is referenced by a foreign key or not), I would recommend this option.
2) If automatic propagation of changed sStudentID and tTeacherID to linkUserGroup is really what you want, then you can use triggers.

Resources