Aprendiendo sobre llaves foráneas en SQL ['ESP','ENG']

Hace un tiempo atrás, casi 1 año había empezado a aprender SQL y me centre en aprender a crear tablas; me centre en aprender que valores se le debe permitir asignar a cada columna de la tabla.
Mientras estudia con la documentación de “PostgreSQL” encontré que existen restricciones para crear relaciones entre las tablas.

Some time ago, almost 1 year ago, I had started learning SQL and I focused on learning how to create tables; I focused on learning what values ​​should be allowed to assign to each column in the table. While studying the “PostgreSQL” documentation I found that there are restrictions on creating relationships between tables.

fk1.jpg
Mis conocimientos aún son básicos sobre SQL, pero entendí lo siguiente, una llave foránea sirve para crear una relación estricta con otra tabla, de manera que si alguien quiere insertar ciertos valores en la tabla con esta restricción SQL dará un mensaje de alerta pidiendo al usuario que le indique a quien debe hacer referencia este valor y cual tabla pertenece para ingresar dicho valor.
Esto me encanto de SQL porque es igual a indicar que cierto usuario registrado en la aplicación tiene datos personales y a la hora de insertarlos en la base de datos debemos indicar la referencia del usuario, ya sea a través de una “Primary KEY” u algún otro tipo de ID.
En este ejemplo mostrare algo sencillo de entender con este tipo de restricciones.

My knowledge is still basic about SQL, but I understood the following, a foreign key is used to create a strict relationship with another table, so that if someone wants to insert certain values ​​into the table with this constraint SQL will give an alert message asking the user that tells you who this value should refer to and which table it belongs to to enter said value. I loved this about SQL because it is the same as indicating that a certain user registered in the application has personal data and when inserting them into the database we must indicate the user's reference, either through a “Primary KEY” or some other ID type. In this example I will show something easy to understand with this type of restrictions.

fk2.jpg


fk3.jpg


fk4.jpg
Como se podrá ver en las imágenes hay dos datos ingresados en la tabla con la llave foránea, porque una de las columnas no posee restricciones, pero la otra si, esto lo hice con el propósito de mostrar que la restricción no permite ingresar datos en esa columna, pero en las demás sí.

As you can see in the images, there are two data entered into the table with the foreign key, because one of the columns does not have restrictions, but the other does, I did this with the purpose of showing that the restriction does not allow data to be entered in that one. column, but in the others yes.

fk5.jpg
Al momento de ingresar datos en la con la restricción debo asignarle el valor del id del usuario al cual hace referencia este valor, por ende, esto es lo que podríamos llamar tablas relacionales con restricciones.

When entering data in the table with the restriction, I must assign the value of the user id to which this value refers, therefore, this is what we could call relational tables with restrictions.


0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar

¡Enhorabuena!


Has recibido el voto de PROYECTO CHESS BROTHERS

✅ Has hecho un buen trabajo, por lo cual tu publicación ha sido valorada y ha recibido el apoyo de parte de CHESS BROTHERS ♔ 💪


♟ Te invitamos a usar nuestra etiqueta #chessbrothers y a que aprendas más sobre nosotros.

♟♟ También puedes contactarnos en nuestro servidor de Discord y promocionar allí tus publicaciones.

♟♟♟ Considera unirte a nuestro trail de curación para que trabajemos en equipo y recibas recompensas automáticamente.

♞♟ Echa un vistazo a nuestra cuenta @chessbrotherspro para que te informes sobre el proceso de curación llevado a diario por nuestro equipo.


🏅 Si quieres obtener ganancias con tu delegacion de HP y apoyar a nuestro proyecto, te invitamos a unirte al plan Master Investor. Aquí puedes aprender cómo hacerlo.


Cordialmente

El equipo de CHESS BROTHERS

0
0
0.000