Getting error on create table with foreign key constraint (Cascade) - sql-server

create table employee
(
eid int primary key,
ename varchar(50),
cid int,
sid int,
constraint fk_hello1 foreign key(cid) references country(cid)on delete
cascade on update cascade,
constraint fk_hello2 foreign key(sid) references state(sid) on delete
cascade on update cascade,
)
I have been trying to apply this code but am constantly getting the error msg.........
ERROR MESSAGE
Msg 1785, Level 16, State 0, Line 1
Introducing FOREIGN KEY constraint 'fk_hello2' on table 'employee' 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 1
Could not create constraint. See previous errors.

You can not use on update cascade when you have cycle references in you relations in database structure:
Only NO ACTION allowed (see you error Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION)
Is State from country has a different meaning then state in employee ?
Correct solution:
and table [Employee] must have one foreign key on multiple columns - FK(CID, SID)

Related

In the FK of the two tables, error to cycles or multiple cascade paths

enter image description here
The three tables have a FK relationship.
Post.PostBy is NOT NULL, Post.Category_id is Nullable.
An error has occurred.
Introducing FOREIGN KEY constraint 'FK_Post_Category' on table 'Post' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
ALTER TABLE [Category] ADD CONSTRAINT [FK_Category_CreatedBy] FOREIGN KEY ([CreatedBy])
REFERENCES [Account]([_id])
ON DELETE CASCADE
ALTER TABLE [Post] ADD CONSTRAINT [FK_Post_PostBy] FOREIGN KEY ([PostBy])
REFERENCES [Account]([_id])
ON DELETE CASCADE
ALTER TABLE [Post] ADD CONSTRAINT [FK_Post_Category_id] FOREIGN KEY ([Category_id])
REFERENCES [Category]([_id])
ON DELETE SET NULL
I don't understand.
PostBy is NOT NULL, so when the referenced row is deleted, we want it to be deleted along with it.
Category_id is NULLABLE, so if the reference row is deleted, set a NULL value.
Category and Post refer to Account, but I think it should work because they have different Account values.

How to delete a column which is the primary key in SQL Server

I'm getting an error while trying to delete the primary key column from the table.
id - primary key column
Table OLTMS_0B8DF2
Query
ALTER TABLE OLTMS_0B8DF2
DROP CONSTRAINT id;
GO
Error
Msg 3728, Level 16, State 1, Line 1
'id' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
You need to specify the existing constraint name rather than the column name to drop the constraint.
You can determine the PK constraint name using the SSMS object browser or with the T-SQL script below. A best practice is to explicitly name constraints (e.g. PK_OLTMS_0B8DF2) rather than rely on auto-generated constraint names. That makes subsequent DDL easier.
SELECT name
FROM sys.key_constraints AS pk
WHERE
pk.parent_object_id = OBJECT_ID(N'OLTMS_0B8DF2')
AND type = 'PK';
If you have foreign key constraints referencing the table, you'll need to similarly drop those too.

Error about CASCADE in Sql - Server 2008

I posted this problem yesterday and didn't get answer.
I attach here sample of my problem. I want to create this DB:
CREATE DATABASE [TRYShemen];
GO
USE [TRYShemen]
GO
CREATE TABLE Persons(
ID VARCHAR(50) PRIMARY KEY,
FullName VARCHAR(50) NOT NULL
);
CREATE TABLE Class(
ClassNum VARCHAR(30) PRIMARY KEY,
Teacher VARCHAR(50) NOT NULL,
constraint Class_FK foreign key (Teacher) references Persons (ID) ON DELETE NO ACTION ON UPDATE CASCADE,
);
CREATE TABLE Students(
StudentID VARCHAR(50) ,
ClassNum VARCHAR(30)
constraint Students_PK PRIMARY KEY (StudentID, ClassNum),
constraint Students_FK foreign key (StudentID) references Persons(ID) ON DELETE NO ACTION ON UPDATE CASCADE ,
constraint Students_FK1 foreign key (ClassNum) references Class(ClassNum) ON DELETE NO ACTION ON UPDATE CASCADE
);
and I get an error:
Introducing FOREIGN KEY constraint 'Students_FK1' on table 'Students' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I know that if i change the 'Students_FK1' to ON CASCADE NO ACTION, it will work, but it's not my target. I want that if the id of 'Person' will update , it will update in the Students table too.
how can I solve it?
Thank You!!
So what can you do about it?
Consider this structure as an alternative.
table-Persons
table-Students
table-Teachers
table-Classes
table-Classes_to_Teachers
table-Classes_to_Students
Then you can have Cascading references for
Persons-->Students & Students-->Classes_to_Students
Persons-->Teachers & Teachers-->Classes_to_Teachers
When you access your data, you will have more joins to get all the data for one class.
But the constraints should work out.
Note that it says "may cause cycles or multiple cascade paths." If you follow a delete action through your ER diagram, you'll probably find that the delete action cascades to the same table via multiple routes.
In this case, imagine that the same person is both the teacher and the student in a class. Yes, I know, that's a logic error, but if that condition were true, then an update to that person would cascade to the students table via two routes.

Unable to get a Foreign key

USE Kudler_FF
ALTER TABLE Employee_Tbl
ADD CONSTRAINT FK_Employee_Tbl
FOREIGN KEY (JobTitle) REFERENCES Job_Tbl (JobTitle);
Message I am getting:
Msg 547, Level 16, State 0, Line 2
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Employee_Tbl". The
conflict occurred in database "Kudler_FF", table "dbo.Job_Tbl", column
'JobTitle'.
What did I add or not add
You usually only get an error adding a foreign key constraint when the constraint would be violated by the current data.
In other words, you probably have a value in Employee_Tbl(JobTitle) that does not exist in Job_Tbl(JobTitle).
You would not be able to add such a constraint until your data is modified so that a violation would not occur.
Find the values for JobTitle in Employee_Tbl that don't exist in Job_Tbl and then add them to that latter table.
I'm not sure of the exact syntax for SQL Server but you could start with:
select distinct JobTitle from Employee_Tbl
where JobTitle not in (
select distinct JobTitle from Job_Tbl
)

Cascade tree deleting in MS SQL Server Express 2005

One table has the name "Stages", every Stage can have 0 to infinity children. In the table "Stages" there is a column named "Parent". This column is foreign a key for the same table "Stages".
How would I make a cascading delete for this tree? I want to when deleting any row in this table, automatically delete all their children and their children's children...
With the following query
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Stage_Stage]') AND parent_object_id = OBJECT_ID(N'[dbo].[Stage]'))
ALTER TABLE [dbo].[Stage] WITH CHECK ADD CONSTRAINT [FK_Stage_Stage] FOREIGN KEY([parent])
REFERENCES [dbo].[Stage] ([id]) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Stage] CHECK CONSTRAINT [FK_Stage_Stage]
GO
I get this error
Msg 1785, Level 16, State 0, Line 2
Introducing FOREIGN KEY constraint 'FK_Stage_Stage' on table 'Stage' 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 2
Could not create constraint. See previous errors.
Msg 4917, Level 16, State 0, Line 1
Constraint 'FK_Stage_Stage' does not exist.
Msg 4916, Level 16, State 0, Line 1
Could not enable or disable the constraint. See previous errors.
Add foreigns key with ON DELETE CASCADE option for all "child" tables.
ALTER TABLE SomeChildTable
CONSTRAINT YOurConstraintName
FOREIGN KEY (YourParentId)
REFERENCES YourParentTable(ParentTableId) ON DELETE CASCADE;
for new tables:
CREATE TABLE ttt
(
...
CONSTRAINT YOurConstraintName
FOREIGN KEY (YourParentId)
REFERENCES YourParentTable(ParentTableId) ON DELETE CASCADE
)

Resources