How can I have 2 foreign keys in SQL Server 2008? - sql-server

Simple question but seems very difficult for me. I need 2 foreign keys in my table and after, will use query builder to get specific columns that I need.
Here is an image:
Currently, the ProductID already has a relation with my Products table. What I want now is to have another relation with my CustomerID to my CustomerProducts table. Any idea or reference on how to do it?

Why not just use code to create the foreign keys?
ALTER TABLE YourTable
ADD CONSTRAINT NameYourFK FOREIGN KEY (YourColumn) REFERENCES YourOtherTable (YourOtherIdentityColumn)

Related

Foreign Key constraint on set of rows of a table data sql server

I have a ValidationTable like
and OrderProcessing table like
I want to create foreign key constraint on OrderProcessing table on column OrderType that references first 3 rows of ValidationTable (of ValidationType = "orders"). Is this possible? I referred similar questions but those did not really help.
I don't believe it is possible to link a specific set of rows.
The constraint has to be for the whole column.

Change Primary Key with custom identity seed on existing table SQL Server

I very new in SQL Server it's about 3 months. Now, I am stuck with a problem.
I need to change the primary key on a table with lots of data, about 10000 rows. Some other tables have relationship with this table (FK), but some tables stand alone. i wanna change the primary key and change the start identity seed with i want.
I have browsing in google but still no luck.
Can someone in here give me solution.
Thanks..
1)To DROP a PRIMARY KEY Constraint
ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID
2)Alter table with new primary constarint
ALTER TABLE Persons
ADD PRIMARY KEY (pk_PersonID)

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.

SQL Server: how to reference a foreign key to another table where the key is named differently

Trying to write the script to reference a foreign key in another table that is named differently. Here's part of my script, see if you can figure out what im trying to do, I don't know a better way to explain it:
ALTER TABLE journal
ADD CONSTRAINT journal_authorid_FK FOREIGN KEY(author_id) REFERENCES employee.emp_id;
as you can see, the author_id in one table references the emp_id primary key in another table. Reason being is that there is already a key in this table called emp_id.The emp_id FK in this table will be used to identify who this journal entry pertains to. The author_id is the person that made the entry. Obviously they are both located on the employee table. How do I make this relationship?
I believe the syntax is:
ALTER TABLE journal
ADD CONSTRAINT journal_authorid_FK FOREIGN KEY(author_id) REFERENCES employee(emp_id);

Resources