How do I create a mapping table in SQL Server Management Studio? - sql-server

I'm learning about table design in SQL and I'm wonder how to create a mapping table in order to establish a many-to-many relationship between two other tables?
I think the mapping table needs two primary keys - but I can't see how to create that as it appears there can only be 1 primary key column?
I'm using the Database Diagrams feature to create my tables and relationships.

The easiest way is to simply select both fields by selecting the first field, and then while holding down the Ctrl key selecting the second field. Then clicking the key icon to set them both as the primary key.

Related

Primary Key in Access

I should create a database on ms-acces. I uploaded the relational database schema. I've created the tables for employee,department,dept_locations,project and dependent.I've assigned the relationships of these. But I don't understand the works_on part.
How can i assign 2 primary keys to a table? What should I do?
Relational Database Schema
what you need is called a composite primary key. In MS Access, the way you get this is by holding down the Ctrl Key while clicking each field in the primary key.
Here is a tutorial.

Setting primary key for SQL Server view linked to Access front end

I have a SQL Server view linked to an Access front end. The view is updatable in SQL Server Management Studio, but not currently updatable through Access. After some research, it seems that I might need to specify the primary key in Access for the view in order for Access to be able to modify records in the view.
I came across this
How to set primary key when linking with CreateTableDef
which shows in the responses a couple of different approaches to setting the primary key. These two approaches are as follows:
CurrentDb.Execute "CREATE UNIQUE INDEX SomeIndex ON vwMyView (PrimaryKeyColumn) WITH PRIMARY".
and
CREATE INDEX <YourIndexName> ON <YourTableName>(<YourFieldName>) WITH PRIMARY
I am confused by the different specification of the "index" and the "field"/"column" names. If the column in my view that I want to be the primary key is myID, does that get specified as the index or field, or both? If not both, what is the other name?
Is the period at the end of the first approach a typo? I haven't seen that before.
CREATE INDEX <YourIndexName> ON <YourTableName>(<YourColumnName>) WITH PRIMARY
<YourIndexName> = Any name you want, this will be used by Access
<YourTableName> = The name of your table in Access
<YourColumnName> = The name of the column in the table

Why does my newly created FK relationship not appear in object explorer?

I just added a table "PackageItems" to an existing SQL Server 2008 R2 database. I'm working in SQL Server Management Studio. After creating the new table I created a relationship between that table and another table called "Package". The relationship FK_PackageItems_Package makes the primary key of the Package table (PackageID) the foreign key in my new PackageItems table. Both tables have a common column called PackageID (int, not null).
My question is the following. The primary and foreign keys for every other table in the database are clearly denoted in the Object Explorer panel in SSMS. I see gold or gray keys beside the columns and I also see a PK or FK in the parentheses beside. But my newly created relationship (FK_PackageItems_Package) is not represented in this way. Did I do something wrong?
Note, to create the FK relationships I entered the design view for PackageItems, clicked the Relationships icon, clicked Add, selected the "..." button next to "Tables and Columns Specification". Then in the window that appeared I chose Package as my primary key table and PackageID. And I chose PackageItems and PackageID for my foreign key table and column.
Hopefully this is not too dumb of a question. Thanks in advance for taking a look.
When you open the Create Foreign Key dialog window, you'll notice that the GUI adds the table design window. After configuring the keys and close the window, you must also save the table design.
There might be an issue when using design view to create a foreign key. If I try to create a foreign key with the GUI view, it doesn't get created. If I create it with a script, it does.
To verify if the foreign key has been created or not, you can use sp_help [TableName].

changing the primary key value in SQL server 2005

I have a Table which has a Primary Key field which is not set to auto-increment.
I want to change one of these primary keys to something different.
The problem arises with the other tables relations. The thing is, the guy who built this system did not build relations in SQL Server, but rather manually coded some override in the program that uses it - a VB 6 program.
How would I update a Primary Key and all instances of the Primary Key in other databases? I have to manually look for the instances(although I do know they are in only two tables) of the Primary Key and change them, but how do I do that?
Even though the person who first created the tables didn't include foreign keys to them, you can add them now if foreign keys are honored in your tables. When you create the foreign keys, create them with ON UPDATE CASCADE option. This way, when you update your primary key, the related foreign keys will also be updated.
One thing I would suggest is using the query of:
select * from INFORMATION_SCHEMA.Columns where Column_Name = 'FieldID'
This queries the metadata to see all where that field exists, just in case there are more. Then, just write an update script to change the key, unfortunately its a manual process but being that the relationship is missing it will make scripting easier.

Identity and primary key

c# + sql server: I have to import a few tables from Access into sql server. The new sql server tables are already defined and created. The Access tables have primary key - foreign key relationships that have to be maintained. I would like to have the new sql tables use identity values for the primary keys, but I can't easily load the legacy rows and maintain the relationships if I do that. I could possibly load the data using identity_insert but I have to back way out of my ORM software (subsonic) to do that. Perhaps I can make my new primary keys non-identity types, but then there is the hassle of generating unique ids for all the rows I add later. I'm sure this has a decent solution out there somewhere.
Could you import the primary keys as non-identity types, then once all the data from all the tables is imported change them to identity types?
Create the tables with the PKs as "int" (without identity), load your data, and then change the PK fields to int identity...

Resources