Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Modify, Delete Primary Key in SQL Server

Here you will learn how to modify or delete an existing primary key in a table.

Delete Primary Key using T-SQL

Use the ALTER TABLE DROP CONSTRAINT command to delete a primary key from a table.

The following T-SQL script deletes a primary key PK_Employee_EmployeeID from the Employee table.

Example: Delete a Primary Key
ALTER TABLE Employee 
DROP CONSTRAINT PK_Employee_EmployeeID;

Delete a Primary Key using SSMS

In the Object Explorer, expand the table that contains the primary key to be deleted.

Expand Keys node and Right-click on the key, and select Delete.

Delete Primary Key in SQL Server

In the Delete Object dialog box, verify the correct key is specified and select OK.

Delete Primary Key in SQL Server

Delete Primary Key using Table Designer

In Object Explorer, right-click the table whose primary key has to be deleted. Select Design.

In the table designer, right-click on the row with the primary key and choose Remove Primary Key.

Remove Primary Key in SQL Server

Save the table to reflect the changes.

Modify Primary Key Using SSMS

You can modify the primary key of a table by changing the key name, columns order, clustered option, or fill factor.

To modify a primary key using T-SQL, you must first delete the existing primary key constraint and then re-create it with the new definition.

To modify or delete a primary key using SSMS right-click on the table for which you want to modify the primary key and click on Design option to open a table in the design mode.

Now, right-click in the table designer and choose Indexes/Keys from the menu, as shown below.

Open Indexes/Keys Configurations

In the Indexes/Keys dialog box, select the primary key index, as shown below.

Modify Primary Key in SQL Server

In the Indexes/Keys dialog box, you can rename a primary key, set the clustered option, set the fill factor, change the primary key column or change the column order in case of the composite primary key.

Click on the Add button to add a new index or primary key.

Click on the Delete button to delete the selected index or primary key.

Save the changes by pressing Ctrl + s. If it prevents saving and display a message to re-create a table, then go to Tools menu, click Options, expand Designers, and then click Table and Database Designers. Clear the Prevent saving changes that require the table to be re-created check box, as shown below.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.