postgresql create table many-to-many 1

postgresql create table many-to-many

CREATE TABLE product (
  product_id serial PRIMARY KEY  -- implicit primary key constraint
, product    text NOT NULL
, price      numeric NOT NULL DEFAULT 0
);

CREATE TABLE bill (
  bill_id  serial PRIMARY KEY
, bill     text NOT NULL
, billdate date NOT NULL DEFAULT CURRENT_DATE
);

CREATE TABLE bill_product (
  bill_id    int REFERENCES bill (bill_id) ON UPDATE CASCADE ON DELETE CASCADE
, product_id int REFERENCES product (product_id) ON UPDATE CASCADE
, amount     numeric NOT NULL DEFAULT 1
, CONSTRAINT bill_product_pkey PRIMARY KEY (bill_id, product_id)  -- explicit pk
);

Here is what the above code is Doing:
1. Create a table called product with 3 columns: product_id, product, and price.
2. Create a table called bill with 3 columns: bill_id, bill, and billdate.
3. Create a table called bill_product with 4 columns: bill_id, product_id, amount, and a primary key.

The primary key is a special type of constraint that ensures that the values in a column are unique.

The primary key is also used to reference a row in another table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a set of columns that uniquely identifies a row in a table.

The primary key is a column or a

Similar Posts