PostgreSQL/Constraints/Unique

Материал из SQL эксперт

Перейти к: навигация, поиск

Содержание

Assign your own name for a unique constraint

postgres=# -- Assign your own name for a unique constraint
postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer CONSTRAINT must_be_different UNIQUE,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
NOTICE:  CREATE TABLE / UNIQUE will CREATE implicit INDEX "must_be_different" FOR TABLE "products"
CREATE TABLE
postgres=#
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#



Specifying the unique column using Unique function

postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer,
postgres(#    name text,
postgres(#    price numeric,
postgres(#    UNIQUE (product_no)
postgres(# );
NOTICE:  CREATE TABLE / UNIQUE will CREATE implicit INDEX "products_product_no_key" FOR TABLE "products"
CREATE TABLE
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#
postgres=#



Unique and not null column

postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer UNIQUE NOT NULL,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
NOTICE:  CREATE TABLE / UNIQUE will CREATE implicit INDEX "products_product_no_key" FOR TABLE "products"
CREATE TABLE
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#



Unique constraint refers to a group of columns, the columns are listed separated by commas

postgres=#
postgres=#
postgres=# -- Unique constraint refers to a group of columns, the columns are listed separated by commas:
postgres=#
postgres=# CREATE TABLE example (
postgres(#    a integer,
postgres(#    b integer,
postgres(#    c integer,
postgres(#    UNIQUE (a, c)
postgres(# );
NOTICE:  CREATE TABLE / UNIQUE will CREATE implicit INDEX "example_a_key" FOR TABLE "example"
CREATE TABLE
postgres=#
postgres=# drop table example;
DROP TABLE
postgres=#



Unique Constraints

postgres=#
postgres=# -- Unique Constraints
postgres=#
postgres=# CREATE TABLE products (
postgres(#    product_no integer UNIQUE,
postgres(#    name text,
postgres(#    price numeric
postgres(# );
NOTICE:  CREATE TABLE / UNIQUE will CREATE implicit INDEX "products_product_no_key" FOR TABLE "products"
CREATE TABLE
postgres=#
postgres=# drop table products;
DROP TABLE
postgres=#
postgres=#