Foreign Key and Check Constraints are two types of constraints that can be disabled or enabled when required. This type of operation is needed when bulk loading operations are required or when there is no need to validate the constraint. The T-SQL Script that does the same is very simple.
USE AdventureWorks
GO
– Disable the constraint
ALTER TABLE HumanResources.Employee
NOCHECK CONSTRAINT CK_Employee_BirthDate
GO
– Enable the constraint
ALTER TABLE HumanResources.Employee
WITH CHECK CHECK CONSTRAINT CK_Employee_BirthDate
GO
It [...]
Archive for the ‘SQL Constraint and Keys’ Category
SQL SERVER – Disable CHECK Constraint – Enable CHECK Constraint
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on November 12, 2009 | 4 Comments »
SQL SERVER – Difference Between Candidate Keys and Primary Key
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on October 22, 2009 | 14 Comments »
Introduction
Not long ago, I had an interesting and extended debate with one of my friends regarding which column should be primary key in a table. The debate instigated an in-depth discussion about candidate keys and primary keys. My present article revolves around the two types of keys.
Let us first try to grasp the definition of [...]
SQL SERVER – Discussion – Effect of Missing Identity on System – Real World Scenario
Posted in DBA, Database, Pinal Dave, Readers Contribution, Readers Question, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Puzzle, SQL Query, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology on August 11, 2009 | 6 Comments »
About a week ago, SQL Server Expert, Imran Mohammed, provided a script, which will list all the missing identity values of a table in a database. In this post, I asked my readers if any could write a similar or better script. The results were interesting. While no one provided a new script, my question [...]
SQL SERVER – Puzzle – Write Script to Generate Primary Key and Foreign Key
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology, tagged SQL Challenge on July 23, 2009 | 30 Comments »
In one of my recent projects, a large database migration project, I confronted a peculiar situation. SQL Server tables were already moved from Database_Old to Database_New. However, all the Primary Key and Foreign Keys were yet to be moved from the old server to the new server.
Please note that this puzzle is to be solved [...]
SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQLServer, T SQL, Technology on July 17, 2009 | 7 Comments »
There are two different methods of retrieving the list of Primary Keys and Foreign Keys from database.
Method 1: INFORMATION_SCHEMA
SELECT
DISTINCT
Constraint_Name AS [Constraint],
Table_Schema AS [Schema],
Table_Name AS [TableName]
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
GO
Method 2: sys.objects
SELECT OBJECT_NAME(OBJECT_ID) AS NameofConstraint,
SCHEMA_NAME(schema_id) AS SchemaName,
OBJECT_NAME(parent_object_id) AS TableName,
type_desc AS ConstraintType
FROM sys.objects
WHERE type_desc IN (‘FOREIGN_KEY_CONSTRAINT’,'PRIMARY_KEY_CONSTRAINT’)
GO
I am often asked about my preferred method of retrieving list of Primary [...]
SQL SERVER – Clustered Index on Separate Drive From Table Location
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology on June 18, 2009 | 7 Comments »
How to improve performance of SQL Server Queries is a common topic of discussion among many of us. Much has been said, much has been discussed. Few days back, I had an interesting discussion with one of the Junior developers regarding performance improvement of SQL Server Queries. We discussed on how by using a separate [...]
SQL SERVER – Difference Between Candidate Keys and Primary Key
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on May 30, 2009 | Leave a Comment »
Let us first try to grasp the definition of the two keys.
Candidate Key – A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.
Primary Key – [...]
SQL SERVER – How to Drop Primary Key Contraint
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on May 12, 2009 | 13 Comments »
One area that always, unfailingly pulls my interest is SQL Server Errors and their solution. I enjoy the challenging task of passing through the maze of error to find a way out with a perfect solution. However, when I received the following error from one of my regular readers, I was a little stumped at [...]
SQL SERVER – 2008 – Find Relationship of Foreign Key and Primary Key using T-SQL – Find Tables With Foreign Key Constraint in Database
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on February 26, 2009 | 2 Comments »
While searching for how to find Primary Key and Foreign Key relationship using T-SQL, I came across my own blog article written earlier SQL SERVER – 2005 – Find Tables With Foreign Key Constraint in Database. It is really handy script and not found written on line anywhere. This is one really unique script and [...]
SQL SERVER – Observation – Effect of Clustered Index over Nonclustered Index
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology on February 21, 2009 | 9 Comments »
Note: This article is re-write of my previous article SQL SERVER – Observation – Effect of Clustered Index over Nonclustered Index. I have received so many request that re-write it as it is little confusing. I am going to re-write this with simpler words.
Query optimization is one art which is difficult to master. Just like [...]
About Pinal Dave
Pinalkumar Dave is a Microsoft SQL Server MVP and a Mentor for Solid Quality India. He has written over 1100 articles on the subject on his blog at http://blog.sqlauthority.com. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager, who specializes in SQL Server Programming and has 7 years of hands-on experience. He holds a Masters of Science degree and a number of certifications, including MCDBA and MCAD (.NET). He was awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 10,853,057 Readers
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Top 7 Commenters
357 - Imran Mohammed
135- Brian Tkatch
55 - Tejas Shah
54 - Jacob Sebastian
47 - Jerry Hung
46 - Kuldip Bhatt
42 - Ashish GilhotraCategories
- About Me (61)
- Best Practices (91)
- Business Intelligence (14)
- Data Warehousing (30)
- Database (268)
- DBA (124)
- MVP (97)
- Poll (5)
- Readers Contribution (37)
- Readers Question (44)
- Software Development (67)
- SQL Add-On (91)
- SQL Backup and Restore (54)
- SQL BOL (10)
- SQL Coding Standards (20)
- SQL Constraint and Keys (51)
- SQL Cursor (28)
- SQL Data Storage (45)
- SQL DateTime (37)
- SQL Documentation (218)
- SQL Download (222)
- SQL Error Messages (125)
- SQL Function (124)
- SQL Humor (25)
- SQL Index (96)
- SQL Interview Questions and Answers (58)
- SQL Joins (63)
- SQL Optimization (65)
- SQL Performance (221)
- SQL Puzzle (25)
- SQL Security (119)
- SQL Server DBCC (42)
- SQL Server Management Studio (28)
- SQL Stored Procedure (101)
- SQL String (19)
- SQL System Table (50)
- SQL Trigger (24)
- SQL User Group (48)
- SQL Utility (126)
- SQL White Papers (36)
- SQLAuthority (371)
- SQL Training (1)
- SQLAuthority Author Visit (80)
- SQLAuthority Book Review (18)
- SQLAuthority News (341)
- SQLAuthority Website Review (28)
- SQLServer (74)
- Tech (910)
- Pinal Dave (900)
- SQL Scripts (531)
- Technology (1152)
- SQL (1152)
- SQL Authority (1152)
- SQL Query (1152)
- SQL Server (1152)
- SQL Tips and Tricks (1152)
- T SQL (1152)
-
Top Posts
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL SERVER - Shrinking Truncate Log File - Log Full
- SQL SERVER - 2005 NorthWind Database or AdventureWorks Database - Samples Databases
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - FIX : ERROR : (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: )
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
-
Authors
-
pinaldave
- SQLAuthority News – Disk Partition Alignment Best Practices for SQL Server
- SQL SERVER – Policy Based Management – Create, Evaluate and Fix Policies
- SQL SERVER – Disable CHECK Constraint – Enable CHECK Constraint
- SQL SERVER – Sharepoint Resource Available for SQL Server
- SQL Authority News – Training MS SQL Server 2005/2008 Query Optimization And Performance Tuning
- SQL SERVER – Removing Key Lookup – Seek Predicate – Predicate – An Interesting Observation Related to Datatypes
- SQL SERVER – Stored Procedure are Compiled on First Run – SP taking Longer to Run First Time
- SQLAuthority News – Data Compression Strategy Capacity Planning and Best Practices
- SQLAuthority News – SQL PASS Summit, Seattle 2009 – Day 4
- SQLAuthority News – SQL PASS Summit, Seattle 2009 – Day 3
-
Archives
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
About Me Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQLServer SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL


