| View previous topic :: View next topic |
| Author |
Message |
el_tonio Newbie

Joined: 04 Dec 2011 Posts: 1
|
Posted: Sun Dec 04, 2011 7:00 am Post subject: HELP failing with relationships |
|
|
Hey,
I'm trying to link 2 tables and failing, every set of fields I try to link returns:
| Quote: | | Primary or unique constraint required on main table: "y" in statement [ALTER TABLE "x" ADD FOREIGN KEY ("Surname") REFERENCES "y" (Surname")] |
So I looked on the Internet for some tutorials but non that help me with the issue.
Help would be much appreciated |
|
| Back to top |
|
 |
mgroenescheij Super User

Joined: 20 Apr 2011 Posts: 862 Location: Australia
|
Posted: Tue Dec 06, 2011 3:47 am Post subject: |
|
|
Hi,
Edit the table and add a Primary Key to the tables.
BTW it is useful if you give some information on the OS, OpenOffice version and Database you use.
Martin _________________ If your problem has been solved please add "[Solved]" to the beginning of your first post title (edit button). |
|
| Back to top |
|
 |
hollyneal Newbie

Joined: 19 Jan 2012 Posts: 3
|
Posted: Thu Jan 19, 2012 4:33 pm Post subject: Failing at relationships |
|
|
I saw another thread on this but it didn't answer my question. I'm getting a Primary and Unique Constraint message.
I've got two tables:
Table 1
Name and SSN.
Table 2
Bname and Birthday
I want to relate Name and Bname. I'm confused as to why I wouldn't be able to do this.
I'm using OpenOffice version 3.3.0.
Any help will be much appreciated. Thanks. |
|
| Back to top |
|
 |
mgroenescheij Super User

Joined: 20 Apr 2011 Posts: 862 Location: Australia
|
Posted: Thu Jan 19, 2012 10:51 pm Post subject: |
|
|
Hi,
The Primary and Unique Constraint message tells you that you don't have a Primary Key.
This Key should be Unique.
In both Tables you have to add a Primary Key.
In Table 2 you need to Replace the Name with the corresponding Primary Key from Table 1.
Make sure you assign Field Type Integer to the Keys.
Martin _________________ If your problem has been solved please add "[Solved]" to the beginning of your first post title (edit button). |
|
| Back to top |
|
 |
hollyneal Newbie

Joined: 19 Jan 2012 Posts: 3
|
Posted: Fri Jan 20, 2012 3:47 pm Post subject: |
|
|
Ok. I now have tables with an auto-enter ID field as the primary key.
How do I set up foreign keys? |
|
| Back to top |
|
 |
mgroenescheij Super User

Joined: 20 Apr 2011 Posts: 862 Location: Australia
|
Posted: Fri Jan 20, 2012 6:31 pm Post subject: |
|
|
Hi,
Assume your tables are something like: | Quote: | Table 1
ID Integer AutoValue Primary Key
Name VARCHAR(50)
SSN VARCHAR(50)
Table 2
ID Integer AutoValue Primary Key
Name Integer
Bname VARCHAR(50)
Birthday DATE |
Now select Tools --> Relationships add the two tables and select ID from Table 1 and drag it to Name in Table 2
That's all.
Martin _________________ If your problem has been solved please add "[Solved]" to the beginning of your first post title (edit button). |
|
| Back to top |
|
 |
hollyneal Newbie

Joined: 19 Jan 2012 Posts: 3
|
Posted: Sat Jan 21, 2012 6:34 am Post subject: More on relationships |
|
|
Thank you Martin. I really appreciate your help.
Now what I'm trying to do is have one record in tblSSN to match multiple records in a different table, say tblPetOwnership. I have multiple pets and I want each record in tblSSN to be related to multiple records in t/lPetOwnership. The only data those records have in common is the name.
I've got multiple tables and they all have the name field in common. Suppose I want to look at everything that has my name on it? I know I can do that with a query but that limits it to coding the name I'm looking for in the query itself. Is there a way to make the query dynamic? So when I run it, it asks me what name I'm looking for? That might solve my problem if it would work...
Thanks again,
Holly |
|
| Back to top |
|
 |
|