.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

Create a sql foreign key constraint

Posted By:      Posted Date: September 14, 2010    Points: 0   Category :Sql Server
Hi, I wish to create a constraint that state as below Code.CodeTable ( CodeID smallint, CategoryID smallint,....)  --> Parent Table Admin.Document( DocumentTypeID smallint,.....)  --> Child Table The FK will be Admin.Document.DocumentTypeID map with Code.CodeTable.CodeID I wish to have the contraint that only check Code.CodeTable.CodeID which the Code.CodeTable.CategoryID =15 only. Thanks.Meng Chew

View Complete Post

More Related Resource Links

Foreign key constraint not replicating

We have a merge replication setup with one publisher/distributor and several subscribers.  Several of the tables being replicated have foreign key constraints and these replicate without issue.  However, some foreign key constraints that utilize cascading do not replicate.  To make matters worse, some tables with these cascading fk constraints replicate fine while others do not.  Has anyone run across this behavior and is there a fix for it?  The cascades will dramatically reduce our merge conflicts and help maintain the integrity of the data so we need to keep them in place.

Create unique constraint on a column which has null values

Hi All, I have a table suppose 'Temp' having one of the column as 'ColA' which has some null values as well as non null values. Now i have a requirement to create a unique constraint on it. We have tried but couldnt do it.Apart from having a trigger on insert and update statements is there any other alternate. Can any one please help me on this. Thanks & Regards, Srikanth  

Create View in an External List that Gets a Field from Another External Content Type via Foreign Key


I currently have two tables in a SQL Server 2k8 database: Locations and Customers

The fields in the Locations table are:

  • LocationName
  • StreetAddress
  • City
  • State
  • ZIP
  • CusotmerID (foreign key joined to Customers table)

The fields in the Customers table are:

  • ID (primary key)
  • CustomerName
  • CurrentBalance

Through SharePoint Designer, I have both tables set up as External Content Types of the same names as the tables and have created External Lists for each External Content Type. All CRUD operations are working perfectly. I added the proper Assocaiation and I've set CustomerName to be the Title of the Customers External Content Type. I can currently use a picker for CustomerID in the Locations List and CustomerName is displayed properly. Also, the default DispForm also correctly displays the CustomerName for any single Location record.

However, I would like to modify the default ReadList so the foreign key (CustomerID) will show the Title field of Customers (CustomerName). However, I am running into trouble finding out how to do this.

Any help with this issue would be greatly appreciated!!


The ALTER TABLE statement conflicted with the FOREIGN KEY constraint


The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "fk_Allergy". The conflict occurred in database "MSCPROJECT", table "dbo.Severity", column 'SeverityCode'.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblLinks_tblWebSite".


How does this happen? the tblLinks doesn't even have a column called websiteID.  websiteID is the primary key of the table tblwebsite. LinkID is the primary key of tblLinks. There is not LinkID column in tblwebsite.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblLinks_tblWebSite". The conflict occurred in database "Linkexchanger", table "dbo.tblWebSite", column 'WebSiteID'. The statement has been terminated.


So how does this happen? How do I terminate the foreign key relationship? I understand I can add this to my Insert stored procedure:

DROP CONSTRAINT FK_tblLinks_tblWebSite

but that stored procedure is used for other functions in my website, and I don't know if doing that is a good idea. Is there a way to relax the constraints for the purpose of this function here?  I'm taking data from a backup links table and trying to write a new row in the links table.

INSERT statement conflicted with COLUMN FOREIGN KEY constraint...


Hi there,

I have a stored procedure which i pass a number of parameters into. One of these parameters is staffNo (only passed this in because i couldn't execute the query without it). The thing is this field can be Null, but when trying to pass null into it it comes up with an Foreign Key conflict. staffNo is a foreign key within the table i'm inserting the data into.

This is the error i get:

"INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'PropStaffFK'. The conflict occurred in database 'DewMountain', table 'TblStaff', column 'staffNo'. The statement has been terminated. The 'PropertyAdvert' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead. "


Does anyone know of away around this? how to pass a null value to the stored procedure without it causing this error.

Thank you


insert statement violated because Foreign key constraint


I  create an SSIS package, i use data flow to import rows from different server to local server. Unfortunately, there always be error message something about insert statement violated because of foreign key "The INSERT statement conflicted with the FOREIGN KEY constraint" since the table is transactional table that has some foreign key. I want to minimize using temporary table and trying to use toolbox item in ssis.I would appreciate any since i am ssis newbie.


How to create 3 tier application using LINQ

As you know that in 3 tier architecture there are three layers

User interface layer. (Is our Form in Windows application and .aspx page in Web application)
Data Access layer. (Which provides interface between Business logic layer and Database)
Business Logic layer.(Which stores your application logic)

Create ToolTip for GridView Header

The following allows you to add a tooltip to the header links of your GridView, a feature that is lacking.

Create And Run Excel Macro At Runtime in C#

You've been tasked with writing a Windows Form application using the .NET Framework that interacts with Microsoft Excel. If you are new to working with Excel in .NET and are having trouble learning how to format certain cells or cell ranges, then this little tip will come in handy. Rather than scour the internet hoping to find the correct set of constants or method syntax for formatting your spreadsheet, use the built in tools in Excel to write the familiar VBA code for you. Then, you can paste the VBA code generated by Excel into the sample C# code below and run it dynamically at runtime.

Create and Use a Typed DataSet by Using Visual C# .NET

This article shows you how to create and use a typed DataSet in a small Web application. Typed DataSets, which inherit from the DataSet class, create first-class members of a DataSet's DataTables and DataColumns, yielding the many benefits that come with the use of strong typing.

Connestion string create any database server

This links important to how to create connection string to any data base sever.

Dynamically Create Controls in ASP.NET by Using Visual C# .NET

This article demonstrates how to dynamically create a control for an .aspx page. The sample project does the following:
Creates two TextBox controls.
Verifies that the contents (TextBox.text) and the attributes of the TextBox are saved across posts to the server.
Illustrates handling events that are posted by a dynamically-created control

Dynamically Create Controls in ASP.NET with Visual Basic .NET

This step-by-step article describes how to dynamically create controls for an ASPX Web page.

The sample project does the following:
It creates two TextBox controls.
It verifies that the TextBox contents (TextBox.text) and attributes are saved across posts to the server.
It describes how events that are posted by a dynamically created control are handled.

How to create a windows mobile (Smart Device) .Cab installer

A Cab file is the default setup format for Windows CE and Windows Mobile devices (similar to windows .msi files). You probably already have installed several application using .cab files, and are familiar with the concept. One point which is often unknown is that .cab files are processed by wceloader.exe, and it can only install one .cab file at a time. That means we cannot have nested .cab files. It doesn't mean we cannot have .cab files contained by another .cab, but the contained .cab files will not be installed during the installation of the container .cab. We should install it manually after the container .cab file installation has been completed.

There are two ways to create a cabinet (.Cab) file. The traditional one, and the friendly VS Smart Device Cab project which doesn't require additional coding, but which also relies on the traditional one at low-level.

Create Web Site vs Web Application


I follow the tutorial and creted a web site for my company sales system. One of my collegue mention we use the wrong templet, we should use Web application for company application.

Should i continue using web site or change to web application? We are creating application for company sales, inventory, hr ,etc where possible more than 1000++ user. And during month end, it will be high traffic and transaction. can Web Site Template with BLL and DAL handle it? or i just need a powerful server?

Create an API (aspx vs mvc)


Hi everybody!

I'm not sure if this is the right place to ask this, so, sorry if it's not. I have to create a web application. I can handle that. But, it has a requirement I've never done before. I need to create an API for that application, so other apps can get or set information from/to my webapp.

I've been searching a lot and still don't have a starting point. Seems that MVC is really good to do that, but I'm not familiar with MVC, so I would need a starting point. I was thinking to do it in aspx (Framework 4), but do not know how to do that with aspx either.

Can anyone help me to get started with this?

Thanks in advance

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend