久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

通過 alter table 命令刪除 T-SQL 中的約束 - 問題

Removing a constraint in T-SQL by alter table command - problem(通過 alter table 命令刪除 T-SQL 中的約束 - 問題)
本文介紹了通過 alter table 命令刪除 T-SQL 中的約束 - 問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我在 t-SQL 中有以下代碼:

I have got the following code in t-SQL:

alter table Persons
drop primary key;

還有消息:消息 156,級別 15,狀態(tài) 1,第 10 行關(guān)鍵字primary"附近的語法不正確.

我檢查了不同的語法組合,但都沒有奏效.這里有什么問題?

I have checked different combinations of syntax and none have worked. What is wrong here?

表格就是這樣創(chuàng)建的 - 這只是學(xué)習(xí)的開始,非常簡單,只有兩個約束.

This is how a table has been created - it is just beggining of studying, so very simple one with only two constraints.

create table Persons(
PersonID int not null primary key,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

推薦答案

不是DROP PRIMARY KEY,而是DROP CONSTRAINT {Object Name}.例如:

CREATE TABLE dbo.YourTable (ID int NOT NULL);
GO
ALTER TABLE dbo.YourTable ADD CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED (ID);
GO
ALTER TABLE dbo.YourTable DROP CONSTRAINT PK_YourTable;
GO
DROP TABLE dbo.YourTable;

這就是為什么顯式命名您的對象如此重要的原因,如上所示,因為您現(xiàn)在不知道 CONSTRAINT 的名稱是什么.但是,您可以通過以下方式獲取名稱:

This is why it's so important to explicitly name your objects, as shown above, as you now don't know what the name of the CONSTRAINT is. You could, however, get the name with the following:

SELECT kc.[name]
FROM sys.key_constraints kc
     JOIN sys.tables t ON kc.parent_object_id = t.object_id
     JOIN sys.schemas s ON t.schema_id = t.schema_id
WHERE s.[name] = N'dbo'
  AND t.[name] = N'YourTable'
  AND kc.[type] = 'PK';

如果你真的不想找出名字然后寫語句,你可以使用動態(tài)語句:

If you really didn't want to find out the name and then write the statement, you could use a dynamic statement:

DECLARE @SQL nvarchar(MAX);
SET @SQL = (SELECT N'ALTER TABLE ' + QUOTENAME(s.[name]) + N'.' + QUOTENAME(t.[name]) + N' DROP CONSTRAINT ' + QUOTENAME(kc.[name]) + N';'
            FROM sys.key_constraints kc
                 JOIN sys.tables t ON kc.parent_object_id = t.object_id
                 JOIN sys.schemas s ON t.schema_id = t.schema_id
            WHERE s.[name] = N'dbo'
              AND t.[name] = N'YourTable'
              AND kc.[type] = 'PK');
EXEC sys.sp_executesql @SQL;

這篇關(guān)于通過 alter table 命令刪除 T-SQL 中的約束 - 問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復(fù)項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 亚洲精品一区中文字幕 | 日本成人在线观看网站 | 日韩成人免费视频 | 久久精品色欧美aⅴ一区二区 | 欧美一区二区 | 99伊人网| 国产精品99久久久久久久久久久久 | 日韩中文字幕 | 亚洲日本视频 | 美女日皮网站 | 久久a久久| 亚洲国产精品人人爽夜夜爽 | 日韩精品在线播放 | 精品美女视频在线观看免费软件 | 免费精品一区 | 拍真实国产伦偷精品 | 精品一区二区三区在线观看 | 国产在线精品一区二区 | 日韩中文字幕一区二区 | 欧美成人精品一区二区男人看 | 国产高清视频在线观看 | 一区二区日本 | 亚洲一区二区av | 99久久精品免费 | 日日日日日日bbbbb视频 | 日韩精品福利 | 美女久久久 | 亚洲人成人一区二区在线观看 | 国产成人精品一区二区三区四区 | 亚洲天堂999| 国产精品国产成人国产三级 | 一级片网址 | 国产成人免费网站 | 欧美精品网站 | 国产日韩欧美在线观看 | 拍拍无遮挡人做人爱视频免费观看 | 69精品久久久久久 | 成在线人视频免费视频 | 欧美日韩1区 | 精品视频免费 | 久草青青草 |