Binding multiple tables to Winform - winforms

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)

Related

How to make a foreign key constraint to a primary key in another table?

I have three tables in my database named "applicant_info", "application_info", "education". I want to make a foreign key relationship from columns in "application_info" named "PassPortNo" and "Nationality" to the same columns in "applicant_info" table that are primary keys. Those columns are indexed in the child table i.e application_info. And all the tables are of type "InnoDB".When I try to construct the relationship in relation view tab "PassPortNo" and "Nationality" from Applicant_Info table are not coming in the drop box. How can I do that?
Indexes on the applicant_info table
Indexes on the application_info table
This is the relation view tab that I constructed the relationship with. On the dropdown that says "Table" I couldn't find table named "Applicant_info". Does this mean I cannot construct such a relationship with columns that are primary keys?

check table and foreign key table check

The system correctly set the foreign key (zemploy01-department) with the check table (zdepartmentt02-department) - inside the red box in the diagram. If i do a system check, the system says: zemploy01-department is consistant.
It is apparent that the system takes all primary keys from the check table, and tries to match it to the keys of the foreign key table. The primary key columns of check table(zdepartmentt02) are: MANDT, CAREER, DEPARTMENT, LANGUAGE. These are matched to MANDT, CAREER, DEPARTMENT columns of the foreign key table(zemploy01). However, only one column should be matched i.e. zdepartmentt02-department and zemploy01-department. Why is the system trying to match all the primary keys of the check table.
The values filled in on this wizard are the fields you have set as primary keys in the check table

SQL Server - Foreign Key Referencing Primary Key A OR B

Say I have a table called Matchup that contains two sports teams. I also have a table called Pick that has a column that must match either Team_A OR Team_B. So it's a foreign key of one OR the other columns in Matchup. Is this possible?
Matchup
Team_A
Team_B
Pick
Pick_Team - FK Matchup (Must match Team_A or Team_B from Matchup).
I would split your Matchup table into two: Matchup proper and MatchupDetails.
The Matchup table would have a MatchupID column as its primary key.
The MatchupDetails one would consist of at least two columns: MatchupID to reference the Matchup table and TeamID to reference the Team table (you do have one, don't you?). The two columns would form the table's composite primary key.
Finally, there would be this Pick table. Since you've got multiple users (as per one of your comments), there would need to be a UserID reference. Two more columns, MatchupID & TeamID would serve as a composite foreign key referencing the corresponding column set in MatchupDetails. And to ensure that one user can pick no more than one team from a match-up, a composite primary key of (UserID, MatchupID) should do.
To summarise, here's a complete outline of the relevant part of the schema:
Matchup:
MatchupID
PRIMARY KEY (MatchupID)
MatchupDetails:
MatchupID
TeamID
FOREIGN KEY (MatchupID)
FOREIGN KEY (TeamID)
PRIMARY KEY (MatchupID, TeamID)
Pick:
UserID
MatchupID
TeamID
FOREIGN KEY (UserID)
FOREIGN KEY (MatchupID, TeamID)
PRIMARY KEY (UserID, MatchupID)
I dont think that this is the correct approach.
I would rather recomend that you add an additional field to table Matchup (lets say Pick) and add a CHECK CONSTRAINT to ensure that it is either Team_A or Team_B.
CHECK constraints enforce domain integrity by limiting the values that
are accepted by one or more columns. You can create a CHECK constraint
with any logical (Boolean) expression that returns TRUE or FALSE based
on the logical operators.
From FOREIGN KEY Constraints
In a foreign key reference, a link is created between two tables when
the column or columns that hold the primary key value for one table
are referenced by the column or columns in another table. This column
becomes a foreign key in the second table.
And it does not seem to be what you are looking for.

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.

Defining Multiple Foreign Keys in Visual Studio

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".

Resources