Defining Multiple Foreign Keys in Visual Studio - database

I have a basic database schema as follows (dumbed down so it's easy to read):
[Staff]
StaffId (pk)
FirstName
LastName
[RosterEvent]
EventId (pk)
StartDate
EndDate
[StaffEvents]
StaffId (pk)
EventId (pk)
Ok so, many Staff can have many RosterEvents, which is why I added the StaffEvents table. It resolves the many-to-many relationship. However I do not know how to define the foreign keys.
In Visual Studio, how do I define these as foreign keys using the Table Designer? If I try to add the relationship using the Foreign Key Relationships dialog box, I get the error message "The columns in table 'StaffEvents' do not match an existing primary key or UNIQUE constraint". Even though the UNIQUE constraint has been applied to all primary keys on every table.
Help is much appreciated! Thanks!

In the table designer you can click the row headers on the side of the two columns you want and click the primary key icon on the toolbar.
Are your PK actually marked as Primary Keys or just UNIQUE? Are they the same data-type?

All primary keys on every table are marked as PK and UNIQUE. They are also all of type int.
This is how I am trying to set up the relationship at the moment.
From the Staff table I click the 'Relationships' toolbar button.
I click 'Add'
I click on the (...) from the 'Tables and Columns Specifications' property.
I select 'StaffEvents' as the primary key table.
I select 'StaffId' from the drop down box.
When I click OK, I get the error message "The columns in table 'StaffEvents' do not match an existing primary key or UNIQUE constraint".

Related

Binding multiple tables to Winform

I have a Winform that will display columns from 2 tables.
The form will have a bindingnavigator.
The primary table on the form is a "child" of the second table.
I need the opposite of the usual Customer/Orders linkage
where I want to scroll thru the "orders" and automatically
pull the "customer" info.
There is one common column (ID) between the 2 tables. The ID
column in the "orders" table is not unique but the ID column
in the "customer" table is.
The primary key in the "orders" table is a composite of the ID
column and an order# column which is unique.
I was also unable to create a foreign key relationship when
choosing the ID columns in both tables.
I get this error:
"The columns in table 'orders' do not match an existing
primary key or UNIQUE constraint"
What am I missing?
If you have a composite key (I mean your question is indicating it), then you have to add both columns (ID and order) in your foreign key reference.
When table have composite key, you can see a key symbol next to columns that are part of the primary key.
When you have composite key the order of creating FK is important.
Go to keys section of table and select script of primary key as create script to clipboard (right click on primary key and choose). Paste it in new command window and then create FK with order shown in that script of primary key. (So ID and order columns need to be defined in FK in right order)

SQLExpress create relationship between two tables

I have 2 tables.
dbo.Department
dbo.Employee Records
In employee records, I have a field 2 important fields.
Employee ID (Set as my primary Key)
Department ID
In Department, I only have 3 fields
Department ID (set as my primary key)
Name
Location
I want Department ID to be populated with data from dbo.Department so the only options are a pull down list. (EXAMPLE department 100 - Marketing - Floor 1, department 200 - Human Resources - Floor 2 ETC.) To do this, I attempted to create a new foreign key in dbo.Department
First, I right clicked on the keys folder in dbo.Department and selected 'New Foreign Key'
Then, I clicked on the 3 dots I got when I selected the 'Tables and Columns Specific' field as shown below.
This pops up a dialog box where I can set the Primary Key table and the foreign key table. I set the primary key table as Employee Records and the foreign key table as Department. Under both fields, I have Department ID selected as shown below.
When I hit ok I get the following message
'The columns in table "Employee Records" do not match a primary key or UNIQUE constraint'
Am I setting this up wrong? I'm new to database practices so I'm not sure if I'm even using the right technique to do what I am trying to achieve.
You are close, but have your tables reversed. Create your foreign key on the Employees table, not the Departments table. The primary key table in this case is the Departments table.

SQL Server having a column as both PK and FK

I have not done much database design work and I was searching around for some example.
While I know the difference from a primary and foreign key, one thing that caught be off guard was that even if a Table has a primary key and is used as a foreign key in another table, as I was used the GUI SSMS tools, I noticed that I sometime end up having this
PhoneID (PK, int, not null)
While my User table
UserId(PK,FK, int, not null)
BOTH of these tables have these ID's as primary keys in those tables, along with foreign keys in other tables, but why does one of them have "PK,FK" obviously I accidentally created it, but should it be like that?
It is Possible for a Primary key to be a Foreign Key as well at the same time.
But looking at you database design, In your case I dont think it was intentional it was done by mistake. And if it wasnt done by mistake then you need to fix it.
In your dbo.PhoneType Table the column PhoneTypeID needs to be a Primary key only not a Foreign key. My guess is this was done by mistake, you wanted to create a Foreign key in your dbo.Phone table on column PhoneTypeID referencing PhoneTypeID column in dbo.PhoneType table. But somehow you end up create a foreign key constraint on the Primary key column of the dbo.Phontype table.
This Design contradicts constraints.
In simple english : The foreign Key Constraint on your dbo.PhoneType(PhoneTypeID) enforces that you cannot have a PhoneTypeID in dbo.PhoneType table unless it exists in PhoneTypeID column of dbo.Phone table.
On the other hand Foreign Key Constraint on dbo.Phone(PhoneTypeID) enforces that you cannot have a PhoneTypeID in dbo.Phone unless it exists in dbo.PhoneType(PhoneTypeID).
and same is true for the UserID column in dbo.Users table.
Solution
You need to drop the following Constraints to make it work properly.
1) In dbo.PhoneType table Drop Foreign key constraint referencing
PhoneTypeID column in dbo.phone table.
2) In dbo.Users Table drop the Drop Foreign key constraint referencing
UserID column in dbo.phone table.
It's entirely possible, yes. A primary key of a table could also be a foreign key referencing another table.
In your case, I'm not exactly sure what you did. You can check the constraints to see which column the UserId column is referencing.
As an additional note, simply adding a foreign reference to a table does not implicitly make that column a foreign key on another table. For example, just because you add FK_PhoneTypeID to the Phone table, SQL Server does not automatically make PhoneTypeID column in the PhoneType table a FK. In your statements, somewhere, it's possible that you made assignments to other columns, or even to themselves.

sql server creating foreign key as one to one not one to many (linq)

I have set up my tables like so:
Table A:
Id
[...]
Table B:
Id
AId
[...]
I have a foreign key created as
FK_TableB_TableA where primary key table is Table A and its primary key is Id and the foreign key table is TableB and its foreign key is AId...
However, when I update my dbml for linq TableB is defined as an entityref instead of an entityset...
Adding a foreign key should generate a one-to-many relationship correct?
This is very generic but if I need to add more detail please let me know!
Didn't figure out why this was happening however on the dbml there was no association created between the two tables for whatever reason - therefore I just added the association.

EF5 How to Add missing Relationships

Hi i have a database first EF5 model defined.
My user table has a primary key guid UserGUID
and another key field UserID with an auto-incrementing integer.
I have created a table called UserCustomField which has
UserID and I have created a foreign key constraint to UserID in my User Table.
When I update the model from the database all foreign key relationships to primary keys are generated but none to Key fields. Ignoring the potential point about using the guid through all my tables.....
A/ shouldn't EF add this relationship?
B/ how can i manually add it?
Cheers
Tim
A: No.
B: You cannot.
EF is able to use relations only when they point to primary key so either change User table to use UserID as primary key and remove UserGuid or change UserCustomField table to point UserID to UserGuid in the User table.
The reason why it doesn't work is that your database requires UserID in User table to be marked as unique (that means unique constraint) and EF doesn't support unique constraints yet.

Resources