拷貝表的sql命令:sql複製表結構和數據

在SQL Server中使用 select into 可以創建一張新表的同時將原有表數據追加到新表中,現在創建一張測試表,裡面存放各城市大學名稱:

 create table [dbo].[school](
	[id] [bigint] identity(1,1) not null,
	[name] [varchar](50) not null,
	[cityid] [bigint] not null,
 constraint [school_primary] primary key clustered 
(
	[id] asc
) )

為測試表創建以cityid為索引列的非聚集索引:

create nonclustered index [index_school_cityid] on [dbo].[school] ([cityid] asc)

追加數據後,查看該表的數據:

select * from school
SQL Server 中Select into複製數據到新表

現在使用 select into 複製一張新表school_test:

select * into school_test from school

查看新表school_test的數據,和原有表schoo相同:

select * from school_test

再來看看新表的結構,發現id的自增屬性被複制了:

SQL Server 中Select into複製數據到新表

而其他的屬性,如原表的主鍵和索引卻沒有被複制到新表:

SQL Server 中Select into複製數據到新表

說明使用select into 可以複製原表的數據、字段和自增屬性,而主鍵和索引等卻無法被複制。

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/226504.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:50
下一篇 2024-12-09 14:50

相關推薦

發表回復

登錄後才能評論