create_table.sgml 33.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
<REFENTRY ID="SQL-CREATETABLE-1">
 <REFMETA>
  <REFENTRYTITLE>
   CREATE TABLE
  </REFENTRYTITLE>
  <REFMISCINFO>SQL - Language Statements</REFMISCINFO>
 </REFMETA>
 <REFNAMEDIV>
  <REFNAME>
   CREATE TABLE
  </REFNAME>
  <REFPURPOSE>
   Creates a new table.
  </REFPURPOSE>
  
 <REFSYNOPSISDIV>
  <REFSYNOPSISDIVINFO>
   <DATE>1998-04-15</DATE>
  </REFSYNOPSISDIVINFO>
  <SYNOPSIS>
   CREATE TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> (
        <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">type</REPLACEABLE> [DEFAULT <REPLACEABLE CLASS="PARAMETER">value</REPLACEABLE>] [CONSTRAINT <REPLACEABLE CLASS="PARAMETER">column_constraint</REPLACEABLE>] [, ...]
        [, <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> ...]
        [, CONSTRAINT <REPLACEABLE CLASS="PARAMETER">table_constraint</REPLACEABLE>]
        ) [INHERITS ( <REPLACEABLE CLASS="PARAMETER">inherited_table</REPLACEABLE> [, ...] )]
  </SYNOPSIS>
  
  <REFSECT2 ID="R2-SQL-CREATETABLE-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    Inputs
   </TITLE>
   <PARA>
   </PARA>
   
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       The name of a new table to be created.
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
    
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       The name of a column.
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
    
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">type</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       The type of the column.
       (Refer to the <ProductName>Postgres</ProductName> User's Guide for
       further information about data types).
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
    
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">value</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       A default value for a column.
       See the DEFAULT clause for more information.
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
    
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">inherited_table</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       The optional INHERITS clause specifies a collection of table
       names from which this table automatically inherits all fields.
       If any inherited field name appears more than once, PostgreSQL
       reports an error. PostgreSQL automatically allows the created
       table to inherit functions on tables above it in the inheritance
       hierarchy. Inheritance of functions is done according
       to the conventions of the Common Lisp Object System (CLOS).
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
    
    <VARLISTENTRY>
     <TERM>
      <REPLACEABLE CLASS="PARAMETER">column_constraint</REPLACEABLE>
      <REPLACEABLE CLASS="PARAMETER">table_constraint</REPLACEABLE>
     </TERM>
     <LISTITEM>
      <PARA>
       The optional CONSTRAINT clause specifies a list of integrity 
       constraints which new or updated entries must satisfy for
       an insert or update operation to succeed. Each constraint
       must evaluate to a boolean expression. Multiple columns
       may be referenced within a single constraint.
       See CONSTRAINT clause for more information.
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST>
   
   
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-CREATETABLE-2">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    Outputs
   </TITLE>
   <PARA>
   </PARA>
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
      <ReturnValue>status</ReturnValue>
     </TERM>
     <LISTITEM>
      <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>CREATE</ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   Message returned if table is successfully created.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
	
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>ERROR</ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   Message returned if table creation failed.
	   This is usually accompanied by some descriptive text, such as:
	   <ProgramListing>
   amcreate: "<replaceable class="parameter">table</replaceable>" relation already exists
	   </ProgramListing>
	   which occurs at runtime, if the table specified already exists
	   in the database.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </VARIABLELIST>
      </PARA>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST>
  </REFSECT2>
 </REFSYNOPSISDIV>
 
 <REFSECT1 ID="R1-SQL-CREATETABLE-1">
  <REFSECT1INFO>
   <DATE>1998-04-15</DATE>
  </REFSECT1INFO>
  <TITLE>
   Description
  </TITLE>
  <PARA>
   CREATE TABLE will enter a new table into the current data
   base. The table will be "owned" by the user issuing the
   command.
   
  <PARA>
   The new table is created as a heap with no initial data.
   A table can have no more than 1600 columns (realistically,
   this is limited by the fact that tuple sizes must
   be less than 8192 bytes), but this limit may be configured
   lower at some sites. A table cannot have the same name as
   a system catalog table.
  </PARA>
  
  <REFSECT2 ID="R2-SQL-DEFAULTCLAUSE-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    DEFAULT clause
   </TITLE>
   <PARA>
    <SYNOPSIS>
     DEFAULT <REPLACEABLE CLASS="PARAMETER">value</REPLACEABLE>
    </SYNOPSIS>
   <PARA>
    The DEFAULT clause assigns a default data value to a column.
   </PARA>
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
     </TERM>
     <LISTITEM>
      <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue><replaceable class="parameter">value</replaceable></ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   The possible values for expression are:
	   <itemizedlist>
	    <listitem>
	     <simpara>
	      a literal value
	     </simpara>
	    </listitem>
	    <listitem>
	     <simpara>
	      a user function
	     </simpara>
	    </listitem>
	    <listitem>
	     <simpara>
	      a niladic function
	     </simpara>
	    </listitem>
	   </itemizedlist>
	  </para>
	 </listitem>
	</VARLISTENTRY>
	
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>ERROR:  DEFAULT: type mismatched</ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   if data type of default value doesn't match the
	   column definition's data type.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </variablelist>
      <PARA>
       The DEFAULT clause assigns a default data value to a column
       (via a column definition in the CREATE TABLE statement). 
       The data type of a default value must match the column definition's
       data type.
      </PARA>
      <PARA>
       An INSERT operation that includes a column without a specified
       default value will assign the NULL value to the column
       if no explicit data value is provided for it.
       Default <replaceable class="parameter">literal</replaceable> means
       that the default is the specified constant value.
       Default <replaceable class="parameter">niladic-function</replaceable>
       or <replaceable class="parameter">user-function</replaceable> means
       that the default
       is the value of the specified function at the time of the INSERT.
      </PARA>
      <PARA>
       There are two types of niladic functions:<variablelist>
	<varlistentry>
	 <term>niladic USER</term>
	 <listitem>
	  <variablelist>
	   <varlistentry>
	    <term>CURRENT_USER / USER</term>
	    <listitem>
	     <simpara>See CURRENT_USER function</simpara>
	    </listitem>
	   </varlistentry>
	   <varlistentry>
	    <term>SESSION_USER</term>
	    <listitem>
	     <simpara>not yet supported</simpara>
	    </listitem>
	   </varlistentry>
	   <varlistentry>
	    <term>SYSTEM_USER</term>
	    <listitem>
	     <simpara>not yet supported</simpara>
	    </listitem>
	   </varlistentry>
	  </variablelist>
	 </listitem>
	</varlistentry>
	<varlistentry>
	 <term>niladic datetime</term>
	 <listitem>
	  <variablelist>
	   <varlistentry>
	    <term> CURRENT_DATE</term>
	    <listitem>
	     <simpara>See CURRENT_DATE function</simpara>
	    </listitem>
	   </varlistentry>
	   <varlistentry>
	    <term>CURRENT_TIME</term>
	    <listitem>
	     <simpara>See CURRENT_TIME function</simpara>
	    </listitem>
	   </varlistentry>
	   <varlistentry>
	    <term>CURRENT_TIMESTAMP</term>
	    <listitem>
	     <simpara>See CURRENT_TIMESTAMP function</simpara>
	    </listitem>
	   </varlistentry>
	  </variablelist>
	 </listitem>
	</varlistentry>
       </variablelist>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST>
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-NOTNULL-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    NOT NULL constraint
   </TITLE>
   <SYNOPSIS>
    [ CONSTRAINT <replaceable class="parameter">name</replaceable> ] NOT NULL 
   </SYNOPSIS>
   <PARA>
    The NOT NULL constraint specifies a rule that a column may
    contain only non-null values. 
   </PARA>
   <PARA>
    The NOT NULL constraint is a column constraint. 
   </PARA>
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
     </TERM>
     <LISTITEM>
      <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue><replaceable class="parameter">name</replaceable></ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   The optional name of a constraint.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </variablelist>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST>
  
  <REFSECT3 ID="R3-SQL-NOTNULL-1">
   <REFSECT3INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT3INFO>
   <TITLE>
    Outputs
   </TITLE>
   <PARA>
   </PARA>
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
     </TERM>
     <LISTITEM>
      <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>ERROR:  ExecAppend: Fail to add null value in not
	   null attribute "<replaceable class="parameter">column</replaceable>".</ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   This error occurs at runtime if one tries to insert a null value
	   into a column which has a NOT NULL constraint.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </variablelist>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST> 
  </REFSECT3>
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-UNIQUECLAUSE-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    UNIQUE constraint
   </TITLE>
   <para>
    Table Constraint definition
   </para>
   <synopsis>
    [ CONSTRAINT <replaceable class="parameter">name</replaceable> ] UNIQUE ( <replaceable class="parameter">column</replaceable> [, ...] )
   </SYNOPSIS>
   <para>
    Column Constraint definition
   </para>
   <synopsis>
    [ CONSTRAINT <replaceable class="parameter">name</replaceable> ] UNIQUE
   </SYNOPSIS>
   <refsect3>
    <title>Parameters</title>
    <variablelist>
     <varlistentry>
      <term>
       <replaceable class="parameter">name</replaceable>
      </term>
      <listitem>
       <para>
	An arbitrary name given to a constraint.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term>
       <replaceable class="parameter">column</replaceable>
      </term>
      <listitem>
       <para>
	A name of a column in a table.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </refsect3>
   <refsect3>
    <title>Outputs</title>
    <PARA>
    </PARA>
    <VARIABLELIST>
     <VARLISTENTRY>
      <TERM>
      </TERM>
      <LISTITEM>
       <PARA>
	<VARIABLELIST>
	 <VARLISTENTRY>
	  <TERM>
	   ERROR: Cannot insert a duplicate key into a unique index.
	  </term>
	  <listitem>
	   <para>
	    This error occurs at runtime if one tries to insert a
	    duplicate value into a column.
	   </para>
	  </listitem>
	 </varlistentry>
	</variablelist></para>
      </listitem>
     </varlistentry>
    </variablelist>
   </refsect3>
   <refsect3>
    <title>Description</title>
    <PARA>
     The UNIQUE constraint specifies a rule that a group of one or
     more distinct columns of a table may contain only unique values.
    </para>
    <para>
     The column definitions of the specified columns do not have to
     include a NOT NULL constraint to be included in a UNIQUE
     constraint.  Having more than one null value in a column without a
     NOT NULL constraint, does not violate a UNIQUE constraint.
    </PARA>
    <PARA>
     Each UNIQUE constraint must name a set of columns that is
     different from the set of columns named by any other UNIQUE or
     PRIMARY KEY constraint defined for the Table.
    </PARA>
    <Note>
     <Para>
      PostgreSQL automatically creates a unique index for each UNIQUE
      constraint, to assure
      data integrity. See CREATE INDEX for more information.
     </Para>
    </Note>
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-CONSTRAINT-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    CONSTRAINT clause
   </TITLE>
   <para>
    Table constraint definition
   </para>
   <SYNOPSIS>
    [ CONSTRAINT name ] 
    { PRIMARY KEY constraint | 
    UNIQUE constraint | 
    CHECK constraint }  
   </SYNOPSIS>
   <PARA>
    Column constraint definition 
   </PARA>
   <SYNOPSIS>
    [ CONSTRAINT name ] 
    { NOT NULL constraint |
    PRIMARY KEY constraint | 
    UNIQUE constraint | 
    CHECK constraint }  
   </SYNOPSIS>
   <PARA>
   </PARA>
   <VARIABLELIST>
    <VARLISTENTRY>
     <TERM>
     </TERM>
     <LISTITEM>
      <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>
	   <replaceable class="parameter">name</replaceable>
	  </ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
          An arbitrary name given to an integrity constraint.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>
	   <replaceable class="parameter">constraint</replaceable>
	  </ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   The definition of the constraint.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </VARIABLELIST>
     </LISTITEM>
    </VARLISTENTRY>
   </VARIABLELIST>
   <para>
    A Constraint is a named rule: a SQL object which helps define
    valid sets of values by putting limits on the results of INSERT,
    UPDATE or DELETE operations performed on a Base table. 
   </para>
   <para>
    There are two ways to define integrity constraints:
    Table constraint and Column constraint.
   </para>
   <para>
    A Table Constraint is an integrity Constraint defined on one or
    more Columns of a Base table. The four variations of "Table
    Constraint" are:
    <simplelist columns="1">
     <member>PRIMARY KEY</member>
     <member>FOREIGN KEY</member>
     <member>UNIQUE</member>
     <member>CHECK</member>
    </simplelist>
   </para>
   <para>
    A column constraint is an integrity constraint defined as part
    of a column definition, and logically becomes a table
    constraint as soon as it is created. The column
    constraints available are:
    <simplelist columns="1">
     <member>PRIMARY KEY</member>
     <member>REFERENCES</member>
     <member>UNIQUE</member>
     <member>CHECK</member>
     <member>NOT NULL</member>
    </simplelist></para>
   <note>
    <para>
     PostgreSQL does not yet (at release 6.3.2) support the FOREIGN KEY or
     REFERENCES integrity constraints, although the parser will accept them.
     Foreign keys may be partially emulated by triggers (See CREATE TRIGGER
     statement)
    </para>
   </note>
   <note>
    <para>
     PostgreSQL does not yet support either DOMAINs or ASSERTIONs.
    </para>
   </note>
   
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-CHECK-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <title>The CHECK constraint</title>
  <SYNOPSIS>
   [ CONSTRAINT name ] CHECK ( condition [, ...] ) 
  </SYNOPSIS>
   <refsect3 id="R3-SQL-CHECK-1">
    <title>Inputs</title>
   <PARA>
       <VARIABLELIST>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue><replaceable class="parameter">name</replaceable></ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   An arbitrary name given to a constraint.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
	<VARLISTENTRY>
	 <TERM>
	  <ReturnValue>condition</ReturnValue>
	 </TERM>
	 <LISTITEM>
	  <PARA>
	   Any valid conditional expression.
	  </PARA>
	 </LISTITEM>
	</VARLISTENTRY>
       </variablelist>
  </REFSECT3>

  <REFSECT3 ID="R3-SQL-CHECK-2">
   <REFSECT3INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT3INFO>
    <TITLE>
     Outputs
    </TITLE>
    <PARA>
     <VARIABLELIST>
      <VARLISTENTRY>
       <TERM>
	<ReturnValue>
	 ERROR:  ExecAppend: rejected due to CHECK constraint
	 "<replaceable class="parameter">table_column</replaceable>".
	</ReturnValue>
       </TERM>
       <LISTITEM>
	<PARA>
	 This error occurs at runtime if one tries to insert an illegal
	 value into a column subject to a CHECK constraint.
	</PARA>
       </LISTITEM>
      </VARLISTENTRY>
     </variablelist>
   </REFSECT3>
   <refsect3>
    <title>Description</title>
    <para>
     The CHECK constraint specifies a rule that a group of one or
     more columns of a table may contain only those values allowed by
     the rule.
    </para>
    <PARA>
     The CHECK constraint is either a table constraint or a column
     constraint.
    </PARA>
    <PARA>
     PostgreSQL automatically creates an unique index to assure
     data integrity. (See CREATE INDEX statement)
    </PARA>
    <PARA>
     The SQL92 CHECK column constraints can only be defined on, and
     refer to, one column of the table. PostgreSQL does not have
     this restriction.
    </PARA>
   </refsect3>
   <refsect3>
    <title>BUGS in CHECK constraint</title>
    <PARA>
     The following CHECK constraints gives a parse error like:
     <programlisting>
      ERROR:  parser: parse error at or near "opname":
     </programlisting>
     <simplelist columns="1">
      <member>CHECK ( <replaceable class="parameter">column</replaceable> BETWEEN 'A' AND 'Z' )</member>
      <member>CHECK ( <replaceable class="parameter">column</replaceable> IN ('A','Z'))</member>
      <member>CHECK ( <replaceable class="parameter">column</replaceable> NOT LIKE 'A%')</member>
     </simplelist>
    </para>
   </refsect3>
  </REFSECT2>
  
  <REFSECT2 ID="R2-SQL-PRIMARYKEY-1">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    PRIMARY KEY clause
   </TITLE>
   <PARA>
   Table constraint definition
   </PARA>
   <SYNOPSIS>
    [ CONSTRAINT <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE> ] PRIMARY KEY ( <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [, ...] ) 
   </SYNOPSIS>
   <PARA>
    Column constraint definition
   </PARA> 
   <SYNOPSIS>
    [ CONSTRAINT <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE> ] PRIMARY KEY 
   </SYNOPSIS>
   
   <refsect3>
    <title>Parameters</title>
    <PARA>
     <VARIABLELIST>
      <VARLISTENTRY>
       <TERM>
	<ReturnValue><REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE></ReturnValue>
       </TERM>
       <LISTITEM>
	<PARA>
	 An arbitrary name for the constraint.
	</PARA>
       </LISTITEM>
      </VARLISTENTRY>
      <VARLISTENTRY>
       <TERM>
	<ReturnValue><REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE></ReturnValue>
       </TERM>
       <LISTITEM>
	<PARA>
	 The name of a column in the table.
	</PARA>
       </LISTITEM>
      </VARLISTENTRY>
     </VARIABLELIST>
    </para>
   </refsect3>
   <refsect3>
    <title>Outputs</title>
    <variablelist>
     <varlistentry>
      <term>ERROR: Cannot insert a duplicate key into a unique index.</term>
      <listitem>
       <para>
	This occurs at run-time if one tries to insert a duplicate value into
	a column subject to a PRIMARY KEY constraint.
      </PARA>
      </listitem>
     </varlistentry>
    </variablelist>
   </refsect3>
   <refsect3>
    <title>Description</title>
    <PARA>
     The PRIMARY KEY constraint specifies a rule that a group of one
     or more distinct columns of a table may contain only unique,
     (not duplicates), non-null values. The column definitions of
     the specified columns do not have to include a NOT NULL
     constraint to be included in a PRIMARY KEY constraint. 
    </PARA>
    <PARA>
     A table's set of valid values may be constrained by only one
     PRIMARY KEY constraint at a time. 
    </PARA>
    <PARA>
     The PRIMARY KEY constraint must name a set of columns that is
     different from the set of columns named by any UNIQUE constraint
     defined for the same table. 
    </PARA>
   </REFSECT3>
   
   <REFSECT3 ID="R3-SQL-PRIMARYKEY-3">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     Notes
    </TITLE>
    <PARA>
     PostgreSQL automatically creates an unique index to assure
     data integrity. (See CREATE INDEX statement)
    </PARA>
   </refsect3>
   
  </REFSECT2>
 </refsect1>
 
 <REFSECT1 ID="R1-SQL-CREATETABLE-2">
  <TITLE>
   Usage
  </TITLE>
  <PARA>
   Create table films and table distributors
  </PARA>
  <ProgramListing>
   CREATE TABLE films (
          code      CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
          title     CHARACTER VARYING(40) NOT NULL,
          did       DECIMAL(3) NOT NULL,
          date_prod DATE,
          kind      CHAR(10),
          len       INTERVAL HOUR TO MINUTE
          );
  </ProgramListing>

  <ProgramListing>
   CREATE TABLE distributors (
          did      DECIMAL(03) PRIMARY KEY DEFAULT NEXTVAL('serial'),
          name     VARCHAR(40) NOT NULL CHECK (name &lt;&gt; '')
          );
  </ProgramListing>

  <PARA>
   Create a table with a 2-dimensional array
  </PARA>
  <ProgramListing>
   CREATE TABLE array (
          vector INT[][]
          );
  </ProgramListing>
   
  <PARA>
   Define two NOT NULL column constraints on the table distributors
  </PARA>
  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(3) CONSTRAINT no_null NOT NULL,
        name     VARCHAR(40) NOT NULL
        );
  </ProgramListing>

  <PARA>
   Define a UNIQUE table constraint for the table films.
   UNIQUE table constraints can be defined on one or more
   columns of the table
  </PARA>
  <ProgramListing>
   CREATE TABLE films (
       code      CHAR(5),
       title     VARCHAR(40),
       did       DECIMAL(03),
       date_prod DATE,
       kind      CHAR(10),
       len       INTERVAL HOUR TO MINUTE,
       CONSTRAINT production UNIQUE(date_prod)
       );
  </ProgramListing>
  
  <PARA>
   Defines a UNIQUE column constraint for the table distributors.
   UNIQUE column constraints can only be defined on one column
   of the table (the following two examples are equivalents).
  </PARA>
  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(03),
        name     VARCHAR(40) UNIQUE,
        UNIQUE(name)
        );
  </ProgramListing>

  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(3),
        name     VARCHAR(40) UNIQUE
        );
  </ProgramListing>

  <PARA>
   Define a CHECK column constraint.
  </PARA>
  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(3) CHECK (did > 100),
        name     VARCHAR(40)
        );
  </ProgramListing>
  
  <PARA>
   Define a CHECK table constraint
  </PARA>
  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(3),
        name     VARCHAR(40)
        CONSTRAINT con1 CHECK (did > 100 AND name > '')
        );
  </ProgramListing>
    
  <PARA>
   Define a PRIMARY KEY table constraint for the table films.
   PRIMARY KEY table constraints can be defined on one or more
   columns of the table
  </PARA>
  <ProgramListing>
   CREATE TABLE films (
       code      CHAR(05),
       title     VARCHAR(40),
       did       DECIMAL(03),
       date_prod DATE,
       kind      CHAR(10),
       len       INTERVAL HOUR TO MINUTE,
       CONSTRAINT code_title PRIMARY KEY(code,title)
       );
  </ProgramListing>
  
  <PARA>
   Defines a PRIMARY KEY column constraint for table distributors.
   PRIMARY KEY column constraints can only be defined on one column
   of the table (the following two examples are equivalents)
  </PARA>
  <ProgramListing>
   CREATE TABLE distributors (
        did      DECIMAL(03),
        name     CHAR VARYING(40),
        PRIMARY KEY(did)
        ); 

   CREATE TABLE distributors (
        did      DECIMAL(03) PRIMARY KEY,
        name     VARCHAR(40)
        );
  </ProgramListing>
  <para>
   To assign a sequence as the default for the column did,
   and a literal to the column name
  </PARA>
  
  <ProgramListing>
       CREATE TABLE distributors (
              did      DECIMAL(3) DEFAULT NEXTVAL('serial'),
              name     VARCHAR(40) DEFAULT 'luso films'
              );
  </ProgramListing>
  
  <REFSECT2 ID="R2-SQL-CREATETABLE-3">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    Notes
   </TITLE>
   <PARA>
    CREATE TABLE/INHERITS is a PostgreSQL language extension.
   </PARA>
  </refsect2>
  
 </REFSECT1>
 
 <REFSECT1 ID="R1-SQL-CREATETABLE-3">
  <TITLE>
   Compatibility
  </TITLE>
  <PARA>
  </PARA>
  
  <REFSECT2 ID="R2-SQL-CREATETABLE-4">
   <REFSECT2INFO>
    <DATE>1998-04-15</DATE>
   </REFSECT2INFO>
   <TITLE>
    SQL92
   </TITLE>
   <PARA>
    In addition to normal CREATE TABLE, SQL92 also supports a
    CREATE TEMPORARY TABLE statement.
   </PARA>
   <synopsis>
   CREATE [ {GLOBAL | LOCAL} ] TEMPORARY TABLE table (
        column type [DEFAULT value] [CONSTRAINT column_constraint] [, ...] )
        [CONSTRAINT table_constraint ]
        [ ON COMMIT {DELETE | PRESERVE} ROWS ] 
   </synopsis>
   <para>
    For temporary tables, the CREATE TEMPORARY TABLE statement
    names a new table and defines the table's columns and
    constraints. 
   </para>
   <para>
    The optional ON COMMIT clause of CREATE TEMPORARY TABLE
    specifies whether or not the temporary table should be emptied of
    rows whenever COMMIT is executed. If the ON COMMIT clause is
    omitted, the default option, ON COMMIT DELETE ROWS, is assumed. 
   </para>
   <para>
    To create a temporary table:
   </para>
   <programlisting>
      CREATE TEMPORARY TABLE actors (
             id      DECIMAL(03),
             name    VARCHAR(40),
             CONSTRAINT actor_id CHECK (id &lt; 150)
             ) ON COMMIT DELETE ROWS
   </programlisting> 
   <para>
    TIP: In the current release of PostgreSQL (6.3.2), to create a temporary
    table you must create and drop the table by explicit commands.
   </para>
   
   <REFSECT3 ID="R3-SQL-UNIQUECLAUSE-1">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     UNIQUE clause
    </TITLE>
    <PARA>
     SQL92 specifies some additional capabilities for UNIQUE:
     Table Constraint definition
    </PARA>
    <synopsis>
      [ CONSTRAINT name ]
      UNIQUE ( column [, ...] )
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     Column Constraint definition
    </para>
    <synopsis>
      [ CONSTRAINT name ]
      UNIQUE
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
   </refsect3>
   
   <REFSECT3 ID="R3-SQL-NOTNULL-4">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     NOT NULL clause
    </TITLE>
    <PARA>
     
     SQL92 specifies some additional capabilities for NOT NULL:
    </PARA>
    <synopsis>
      [ CONSTRAINT name ] NOT NULL 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
   </REFSECT3>
   
   <REFSECT3 ID="R3-SQL-DEFAULTCLAUSE-1">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     DEFAULT clause
    </TITLE>
    <PARA>
     SQL92 specifies some additional capabilities for the DEFAULT clause.
     A DEFAULT clause is used to set the default value for a column
     or a domain.
    </para>
    <synopsis>
     DEFAULT <replaceable class="parameter">literal</replaceable> |
              niladic USER function |
              niladic datetime function |
              NULL
    </synopsis>
   </refsect3>
   
   <REFSECT3 ID="R3-SQL-CONSTRAINT-3">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     CONSTRAINT clause
    </TITLE>
    <PARA>
     SQL92 specifies some additional capabilities for CONSTRAINTs,
     it also defines assertions and domain constraints.
    </PARA>
    <PARA>
     An assertion is a special type of integrity constraint and share
     the same namespace as other constraints.
     However, an assertion is not necessarily dependent on one
     particular base table as constraints are, so SQL-92 provides the
     CREATE ASSERTION statement as an alternate method for defining a
     constraint:
    </para>
    <synopsis>
     CREATE ASSERTION name CHECK ( condition )
    </synopsis>
    
    <PARA>
     Domain constraints are defined by CREATE DOMAIN or ALTER DOMAIN
     statements:
    </para>
    <PARA>
     Domain constraint: 
    </para>
    <synopsis>
      [ CONSTRAINT name ] 
          CHECK constraint 
          [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
          [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     Table constraint definition:
    </para>
    <synopsis>
      [ CONSTRAINT name ] 
        { PRIMARY KEY constraint | 
          FOREIGN KEY constraint | 
          UNIQUE constraint | 
          CHECK constraint }
          [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
          [ [ NOT ] DEFERRABLE ]</synopsis>
    <para>
     Column constraint definition:
    </para>
    <synopsis>
     [ CONSTRAINT name ] 
        { NOT NULL constraint |
          PRIMARY KEY constraint | 
          FOREIGN KEY constraint | 
          UNIQUE constraint | 
          CHECK constraint }  
          [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
          [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     A CONSTRAINT definition may contain one deferment attribute
     clause and/or one initial constraint mode clause, in any order.
     <variablelist>
      <varlistentry>
       <term>NOT DEFERRABLE</term>
       <listitem>
	<para>
	 means that the Constraint must be checked for
	 violation of its rule after the execution of every SQL statement.
	</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term>DEFERRABLE</term>
       <listitem>
	<para>
	 means that checking of the Constraint may be deferred
	 until some later time, but no later than the end of the current
	 transaction.
	</para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
    <para>
     The constraint mode for every Constraint always has an initial
     default value which is set for that Constraint at the beginning
     of a transaction.
     <variablelist>
      <varlistentry>
       <term>INITIALLY IMMEDIATE</term>
       <listitem>
	<para>
	 means that, as of the start of the transaction,
	 the Constraint must be checked for violation of its rule after the
	 execution of every SQL statement.
	</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term>INITIALLY DEFERRED</term>
       <listitem>
	<para>
	 means that, as of the start of the transaction,
	 checking of the Constraint may be deferred until some later time,
	 but no later than the end of the current transaction.</para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </refsect3>
   
   
   <REFSECT3 ID="R3-SQL-CHECK-4">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     CHECK clause
    </TITLE>
    <PARA>
     SQL92 specifies some additional capabilities for CHECK:
    </PARA>
    <para>
     A CHECK constraint is either a table constraint, a column
     constraint or a domain constraint.
    </para> 
    <para>
     table constraint definition:
    </para>
    <synopsis>
     [ CONSTRAINT name ] 
      CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     column constraint definition:
    </para>
    <synopsis>
      [ CONSTRAINT name ] 
      CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     domain constraint definition: 
    </para>
    <synopsis>
     [ CONSTRAINT name ] 
      CHECK ( VALUE condition ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     CHECK domain constraints can be defined in either
     a CREATE DOMAIN statement or an ALTER DOMAIN statement:
    </para>
    <programlisting>
     CREATE DOMAIN duration AS SMALLINT 
        CONSTRAINT minutes CHECK (VALUE IN (90,120,180,240)); 

     ALTER DOMAIN cities 
        ADD CONSTRAINT new_city CHECK (VALUE LIKE 'L%');
    </programlisting>
   </refsect3>
   
   
   <REFSECT3 ID="R3-SQL-PRIMARYKEY-1">
    <REFSECT3INFO>
     <DATE>1998-04-15</DATE>
    </REFSECT3INFO>
    <TITLE>
     PRIMARY KEY clause
    </TITLE>
    <PARA>
     SQL92 specifies some additional capabilities for PRIMARY KEY:
    </para>
    <PARA>
     Table Constraint definition:
    </para>
    <synopsis>
      [ CONSTRAINT name ] 
      PRIMARY KEY ( column [, ...] ) 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
    <para>
     Column Constraint definition: 
    </para>
    <synopsis>
      [ CONSTRAINT name ] 
      PRIMARY KEY 
      [ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
      [ [ NOT ] DEFERRABLE ]
    </synopsis>
   </refsect3>
  </refsect2>
 </refsect1>
</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
1304
-->