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

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 :: SQL Subtraction Operator 
Sql :: insert into without column names 
Sql :: union vs intersect sql 
Sql :: there is no unique constraint matching given keys for referenced table 
Sql :: sql ending with vowels 
Sql :: import mysql database command line linux 
Sql :: mysql find duplicate rows multiple columns 
Sql :: Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000. 
Sql :: SQL select example 
Sql :: postgres get number of days between two dates 
Sql :: select into 
Sql :: location of the log postgresql linux 
Sql :: oracle avg 
Sql :: what is a unique key in sql 
Sql :: ms sql print more than 1 variable 
Sql :: remove last characters in mysql 
Sql :: sql unique constraint 
Sql :: update sql sintax 
Sql :: redo files log oracle 
Sql :: how to use timestampdiff in a table in sql 
Sql :: porcentaje sql 
Sql :: psql check tables command 
Sql :: mysql find max value row 
Sql :: dublicate row sql 
Sql :: sqlite modify row 
Sql :: copy from one table to another postgres using matching column 
Sql :: microsoft sql server python connection 
Sql :: timestamp datatype in sql 
Sql :: sql drop all tables 
Sql :: plpgsql coalesce equivalent for empty string 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =