how to truncate table with foreign key constraint
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table1;
how to truncate foreign key constraint table
DELETE FROM [TableName]
DBCC CHECKIDENT ([TableName], RESEED, 0)
truncate table sql server foreign key
CREATE TABLE #x -- feel free to use a permanent table
(
  drop_script NVARCHAR(MAX),
  create_script NVARCHAR(MAX)
);

DECLARE @drop   NVARCHAR(MAX) = N'',
        @create NVARCHAR(MAX) = N'';

-- drop is easy, just build a simple concatenated list from sys.foreign_keys:
SELECT @drop += N'
ALTER TABLE ' + QUOTENAME(cs.name) + '.' + QUOTENAME(ct.name) 
    + ' DROP CONSTRAINT ' + QUOTENAME(fk.name) + ';'
FROM sys.foreign_keys AS fk
INNER JOIN sys.tables AS ct
  ON fk.parent_object_id = ct.[object_id]
INNER JOIN sys.schemas AS cs 
  ON ct.[schema_id] = cs.[schema_id];

INSERT #x(drop_script) SELECT @drop;

-- create is a little more complex. We need to generate the list of 
-- columns on both sides of the constraint, even though in most cases
-- there is only one column.
SELECT @create += N'
ALTER TABLE ' 
   + QUOTENAME(cs.name) + '.' + QUOTENAME(ct.name) 
   + ' ADD CONSTRAINT ' + QUOTENAME(fk.name) 
   + ' FOREIGN KEY (' + STUFF((SELECT ',' + QUOTENAME(c.name)
   -- get all the columns in the constraint table
    FROM sys.columns AS c 
    INNER JOIN sys.foreign_key_columns AS fkc 
    ON fkc.parent_column_id = c.column_id
    AND fkc.parent_object_id = c.[object_id]
    WHERE fkc.constraint_object_id = fk.[object_id]
    ORDER BY fkc.constraint_column_id 
    FOR XML PATH(N''), TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'')
  + ') REFERENCES ' + QUOTENAME(rs.name) + '.' + QUOTENAME(rt.name)
  + '(' + STUFF((SELECT ',' + QUOTENAME(c.name)
   -- get all the referenced columns
    FROM sys.columns AS c 
    INNER JOIN sys.foreign_key_columns AS fkc 
    ON fkc.referenced_column_id = c.column_id
    AND fkc.referenced_object_id = c.[object_id]
    WHERE fkc.constraint_object_id = fk.[object_id]
    ORDER BY fkc.constraint_column_id 
    FOR XML PATH(N''), TYPE).value(N'.[1]', N'nvarchar(max)'), 1, 1, N'') + ');'
FROM sys.foreign_keys AS fk
INNER JOIN sys.tables AS rt -- referenced table
  ON fk.referenced_object_id = rt.[object_id]
INNER JOIN sys.schemas AS rs 
  ON rt.[schema_id] = rs.[schema_id]
INNER JOIN sys.tables AS ct -- constraint table
  ON fk.parent_object_id = ct.[object_id]
INNER JOIN sys.schemas AS cs 
  ON ct.[schema_id] = cs.[schema_id]
WHERE rt.is_ms_shipped = 0 AND ct.is_ms_shipped = 0;

UPDATE #x SET create_script = @create;

PRINT @drop;
PRINT @create;

/*
EXEC sp_executesql @drop
-- clear out data etc. here
EXEC sp_executesql @create;
*/

SQL相关代码片段

postgresql insert datetime example

relational dbms

brew mysql

is operator sql

What is Partitioned_By in SQL?

for loop pl/sql

oracle change column type

convert sql table to c# class

mysql date time month

SQL create table

show store procedure create process

MISSIng index sql server

postgres update column

pagination in mssql

how to search in myanmar unicode in ssms database

how to get first and last day of month in sql

oracle list foreign keys referencing a table

what is unsigned in mysql

Impala Wildcard Search Query

sql server force parallelism

oracle list of materialized views

ajouter un mot de passer mysql

show view code mssql

Filter window function

oracle get plan_hash_value from "explain plan"

Incorrect syntax near 'RSA_512'.

analyze table oracle

mysql created_at and updated_at add value

supabase "permission denied for schema public"

get age using sql query

phpmyadmin get list of all databases with sizes

how to get scend number row from mysql database

create a sequence in oracle sql

does not contain KQL (Kusto)

how to alter data enum status in postgres

how to alter data enum status

order by sql multiple columns with case

idle connections

create table using existing table bigquery

perform sql inset comand in c#

oracle apex avoid confirmation

mysql ERROR: ASCII '\0' appeared in the statement

oracle date to timestamp with timezone

check what is using sql cpu on sql server

wp secure sql query

list out custom functions cmd in mysql

sql having count is null

find particular column or table name from whole db

EPOCH postgres

MSSQL Work table

float to varchar in sql

sqlplus generate awr

length vs char_length mysql

get last inserted primary key mysqli

alter multiple columns in table sql server

psql check

editeur plSQL en ligne

sql get last 2 month

levenshtein sql mysql 8

CONVERT Date Formats

postgresql root password change

postgresql root password reset

is null and is not null in sql

mysql cli create database with engine and charset

dateformat mysql

creating a tablespace

mysql connection refused docker

reset table mysql

oracle plsql how to print array type

hive insert from select

sql delete all the database tables

sql create command

sql server select if else example

how to make the dump out of database in postgresql

how to save sql_mode in mysql permanently

created_at updated_at mysql

sql convert date to yyyymmdd

not equal KQL

setup mysql database ubuntu

mysqldump overwrite existing data