Search
 
SCRIPT & CODE EXAMPLE
 

SQL

add foreign key constraint in postgresql

ALTER TABLE orders
    ADD CONSTRAINT fk_orders_customers FOREIGN KEY (customer_id) REFERENCES customers (id);
Comment

create foreign key postgres

DROP TABLE IF EXISTS contacts;
DROP TABLE IF EXISTS customers;

CREATE TABLE customers(
   customer_id INT GENERATED ALWAYS AS IDENTITY,
   customer_name VARCHAR(255) NOT NULL,
   PRIMARY KEY(customer_id)
);

CREATE TABLE contacts(
   contact_id INT GENERATED ALWAYS AS IDENTITY,
   customer_id INT,
   contact_name VARCHAR(255) NOT NULL,
   phone VARCHAR(15),
   email VARCHAR(100),
   PRIMARY KEY(contact_id),
   CONSTRAINT fk_customer
      FOREIGN KEY(customer_id) 
	  REFERENCES customers(customer_id)
	  ON DELETE CASCADE
);

INSERT INTO customers(customer_name)
VALUES('BlueBird Inc'),
      ('Dolphin LLC');	   
	   
INSERT INTO contacts(customer_id, contact_name, phone, email)
VALUES(1,'John Doe','(408)-111-1234','john.doe@bluebird.dev'),
      (1,'Jane Doe','(408)-111-1235','jane.doe@bluebird.dev'),
      (2,'David Wright','(408)-222-1234','david.wright@dolphin.dev');
Code language: SQL (Structured Query Language) (sql)
Comment

postgres add foreign key to existing table

ALTER TABLE tblA ADD COLUMN colA VARCHAR(10); /* 1st add column */
ALTER TABLE tblA ADD CONSTRAINT colA /* Make colA on tblA a foreign key */
FOREIGN KEY (colA) REFERENCES tblB (colB); /* colB must be a pk on tblB*/
Comment

create table postgresql foreign key

CREATE TABLE so_items (
	so_id INTEGER,
  	...
	FOREIGN KEY (so_id) REFERENCES so_headers (id)
);
Comment

postgre alter table foreign key

ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
Comment

add foreign key to existing table postgres

ALTER TABLE table_a
ADD FOREIGN KEY (col_table_a) REFERENCES table_b(pk_table_b);
Comment

postgres add foreign key alter table

CREATE TABLE orders (
    order_id SERIAL,
    dish_name TEXT,
    customer_id INTEGER REFERENCES customers (id)
);
Comment

PostgreSQL foreign key constraint syntax

[CONSTRAINT fk_name]
   FOREIGN KEY(fk_columns) 
   REFERENCES parent_table(parent_key_columns)
   [ON DELETE delete_action]
   [ON UPDATE update_action]
Code language: CSS (css)
Comment

postgres add foreign key to existing table

UPDATE student
SET name = institute.inst_name
FROM institute
WHERE student.student_id = institute.inst_id;
Comment

PREVIOUS NEXT
Code Example
Sql :: postgresql functions 
Sql :: postgresql cast string to int 
Sql :: select where sql 
Sql :: mariadb 
Sql :: sqllite format 
Sql :: if mysql UPDATE 
Sql :: restore backupfile discourse 
Sql :: identitye atama yapma SQL 
Sql :: sqlcmd xml output insert line break after every 2033 characters 
Sql :: accessing varchar array from sql 
Sql :: mysql create link between tablesdatabase 
Sql :: postgres type equivalent to timespan c# 
Sql :: mysql group by derived column 
Sql :: mysql set session timeout 
Sql :: execute oracle ash command 
Sql :: mysql set variable in a session 
Sql :: postgresql between month 
Sql :: parent: [Error: SQLITE_READONLY: attempt to write a readonly database] { 
Sql :: sqlalchemy where in query 
Sql :: second highest salary in mysql 
Sql :: plsql check how much space all databases are consuming 
Sql :: mysql missin expression near on 
Sql :: drop unique constraint 
Sql :: r dbConnect(odbc::odbc() to ms sql server remote 
Sql :: how to import sqlite driver class in java using maven 
Sql :: md5 encryption for existing records 
Sql :: SQL SERVER xquery count child nodes 
Sql :: sql how to get courses that i have made prerequisites 
Sql :: sql express database size limit 
Sql :: Raw query must include the primary key 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =