473,424 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,424 software developers and data experts.

Assigning Foreign Key To New SQL Server Table

. .
Hi

I am creating new SQL Server Tables using SQL Server 2005. I have set
primary key to the tables .But I do not know how to assign Foreign key
to the tables .I need to do some joins later and that is why I have to
put Foreign key to the table . The Primary key is visible and can be
assigned easily .But How do I assign foreign key .

Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Aug 31 '05 #1
3 39962
.. . (km*****@sark.com) writes:
I am creating new SQL Server Tables using SQL Server 2005. I have set
primary key to the tables .But I do not know how to assign Foreign key
to the tables .I need to do some joins later and that is why I have to
put Foreign key to the table . The Primary key is visible and can be
assigned easily .But How do I assign foreign key .


First of all, questions about SQL 2005 are best asked in the SQL 2005
newsgroups, as these are monitored by the SQL Server deverlopers. Access
info here: http://go.microsoft.com/fwlink/?linkid=31765

As for your question, the syntax is as in this example:

ALTER TABLE tbl ADD CONSTRAINT fk_myforeignley (col1, col2)
REFERENCES othertbl (col1, col2)

Or were you using the table designer? I recommend that you learn the
syntax to create table from SQL statements. In the long run that will
make you more effective and productive, than clicking around in the
table designer. Also, there are several *serious* bugs in the table
designer when it comes to modify existing tables, so the less you use
it, the better.

(If you are dead set on it, I believe that if you right-click there
are foreign keys in the context menu.)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 31 '05 #2
. .


Hi

Thanks for your reply . It worked and I was able to assign Foreign key
to the tables . Thanks again .
*** Sent via Developersdex http://www.developersdex.com ***
Sep 1 '05 #3
Foreign keys will definitely help with relational integrity. However,
for the purposes of joining the foreign key need not be pre-defined.
That is what the join syntax in the select statement is for.

Sep 1 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: geoff | last post by:
The table creation script(at the end of this post) works fine on 4.0.1-alpha-win, but the foreign key constraints fail on 4.0.15-win. I am starting the server with the same command for both...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Gunnar Vøyenli | last post by:
Hi! For the sake of simplicity, I have three tables, Employee, Department and Work Employee >---- Department \ / \ / ^ ^ Work
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
11
by: FreeToGolfAndSki | last post by:
Hi, I have an Orders Table that has Employee1 and Employee2 (one is the sales rep the other is the telemarketing rep) Both of these fields need to cascade update against the Employees table. I...
1
by: HandersonVA | last post by:
There is itemlookup table, which stores item number and item description. Also there is a child table, pricehistory, of the itemlookup table, which stores different prices and different date...
1
by: apax999 | last post by:
Kinda new to SQL, using SQL Server 2005. I have some foreign keys in a couple of tables. I need to drop these tables, but can't since I'll get the error: Msg 3726, Level 16, State 1, Line...
6
by: bobdurie | last post by:
If i create a simple table with a foreign key constraint, does it create an implicit index on that given ID? I've been told this is done in some databases, but i need to know for sure if SQL...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.