PostgreSQL/Constraints/Unique
Содержание
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=#