Thursday, March 29, 2012

foreign key

Hello,

I want to make a poll and have to use a foreign key in my database. This is not possible in Web Matrix so I have to use SQL Enterprise Manager. But don't know how. I read the help but can't figure it out. Can someone help me? Thanks in advance.

Regards,

Roel AlblasHi Roel,

I'm not quite sure what you mean. Do you need to know how to write the SQL statement that uses a foreign key? Or create tables with a foreign key?

Tell us more about what you want to do and we'll try to help.

Don|||I'm making a poll following an example. In that example I have to make tables wich has an relation to each other with a foreing key. I use SQL Server 2000 and Web Matrix.

Roel|||So you're building a table. And you'll use EM to do it. Okay.

Here's a simple example. Since I don't know the particulars of the data you'll use, I'll use a simple contact management example, where each person can have multiple phone numbers.

The Person table would look something like this:

PersonID int (identity, primary key)
Name varchar(30)
...

The Phone table would look something like this:

PhoneID int (identity, primary key)
PhoneNumber varchar(15)
PersonID int
... (type of number, etc.)

In this case, I've named the linking field, PersonID, the same in each table but that is not necessary. You may want to use a naming standard that identifies both primary and foreign keys in your tables. Note in particular that Phone.PersonID isnot an identity field, because it can have duplicate data when a person has several phone numbers.

Using this structure you can now do joins on the two tables to return all of the numbers for a person, or a list of everyone and their phone numbers.

If you want the database to enforce referential integrity (make sure that there are no phone numbers without a person, cascade deletes, etc.) you can also create a relationship between the tables. The easiest way to do this in EM is to create a database diagram with the two tables and create it visually.

Is this enough information? If not, ask away.

Don|||Hi

Just wondering if you know of a similar feature in web matrix to create relationships?
I am using MSDE?

Thanks

Ramila|||No, I sure don't. It's been a while since I did a project with Web Matrix.

There are some other admin tools available, such as these, but I don't know their capabilities for creating relationships:

ASP.NET Enterprise Manager, an open source SQL Server and MSDE management tool.

Microsoft's Web Data Administrator is a free web-based MSDE management program written using C# and ASP.NET, and includes source code.

You can also use T-SQL through the osql command-line utility to create or modify your tables.

How are you creating the structure of your database? Through Matrix? Another way?

Don

No comments:

Post a Comment