Create two tables tbltesta and tbltesb, and delete tbltesta
create table dbo.TblTestA(
Id bigint primary key identity(1, 1),
name varchar(20) unique,
)
create table dbo.TblTestB(
Id bigint primary key identity(1, 1),
TestAId bigint not null foreign key references
Status varchar(1),
)
Error reported: it is referenced by a foreign key constraint
Reason: other tables refer to the foreign key of this table
Solution:
1. Find the foreign key of this table referenced by other tables
exec sp_Helpconstraint ‘table name’;
2. Find the associated foreign key constraint table name
select name
from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id
where f.parent_object_id=object_ID (‘foreign key association table name ‘)
3. Delete the foreign key constraint
Alter table foreign key constraint table, Drop constraint name (constraint name found above)
4. Delete table (events can be added)
BEGIN TRY
BEGIN TRANSACTION
ALTER TABLE TblTestB DROP CONSTRAINT FK__TblTestB__TestAI__635CD8E4
DROP TABLE dbo.TblTestA
COMMIT TRANSACTION
PRINT 'commit sucesss'
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'rollback sucesss'
END CATCH
Read More:
- Error creating foreign key in MySQL: 1215 cannot add the foreign key constraint
- C# System.Data.SQLite.SQLiteException:“SQL logic error or missing database no such table: XX”
- How to Fix MySQL error 1005: can’t create table (errno: 150)
- MySQL Build table error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
- MYSQL Index Key Length 1071 – Specified key was too long; max key length is 3072 bytes
- SQL server converts multiple lines into one line, separated by characters
- Lock request time out period exceeded. (Microsoft SQL Server, Error: 1222)
- Navicat connecting to SQL Server Error [How to Solve]
- [Solved] EOS7.6 Error: Init DB failed [Specified key was too long; max key length is 767 bytes…
- IDEA Database Tool connects to SQL Server 2008 Error [How to Solve]
- SQL Server Group sort de duplication row_ number() over ( PARTITION BY t1.col_ 2 ORDER BY 1 )
- Introduction to Relational Databases in SQL
- [Linux Docker Mirror] MYSQL Run sql Script Error: Failed to open file ‘/home/mydatabase.sql‘, error: 2
- Bulk Update Error: #Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the m
- java.sql.SQLException: Disk full (/tmp/#sql_1eaa2_60.MAI); waiting for someone to free some space
- [Solved] java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corres
- MySQL 8.0 error 1114 (HY000): the table’sbtest1’is full (Fixed)
- [Solved] SQL Error: Method queryTotal execution error of sql
- ERROR 1075 (42000) Incorrect table definition; there can be only one auto column and it must be defi
- [305]MYSQL 1062 error: duplicate entry ‘…’ for key ‘primary