--
-- CONSTRAINTS
-- Constraints can be specified with:
--  - DEFAULT clause
--  - CHECK clauses
--  - PRIMARY KEY clauses
--  - UNIQUE clauses
--
--
-- DEFAULT syntax
--
CREATE TABLE DEFAULT_TBL (i int DEFAULT 100,
	x text DEFAULT 'vadim', f float8 DEFAULT 123.456);
INSERT INTO DEFAULT_TBL VALUES (1, 'thomas', 57.0613);
INSERT INTO DEFAULT_TBL VALUES (1, 'bruce');
INSERT INTO DEFAULT_TBL (i, f) VALUES (2, 987.654);
INSERT INTO DEFAULT_TBL (x) VALUES ('marc');
INSERT INTO DEFAULT_TBL VALUES (3, null, 1.0);
SELECT '' AS five, * FROM DEFAULT_TBL;
 five |  i  |   x    |    f    
------+-----+--------+---------
      |   1 | thomas | 57.0613
      |   1 | bruce  | 123.456
      |   2 | vadim  | 987.654
      | 100 | marc   | 123.456
      |   3 |        |       1
(5 rows)

CREATE SEQUENCE DEFAULT_SEQ;
CREATE TABLE DEFAULTEXPR_TBL (i1 int DEFAULT 100 + (200-199) * 2,
	i2 int DEFAULT nextval('default_seq'));
INSERT INTO DEFAULTEXPR_TBL VALUES (-1, -2);
INSERT INTO DEFAULTEXPR_TBL (i1) VALUES (-3);
INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (-4);
INSERT INTO DEFAULTEXPR_TBL (i2) VALUES (NULL);
SELECT '' AS four, * FROM DEFAULTEXPR_TBL;
 four | i1  | i2 
------+-----+----
      |  -1 | -2
      |  -3 |  1
      | 102 | -4
      | 102 |   
(4 rows)

-- syntax errors
--  test for extraneous comma
CREATE TABLE error_tbl (i int DEFAULT (100, ));
ERROR:  syntax error at or near ")" at character 45
LINE 1: CREATE TABLE error_tbl (i int DEFAULT (100, ));
                                                    ^
--  this will fail because gram.y uses b_expr not a_expr for defaults,
--  to avoid a shift/reduce conflict that arises from NOT NULL being
--  part of the column definition syntax:
CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
ERROR:  syntax error at or near "IN" at character 43
LINE 1: CREATE TABLE error_tbl (b1 bool DEFAULT 1 IN (1, 2));
                                                  ^
--  this should work, however:
CREATE TABLE error_tbl (b1 bool DEFAULT (1 IN (1, 2)));
DROP TABLE error_tbl;
--
-- CHECK syntax
--
CREATE TABLE CHECK_TBL (x int,
	CONSTRAINT CHECK_CON CHECK (x > 3));
INSERT INTO CHECK_TBL VALUES (5);
INSERT INTO CHECK_TBL VALUES (4);
INSERT INTO CHECK_TBL VALUES (3);
ERROR:  new row for relation "check_tbl" violates check constraint "check_con"
INSERT INTO CHECK_TBL VALUES (2);
ERROR:  new row for relation "check_tbl" violates check constraint "check_con"
INSERT INTO CHECK_TBL VALUES (6);
INSERT INTO CHECK_TBL VALUES (1);
ERROR:  new row for relation "check_tbl" violates check constraint "check_con"
SELECT '' AS three, * FROM CHECK_TBL;
 three | x 
-------+---
       | 5
       | 4
       | 6
(3 rows)

CREATE SEQUENCE CHECK_SEQ;
CREATE TABLE CHECK2_TBL (x int, y text, z int,
	CONSTRAINT SEQUENCE_CON
	CHECK (x > 3 and y <> 'check failed' and z < 8));
INSERT INTO CHECK2_TBL VALUES (4, 'check ok', -2);
INSERT INTO CHECK2_TBL VALUES (1, 'x check failed', -2);
ERROR:  new row for relation "check2_tbl" violates check constraint "sequence_con"
INSERT INTO CHECK2_TBL VALUES (5, 'z check failed', 10);
ERROR:  new row for relation "check2_tbl" violates check constraint "sequence_con"
INSERT INTO CHECK2_TBL VALUES (0, 'check failed', -2);
ERROR:  new row for relation "check2_tbl" violates check constraint "sequence_con"
INSERT INTO CHECK2_TBL VALUES (6, 'check failed', 11);
ERROR:  new row for relation "check2_tbl" violates check constraint "sequence_con"
INSERT INTO CHECK2_TBL VALUES (7, 'check ok', 7);
SELECT '' AS two, * from CHECK2_TBL;
 two | x |    y     | z  
-----+---+----------+----
     | 4 | check ok | -2
     | 7 | check ok |  7
(2 rows)

--
-- Check constraints on INSERT
--
CREATE SEQUENCE INSERT_SEQ;
CREATE TABLE INSERT_TBL (x INT DEFAULT nextval('insert_seq'),
	y TEXT DEFAULT '-NULL-',
	z INT DEFAULT -1 * currval('insert_seq'),
	CONSTRAINT INSERT_CON CHECK (x >= 3 AND y <> 'check failed' AND x < 8),
	CHECK (x + z = 0));
INSERT INTO INSERT_TBL(x,z) VALUES (2, -2);
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
SELECT '' AS zero, * FROM INSERT_TBL;
 zero | x | y | z 
------+---+---+---
(0 rows)

SELECT 'one' AS one, nextval('insert_seq');
 one | nextval 
-----+---------
 one |       1
(1 row)

INSERT INTO INSERT_TBL(y) VALUES ('Y');
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
INSERT INTO INSERT_TBL(y) VALUES ('Y');
INSERT INTO INSERT_TBL(x,z) VALUES (1, -2);
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_tbl_check"
INSERT INTO INSERT_TBL(z,x) VALUES (-7,  7);
INSERT INTO INSERT_TBL VALUES (5, 'check failed', -5);
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
INSERT INTO INSERT_TBL VALUES (7, '!check failed', -7);
INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
SELECT '' AS four, * FROM INSERT_TBL;
 four | x |       y       | z  
------+---+---------------+----
      | 3 | Y             | -3
      | 7 | -NULL-        | -7
      | 7 | !check failed | -7
      | 4 | -!NULL-       | -4
(4 rows)

INSERT INTO INSERT_TBL(y,z) VALUES ('check failed', 4);
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_tbl_check"
INSERT INTO INSERT_TBL(x,y) VALUES (5, 'check failed');
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
INSERT INTO INSERT_TBL(x,y) VALUES (5, '!check failed');
INSERT INTO INSERT_TBL(y) VALUES ('-!NULL-');
SELECT '' AS six, * FROM INSERT_TBL;
 six | x |       y       | z  
-----+---+---------------+----
     | 3 | Y             | -3
     | 7 | -NULL-        | -7
     | 7 | !check failed | -7
     | 4 | -!NULL-       | -4
     | 5 | !check failed | -5
     | 6 | -!NULL-       | -6
(6 rows)

SELECT 'seven' AS one, nextval('insert_seq');
  one  | nextval 
-------+---------
 seven |       7
(1 row)

INSERT INTO INSERT_TBL(y) VALUES ('Y');
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
SELECT 'eight' AS one, currval('insert_seq');
  one  | currval 
-------+---------
 eight |       8
(1 row)

-- According to SQL92, it is OK to insert a record that gives rise to NULL
-- constraint-condition results.  Postgres used to reject this, but it
-- was wrong:
INSERT INTO INSERT_TBL VALUES (null, null, null);
SELECT '' AS nine, * FROM INSERT_TBL;
 nine | x |       y       | z  
------+---+---------------+----
      | 3 | Y             | -3
      | 7 | -NULL-        | -7
      | 7 | !check failed | -7
      | 4 | -!NULL-       | -4
      | 5 | !check failed | -5
      | 6 | -!NULL-       | -6
      |   |               |   
(7 rows)

--
-- Check inheritance of defaults and constraints
--
CREATE TABLE INSERT_CHILD (cx INT default 42,
	cy INT CHECK (cy > x))
	INHERITS (INSERT_TBL);
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,11);
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (7,-7,6);
ERROR:  new row for relation "insert_child" violates check constraint "insert_child_check"
INSERT INTO INSERT_CHILD(x,z,cy) VALUES (6,-7,7);
ERROR:  new row for relation "insert_child" violates check constraint "insert_tbl_check"
INSERT INTO INSERT_CHILD(x,y,z,cy) VALUES (6,'check failed',-6,7);
ERROR:  new row for relation "insert_child" violates check constraint "insert_con"
SELECT * FROM INSERT_CHILD;
 x |   y    | z  | cx | cy 
---+--------+----+----+----
 7 | -NULL- | -7 | 42 | 11
(1 row)

DROP TABLE INSERT_CHILD;
--
-- Check constraints on INSERT INTO
--
DELETE FROM INSERT_TBL;
DROP SEQUENCE INSERT_SEQ;
CREATE SEQUENCE INSERT_SEQ START 4;
CREATE TABLE tmp (xd INT, yd TEXT, zd INT);
INSERT INTO tmp VALUES (null, 'Y', null);
INSERT INTO tmp VALUES (5, '!check failed', null);
INSERT INTO tmp VALUES (null, 'try again', null);
INSERT INTO INSERT_TBL(y) select yd from tmp;
SELECT '' AS three, * FROM INSERT_TBL;
 three | x |       y       | z  
-------+---+---------------+----
       | 4 | Y             | -4
       | 5 | !check failed | -5
       | 6 | try again     | -6
(3 rows)

INSERT INTO INSERT_TBL SELECT * FROM tmp WHERE yd = 'try again';
INSERT INTO INSERT_TBL(y,z) SELECT yd, -7 FROM tmp WHERE yd = 'try again';
INSERT INTO INSERT_TBL(y,z) SELECT yd, -8 FROM tmp WHERE yd = 'try again';
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
SELECT '' AS four, * FROM INSERT_TBL;
 four | x |       y       | z  
------+---+---------------+----
      | 4 | Y             | -4
      | 5 | !check failed | -5
      | 6 | try again     | -6
      |   | try again     |   
      | 7 | try again     | -7
(5 rows)

DROP TABLE tmp;
--
-- Check constraints on UPDATE
--
UPDATE INSERT_TBL SET x = NULL WHERE x = 5;
UPDATE INSERT_TBL SET x = 6 WHERE x = 6;
UPDATE INSERT_TBL SET x = -z, z = -x;
UPDATE INSERT_TBL SET x = z, z = x;
ERROR:  new row for relation "insert_tbl" violates check constraint "insert_con"
SELECT * FROM INSERT_TBL;
 x |       y       | z  
---+---------------+----
 4 | Y             | -4
   | try again     |   
 7 | try again     | -7
 5 | !check failed |   
 6 | try again     | -6
(5 rows)

-- DROP TABLE INSERT_TBL;
--
-- Check constraints on COPY FROM
--
CREATE TABLE COPY_TBL (x INT, y TEXT, z INT,
	CONSTRAINT COPY_CON
	CHECK (x > 3 AND y <> 'check failed' AND x < 7 ));
COPY COPY_TBL FROM '@abs_srcdir@/data/constro.data';
SELECT '' AS two, * FROM COPY_TBL;
 two | x |       y       | z 
-----+---+---------------+---
     | 4 | !check failed | 5
     | 6 | OK            | 4
(2 rows)

COPY COPY_TBL FROM '@abs_srcdir@/data/constrf.data';
ERROR:  new row for relation "copy_tbl" violates check constraint "copy_con"
CONTEXT:  COPY copy_tbl, line 2: "7	check failed	6"
SELECT * FROM COPY_TBL;
 x |       y       | z 
---+---------------+---
 4 | !check failed | 5
 6 | OK            | 4
(2 rows)

--
-- Primary keys
--
CREATE TABLE PRIMARY_TBL (i int PRIMARY KEY, t text);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "primary_tbl_pkey" for table "primary_tbl"
INSERT INTO PRIMARY_TBL VALUES (1, 'one');
INSERT INTO PRIMARY_TBL VALUES (2, 'two');
INSERT INTO PRIMARY_TBL VALUES (1, 'three');
ERROR:  duplicate key violates unique constraint "primary_tbl_pkey"
INSERT INTO PRIMARY_TBL VALUES (4, 'three');
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
ERROR:  null value in column "i" violates not-null constraint
SELECT '' AS four, * FROM PRIMARY_TBL;
 four | i |   t   
------+---+-------
      | 1 | one
      | 2 | two
      | 4 | three
      | 5 | one
(4 rows)

DROP TABLE PRIMARY_TBL;
CREATE TABLE PRIMARY_TBL (i int, t text,
	PRIMARY KEY(i,t));
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "primary_tbl_pkey" for table "primary_tbl"
INSERT INTO PRIMARY_TBL VALUES (1, 'one');
INSERT INTO PRIMARY_TBL VALUES (2, 'two');
INSERT INTO PRIMARY_TBL VALUES (1, 'three');
INSERT INTO PRIMARY_TBL VALUES (4, 'three');
INSERT INTO PRIMARY_TBL VALUES (5, 'one');
INSERT INTO PRIMARY_TBL (t) VALUES ('six');
ERROR:  null value in column "i" violates not-null constraint
SELECT '' AS three, * FROM PRIMARY_TBL;
 three | i |   t   
-------+---+-------
       | 1 | one
       | 2 | two
       | 1 | three
       | 4 | three
       | 5 | one
(5 rows)

DROP TABLE PRIMARY_TBL;
--
-- Unique keys
--
CREATE TABLE UNIQUE_TBL (i int UNIQUE, t text);
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "unique_tbl_i_key" for table "unique_tbl"
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
INSERT INTO UNIQUE_TBL VALUES (2, 'two');
INSERT INTO UNIQUE_TBL VALUES (1, 'three');
ERROR:  duplicate key violates unique constraint "unique_tbl_i_key"
INSERT INTO UNIQUE_TBL VALUES (4, 'four');
INSERT INTO UNIQUE_TBL VALUES (5, 'one');
INSERT INTO UNIQUE_TBL (t) VALUES ('six');
INSERT INTO UNIQUE_TBL (t) VALUES ('seven');
SELECT '' AS five, * FROM UNIQUE_TBL;
 five | i |   t   
------+---+-------
      | 1 | one
      | 2 | two
      | 4 | four
      | 5 | one
      |   | six
      |   | seven
(6 rows)

DROP TABLE UNIQUE_TBL;
CREATE TABLE UNIQUE_TBL (i int, t text,
	UNIQUE(i,t));
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "unique_tbl_i_key" for table "unique_tbl"
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
INSERT INTO UNIQUE_TBL VALUES (2, 'two');
INSERT INTO UNIQUE_TBL VALUES (1, 'three');
INSERT INTO UNIQUE_TBL VALUES (1, 'one');
ERROR:  duplicate key violates unique constraint "unique_tbl_i_key"
INSERT INTO UNIQUE_TBL VALUES (5, 'one');
INSERT INTO UNIQUE_TBL (t) VALUES ('six');
SELECT '' AS five, * FROM UNIQUE_TBL;
 five | i |   t   
------+---+-------
      | 1 | one
      | 2 | two
      | 1 | three
      | 5 | one
      |   | six
(5 rows)

DROP TABLE UNIQUE_TBL;
