I have quite the SQL conundrum.
I am a junior programmer in their first co-op position and I have been given a database with three tables. There are no primary keys on any of the three tables. These tables have no initial relationships with each other. This is how the database is (roughly) designed.
Table 1 Table2 Table3
======== ======== ========
SSerial BSerial MSerial
B1 data2 data2
B2 data3 data3
B3 data4 data4
M1 data5 data5
M2 data6 data6
M3 data7 data7
SSerial, BSerial and MSerial have unique alphanumeric values that will never be repeated(nvarchar(MAX)).
BSerial's values can be found in B1, B2 and B3 of Table 1.
MSerial's values can be found in M1, M2 and M3 of Table1.
The end goal of the application I am working on is allowing the client to search by the unique SSerial number in Table 1, then view the BSerial values that are in B1, B2 and B3. They will also be able to do the same with MSerial's values that are in M1, M2 and M3.
I have tried implementing a primary key so that I may add a relationship between Table1 and the other tables, but with no avail.
When I attempted to create a primary key:
ALTER TABLE dbo.Table2
ADD Table2_ID int NOT NULL IDENTITY (1,1);
ALTER TABLE dbo.Table2
ADD CONSTRAINT PK_Table2_ID PRIMARY KEY (Table2_ID, BSerial);
I was blessed with the following error message:
Msg 1919, Level 16, State 1, Line 1
Column 'BSerial' in table 'Table2' is of a type that is invalid for use as a key column in an index.
Additional info:
- SSerial, BSerial and MSerial are NOT NULL and are of type NVARCHAR(MAX).
- B1, B2, B3, M1, M2 and M3 ALLOW NULLS, as there will not always be three BSerial's or MSerial's associated to SSerial. They are of type NVARCHAR(MAX).
- Recreating the database from scratch is not ideal as there are thousands of rows of data in each table, but it can be done.
This is my first Stack Overflow question and if more information is needed I am more than welcome to elaborate. Thank you for your time!