¿Relación entre clave externa y primaria?

Por ejemplo:

Tabla 1:

user_ID - 
username -
first_name -
email -

Tabla 2:

message_ID - 
user_ID - 
message_title -
message_body - 

As you can see I have user_ID as primary key in table 1, and as foreign key in the second table.

If I enter 2 rows into the first table, eg.

1, undergz, sarah, sarah@hotmail.com
2, michel21, michaele, michalzost@gmail.com

And now, michaele enters the message, and I now I add this to table 2:

1, 2, hello, hello everybody, 

I added this entry in the table 2 considering first table, I added user_ID number the same as in the table 1.

I did this from $_SESSION['user_ID'] variable, when I entered the second query. So it was like

INSERT INTO table_2 VALUES (NULL, {$_SESSION['user_id']}, '$title','$message') 

My question is, is this the right way to do this? We define foreign key in table so we enter data STRICTLY according to primary key, it must be the same for all values?

preguntado el 02 de febrero de 12 a las 11:02

"As you can see I have user_ID as primary key in table 1, and as foreign key in the second table" -- I can see there are attributes of the same name in both tables but this does not prove the existence of a FOREIGN KEY constraint. Have you created a FOREIGN KEY constraint? You need to tell us, we can't see your SQL DDL. -

no I didn't create FOREIGN KEY explicitly. Do I need to? If I just link all tables with data in order I enter it, is that enough? -

Short answer: yes, you need to. -

2 Respuestas

The question you should ask yourself is:

Can a row in the table 2 exist without a corresponding row in the table 1?

If the answer to your question is "no", then you need the FOREIGN KEY (a.k.a. integridad referencial) constraint from table 2 ("child") to table 1 ("parent"). You can think of it as having a "pointer" from one row to another, where the DBMS itself garantías you can never have a "dangling" pointer.

The "parent" set of columns in the FOREIGN KEY must always be the key, be it primary or alternate (a.k.a. UNIQUE). The "child" can be any column or combination of columns, whether they are part of a key or not. You can think of that as the parent's key columns being propagated to the child, and depending on dónde the propagated columns end up, you can have the following 2 kinds of relationships:

1) "Identifying" Relationship

The parent key becomes a part of the child key (so parent "helps" identify the child):

relación de identificación or relación de identificación

This models either 1:N or 1:1 relationship.

2) "Non-Identifying" Relationship

The parent key does no become the part of the child's key:

relación no identificable

This models 1:N relationship, but requires message_ID to be unique on its own (instead of in combination with user_ID).

Respondido 03 Feb 12, 06:02

You can create a FOREIGN KEY constraint as part of the table definition when you create a table. If a table already exists, you can add a FOREIGN KEY constraint, provided that the FOREIGN KEY constraint is linked to an existing PRIMARY KEY constraints or UNIQUE constraint in another, or the same, table. A table can contain multiple FOREIGN KEY constraints.

If a FOREIGN KEY constraint already exists, you can modify or delete it. For example, you may want the FOREIGN KEY constraint of the table to reference other columns. However, you cannot change the length of a column that is defined with a FOREIGN KEY constraint.

So, you can only had values in table2 con un user_ID that is already present in table1.

Regarding your question(s):

My question is, is this the right way to do this?

It seems that yes.

We define foreign key in table so we enter data STRICTLY according to primary key


it must be the same for all values?

It depends, it must be a value that already exists in table1. In your case, for each login (user), the value should be from that user. But if you login with another, the value should be the id from that user.

Respondido el 20 de junio de 20 a las 12:06

I agree, the thing I needed to check is that entering all values in foreign key is on all developer, the database doesn't make difference between foreign key and primary key. If I skip to enter foreign key, it will remain NULL value, and won't be automatically field right? - Zack Westwater

Yes, you have to put the value. In the insertion the database doesn't assign it automatically. - aF.

A foreign key isn't a "field", it's a constraint on one or more columns of a table to make their values refer to existing values in another table. It doesn't have to refer to a primary key in the other table (it can be unique). - Marrón

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.