Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/03/2009, 17:07
Avatar de Sergestux
Sergestux
 
Fecha de Ingreso: agosto-2007
Ubicación: Tapachula
Mensajes: 1.218
Antigüedad: 17 años, 3 meses
Puntos: 20
Respuesta: No puedo crear una llave foranea

Por aca esta la manera correcta de crear llaves foraneas en postgresql

Código sql:
Ver original
  1. /*
  2. 5.3.5. Foreign Keys
  3.  
  4. A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table.
  5. We say this maintains the referential integrity between two related tables.
  6. Say you have the product table that we have used several times already:
  7. */
  8.  
  9. CREATE TABLE products (
  10.     product_no INTEGER PRIMARY KEY,
  11.     name text,
  12.     price NUMERIC
  13. );
  14.  
  15. /*
  16. Let's also assume you have a table storing orders of those products.
  17. We want to ensure that the orders table only contains orders of products that actually exist.
  18.  
  19. So we define a foreign key constraint in the orders table that references the products table:
  20. */
  21.  
  22. CREATE TABLE orders (
  23.     order_id INTEGER PRIMARY KEY,
  24.     product_no INTEGER REFERENCES products (product_no),
  25.     quantity INTEGER
  26. );
  27.  
  28. /*
  29. Now it is impossible to create orders with product_no entries that do not appear in the products table.
  30.  
  31. We say that in this situation the orders table is the referencing table and the products table is the referenced table.
  32. Similarly, there are referencing and referenced columns.
  33. */