Creating a foreign key relationship through the UI in VS2015 SQL Server Database Project - sql-server

I added a new SQL Server database project to my solution in VS2015. I added all of my tables to this project.
I am now trying to figure out how to add foreign keys to the project through the UI preferably .
I am in the table designer and on the right side I click foreign keys and then add new. This adds a new foreign key, but when I go to the properties of it I am unable to set the columns and tables. Everything is grayed out.
The T-SQL looks like this:
CONSTRAINT [FK_Product_ToTable]
FOREIGN KEY ([Column]) REFERENCES [ToTable]([ToTableColumn])
I know I can just simply replace the params with the correct info in the T-SQL pane, but is there any way to completely do it through the UI and let the T-SQL update on its own?

double click your table in solution explorer to edit the columns
add FK column to the table, optionally check "Allow Nulls"
to the right of the table column list, right click "Foreign Keys"
a menu will appear with an item "Add New Foreign Key"
click that item, and you will see it add a new FK constraint under the "Foreign Keys" list. Rename the FK to your liking, then press ENTER.
This will add a FK SQL statement in the "T-SQL" window below the table column list.
edit the SQL script to define the "Column", "ToTable", and "ToTableColumn" for your foreign key.
enter image description here

Related

Strange Issue with Primary Key Addition

I am using SQL server 2008 r2.
I was creating a table implicitly i.e without writing queries and tried to add primary key to one of the column by right clicking and selecting primary key.
But i could not add the primary key as it was not reflected there, i refreshed the table , the database and also closed the connection and re-connected but primary key was still not reflected on that column name. I tried this thrice.
Then I added primary key using query (Table was empty)
alter table [Table Name]
Add Primary key ([column name])
and then it got reflected in the table.
I tried searching for reason but could not find any convincing reason.
If anybody knows reason for this please reply.
ALTER TABLE [TableName]
ADD CONSTRAINT [PrimaryKeyName] PRIMARY KEY CLUSTERED ([ExistingColumn])
Make sure the column is NOT NULL i.e doesnt allow NULL values to be inserted.
Please check that you have "UNCHECKED" checkbox which says Prevent saving changes that require the table re-creation. You can follow below instruction to uncheck it.
Open SQL Server Management Studio (SSMS).
On the Tools menu, click Options.
In the navigation pane of the Options window, click Designers.
Select or clear the Prevent saving changes that require the table
re-creation check box, and then click OK.

SQL Server Create multiple foreign keys

The table contains a PRIMARY KEY column and another column which is FOREIGN KEY. This works fine. When I attempt to add another column as a FOREIGN KEY I get the following message:
Link to pic
This is unrelated to the PKs or FKs.
There is a setting in SQL Server 2008 SSSM to prevent table rebuilds via the designer.
Tools..Options..Designers... clear the option “Prevent saving changes that require table re-creation”.

how to add foreign key constraint, SQL Server

I have a registration table with primary key RegId. I have another table named Users, also contain RegId as Foreign key.
When I delete one RegId from registration, I have to delete RegId from Users. Can anybody help?
Define the foreign key with "ON DELETE CASCADE".
You can do this is in T-SQL or in design view in SSMS
You could to alter the main table with a constraint saying that a Delete On Cascade must be done.
Based on your inputs, something similar to this:
ALTER TABLE dbo.Registration
ADD CONSTRAINT FK_Registration_Users_Cascade
FOREIGN KEY (RegId) REFERENCES dbo.Users(RegId) ON DELETE CASCADE
Also, you can achieve this with the SQL Management Studio selecting your table (in Design mode) and go to Relationships option. There you'll see the "INSERT and UPDATE specifications" button for setting this things.
MSDN Article
Cascading Referential Integrity Constraints

How does the constraints GUI work in SQL Server Management Studio?

In my TSQL table I added a constraint with the following sQL statement
alter table disabledqualities
add constraint uc_uIdQualCode
unique (userId, qualitycode)
I did it by hand because I just can't work out how the GUI is supposed to work.
You add a constraint and then you get a window where you should "define" your constraint. It's basically just a textbox. And no matter what I seem to enter, it never validates..
What am I supposed to enter in the box?
you would use new index not new constraint to add a unique constraint (read index)
new constraint is to add check constraints
in the new index window check unique
Example, column must be between 0 and 1,
((0)<=[TABLE].[COLUMN] AND [TABLE].[COLUMN]<=(1))
When adding unique constraints, it's actually an index, like primary key, so you click on indexes/keys.

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