PostgreSQL/Constraints/Unique

Материал из SQL эксперт
Версия от 13:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Assign your own name for a unique constraint

   <source lang="sql">

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=#

      </source>
   
  


Specifying the unique column using Unique function

   <source lang="sql">

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=#

      </source>
   
  


Unique and not null column

   <source lang="sql">

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=#

      </source>
   
  


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

   <source lang="sql">

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=#

      </source>
   
  


Unique Constraints

   <source lang="sql">

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=#

      </source>