Author |
Topic |
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 11:38:19
|
I am trying to create two tables (CoreBusiness and Addresses) respectively. The two tables are joined by "ID"(CoreBusiness) and "CB_ID"(Addresses)With teh 2nd Table Addresses also having an ID field set to autoNumberI am trying to make the tables and getting errors thatI assuem that I am settign my Primary Keys and Foreign Keys incorrectly...Can anyone out there help me to get this right to create the two tables....Many to One from CoreBusiness(ID which is autoNumber) to Addresses(CB_ID which also has ID autoNumber field) Error:Msg 2714, Level 16, State 4, Line 1There is already an object named 'PK_Person' in the database.Msg 1750, Level 16, State 0, Line 1Could not create constraint. See previous errors.CoreBusinesCREATE TABLE CoreBusiness2 ( ID INT NOT NULL IDENTITY(1,1), BldgSqFt FLOAT NULL , CONSTRAINT PK_Person PRIMARY KEY CLUSTERED (ID ASC) ON [PRIMARY] )GOAddressesCREATE TABLE Addresses2 ( ID INT NOT NULL IDENTITY(1,1), BusinessMgr VARCHAR(255) NULL , CB_ID INT NOT NULL , CONSTRAINT PK_Person PRIMARY KEY CLUSTERED (CB_ID ASC) ON [PRIMARY] )GO |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-12-08 : 11:48:36
|
Every object needs to have a unique name. Even primary keys.So you can't name it PK_Person on both tables.How 'bout PK_CoreBusiness and PK_Addresses instead? |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-08 : 11:49:36
|
constraint names have to be unique. You can't have PK_Person on both tables.I usually do pk_<TableName>.Note: you also have problems if you rename a table and create another in its place - but you can use sp_rename to rename the constraint when you rename the table.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 11:51:21
|
Thanks both of you...BUT how about Foriegn Keys....have a hard time understanding that...hwo do I set up the Foreign Key in the Addresses table?Thanks again for the help |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 11:53:15
|
I think I would have CoreBusiness PK = IDAddresses PK = ID , FK = CB_IDWhere the PK and FK would be linking the two tables? THis value would yield the Many to One relationshipI guess the question is how to create that realtionship while creating the tables? Is that correct?If not how do I do that? |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-08 : 12:11:44
|
something likealter table Addresses2 add constraint FK_Addresses2_01 foreign key (CB_ID) references CoreBusiness2 (ID)Or you can add it into the table create asconstraint FK_Addresses2_01 foreign key (CB_ID) references CoreBusiness2 (ID)orCB_ID INT NOT NULL references CoreBusiness2 (ID)I would go for the alter statement (and for the PK).==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 12:16:11
|
Whats the 01 in this line FK_Addresses2_01THanks..... |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-12-08 : 12:30:03
|
A sequence number to nake the name unique.==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 12:34:21
|
Gotcha....I did this and the CoreBusiness table created fineCREATE TABLE CoreBusiness2 ( ID INT NOT NULL IDENTITY(1,1), BldgSqFt FLOAT NULL , CONSTRAINT PK_CoreBusiness2 PRIMARY KEY CLUSTERED (ID ASC) ON [PRIMARY] )GOI did this with the Adderssess table creation and I get an eror:Error:Msg 156, Level 15, State 1, Line 30Incorrect syntax near the keyword 'CONSTRAINT'.CREATE TABLE Addresses2 ( ID INT NOT NULL IDENTITY(1,1), BusinessMgr VARCHAR(255) NULL , CB_ID INT NOT NULL , CONSTRAINT PK_Addresses2 PRIMARY KEY CLUSTERED (ID ASC) CONSTRAINT FK_Addresses2_01 FOREIGN KEY(CB_ID) references CoreBusiness2 (ID) ON [PRIMARY] )GOTHANK YOU VERY MUCH FOR YOUR HELP.... |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 13:26:52
|
Any thoughts with the syntax error on my last post...when creating the PK and FK?Thanks |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 13:47:54
|
GOT IT..was missing the , (comma) after:CONSTRAINT PK_Addresses2 PRIMARY KEY CLUSTERED (ID ASC) |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 13:56:33
|
DONT KNOW IF I SHOULD POST IN ANOTHER ENTRY? BUT THIS SORT OF IS RELATING TO THE SAME ISSUE..I am now trying to INSERT into this newly created tables and getting errors:The INSERT into the CoreBusiness worked fineI am getting errors when tryign to copy records into the Addresses table...think cause of the FK and PK?????Addresses Table SQL statement INSERT INTOINSERT INTO Addresses2(BusinessMgr, CB_ID) SELECT [Business Mgr], CB_IDFROM AddressesERROR: NOTING THAT ID was the PK in BOTH TABLESMsg 547, Level 16, State 0, Line 1The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Addresses2_01". The conflict occurred in database "BusinessDatabaseTestSilverlight", table "dbo.CoreBusiness2", column 'ID'.The statement has been terminated.Do I have to copy records with the INSERT INTO and then use ALTER to add the PK and FK? |
|
|
X002548
Not Just a Number
15586 Posts |
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 15:12:04
|
INteresting results...In CoreBusiness table I have 1887 recordsIn Addresses I have 1925 recordsSo you can see there is a many to one relationship going on...This is only test data...I think I should be using WHERE i.ID = o.CB_ID instead of WHERE i.ID = o.ID because the PK - FK relation is ID - CB_ID from CoreBusiness to AddressesI am positive that there is an ID - CB_ID match for every record...only there are multiple CB_ID matching with single IDIf I do this I get 1365 Records Copied in...I know there are 1925 in teh Addresses table adn thats all the records are linkedINSERT INTO Addresses2(BusinessMgr,CB_ID) SELECT [Business Mgr],CB_IDFROM Addresses oWHERE EXISTS (SELECT * FROM CoreBusiness2 i WHERE i.ID = o.ID)If I do this I get 1405 Records Copied in...I used CB_ID because CB_ID is the foreign Key to ID in the CoreBusiness TableINSERT INTO Addresses2(BusinessMgr,CB_ID) SELECT [Business Mgr],CB_IDFROM Addresses oWHERE EXISTS (SELECT * FROM CoreBusiness2 i WHERE i.ID = o.CB_ID) |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 15:17:53
|
Wait a second...when I recreate the CoreBusiness table I am recreating the ID field...so I think I am breaking the Relationship from ID to CB_IDhmmmmm |
|
|
Jaykappy
Starting Member
19 Posts |
Posted - 2011-12-08 : 17:21:46
|
Yea that was it...I think I got what I needed from this...my major problem is that I have two tables existing...code is written in Silverlight and I have to now modify the tables and create AutoNumbers and a new FK..If I recreate a new Parent table with AutoNumber it breaks the relationship.I moved the unique value to another field and then recreated the table, thus retaining my connection to the child table.I then updated the FK in teh child table with the newly created Unique ID from the Parent table and recreated the PK and FK on the child table...But when I try and replace this in silverlight the sky falls apart....So as it stands I got the talbes in SQL server working...anyone have any suggestions on how to slip these into Silverlight without error please let me know...THanks |
|
|
|