1
- <!-- $Header: /cvsroot/pgsql/doc/src/sgml/ddl.sgml,v 1.2 2002/08/28 20:17:44 momjian Exp $ -->
1
+ <!-- $Header: /cvsroot/pgsql/doc/src/sgml/ddl.sgml,v 1.3 2002/09/05 21:32:23 petere Exp $ -->
2
2
3
3
<chapter id="ddl">
4
4
<title>Data Definition</title>
@@ -151,7 +151,7 @@ DROP TABLE products;
151
151
columns will be filled with their respective default values. A
152
152
data manipulation command can also request explicitly that a column
153
153
be set to its default value, without knowing what this value is.
154
- (Details about data manipulation commands are in the next chapter .)
154
+ (Details about data manipulation commands are in <xref linkend="dml"> .)
155
155
</para>
156
156
157
157
<para>
@@ -263,7 +263,7 @@ CREATE TABLE products (
263
263
The first two constraints should look familiar. The third one
264
264
uses a new syntax. It is not attached to a particular column,
265
265
instead it appears as a separate item in the comma-separated
266
- column list. In general, column definitions and constraint
266
+ column list. Column definitions and these constraint
267
267
definitions can be listed in mixed order.
268
268
</para>
269
269
@@ -299,8 +299,10 @@ CREATE TABLE products (
299
299
300
300
<para>
301
301
It should be noted that a check constraint is satisfied if the
302
- check expression evaluates to true or the null value. To ensure
303
- that a column does not contain null values, the not-null
302
+ check expression evaluates to true or the null value. Since most
303
+ expressions will evaluate to the null value if one operand is null
304
+ they will not prevent null values in the constrained columns. To
305
+ ensure that a column does not contain null values, the not-null
304
306
constraint described in the next section should be used.
305
307
</para>
306
308
</sect2>
@@ -322,12 +324,13 @@ CREATE TABLE products (
322
324
323
325
<para>
324
326
A not-null constraint is always written as a column constraint. A
325
- not-null constraint is equivalent to creating a check constraint
326
- <literal>CHECK (<replaceable>column_name</replaceable> IS NOT
327
- NULL)</literal>, but in <productname>PostgreSQL</productname>
328
- creating an explicit not-null constraint is more efficient. The
329
- drawback is that you cannot give explicit names to not-null
330
- constraints created that way.
327
+ not-null constraint is functionally equivalent to creating a check
328
+ constraint <literal>CHECK (<replaceable>column_name</replaceable>
329
+ IS NOT NULL)</literal>, but in
330
+ <productname>PostgreSQL</productname> creating an explicit
331
+ not-null constraint is more efficient. The drawback is that you
332
+ cannot give explicit names to not-null constraints created that
333
+ way.
331
334
</para>
332
335
333
336
<para>
@@ -564,8 +567,8 @@ CREATE TABLE t1 (
564
567
<emphasis>FOREIGN KEY (b, c) REFERENCES other_table (c1, c2)</emphasis>
565
568
);
566
569
</programlisting>
567
- Of course, the number and type of constrained columns needs to
568
- match the number and type of referenced columns.
570
+ Of course, the number and type of the constrained columns needs to
571
+ match the number and type of the referenced columns.
569
572
</para>
570
573
571
574
<para>
@@ -847,13 +850,14 @@ SET SQL_Inheritance TO OFF;
847
850
<title>Modifying Tables</title>
848
851
849
852
<para>
850
- When you create a table and you realize that you made a mistake,
851
- then you can drop the table and create it again. But this is not a
852
- convenient option if the table is already filled with data, or if
853
- the table is referenced by other database objects (for instance a
854
- foreign key constraint). Therefore
855
- <productname>PostgreSQL</productname> provides a family of commands
856
- to make modifications on existing tables.
853
+ When you create a table and you realize that you made a mistake, or
854
+ the requirements of the application changed, then you can drop the
855
+ table and create it again. But this is not a convenient option if
856
+ the table is already filled with data, or if the table is
857
+ referenced by other database objects (for instance a foreign key
858
+ constraint). Therefore <productname>PostgreSQL</productname>
859
+ provides a family of commands to make modifications on existing
860
+ tables.
857
861
</para>
858
862
859
863
<para>
@@ -862,6 +866,9 @@ SET SQL_Inheritance TO OFF;
862
866
<listitem>
863
867
<para>Add columns,</para>
864
868
</listitem>
869
+ <listitem>
870
+ <para>Remove a column,</para>
871
+ </listitem>
865
872
<listitem>
866
873
<para>Add constraints,</para>
867
874
</listitem>
@@ -879,22 +886,135 @@ SET SQL_Inheritance TO OFF;
879
886
</listitem>
880
887
</itemizedlist>
881
888
882
- In the current implementation you cannot
883
- <itemizedlist spacing="compact">
884
- <listitem>
885
- <para>Remove a column,</para>
886
- </listitem>
887
- <listitem>
888
- <para>Change the data type of a column.</para>
889
- </listitem>
890
- </itemizedlist>
891
- These may be possible in a future release.
889
+ All these actions are performed using the <literal>ALTER
890
+ TABLE</literal> command.
892
891
</para>
893
892
894
- <comment>
895
- OK, now explain how to do this. There's currently so much activity
896
- on <literal>ALTER TABLE</literal> that I'm holding off a bit.
897
- </comment>
893
+ <sect2>
894
+ <title>Adding a Column</title>
895
+
896
+ <para>
897
+ To add a column, use this command:
898
+ <programlisting>
899
+ ALTER TABLE products ADD COLUMN description text;
900
+ </programlisting>
901
+ The new column will initially be filled with null values in the
902
+ existing rows of the table.
903
+ </para>
904
+
905
+ <para>
906
+ You can also define a constraint on the column at the same time,
907
+ using the usual syntax:
908
+ <programlisting>
909
+ ALTER TABLE products ADD COLUMN description text CHECK (description <> '');
910
+ </programlisting>
911
+ A new column cannot have a not-null constraint since the column
912
+ initially has to contain null values. But you can add a not-null
913
+ constraint later. Also, you cannot define a default value on a
914
+ new column. According to the SQL standard, this would have to
915
+ fill the new columns in the existing rows with the default value,
916
+ which is not implemented yet. But you can adjust the column
917
+ default later on.
918
+ </para>
919
+ </sect2>
920
+
921
+ <sect2>
922
+ <title>Removing a Column</title>
923
+
924
+ <para>
925
+ To remove a column, use this command:
926
+ <programlisting>
927
+ ALTER TABLE products DROP COLUMN description;
928
+ </programlisting>
929
+ </para>
930
+ </sect2>
931
+
932
+ <sect2>
933
+ <title>Adding a Constraint</title>
934
+
935
+ <para>
936
+ To add a constraint, the table constraint syntax is used. For example:
937
+ <programlisting>
938
+ ALTER TABLE products ADD CHECK (name <> '');
939
+ ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
940
+ ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
941
+ </programlisting>
942
+ To add a not-null constraint, which cannot be written as a table
943
+ constraint, use this syntax:
944
+ <programlisting>
945
+ ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
946
+ </programlisting>
947
+ </para>
948
+
949
+ <para>
950
+ The constraint will be checked immediately, so the table data must
951
+ satisfy the constraint before it can be added.
952
+ </para>
953
+ </sect2>
954
+
955
+ <sect2>
956
+ <title>Removing a Constraint</title>
957
+
958
+ <para>
959
+ To remove a constraint you need to know its name. If you gave it
960
+ a name then that's easy. Otherwise the system assigned a
961
+ generated name, which you need to find out. The
962
+ <application>psql</application> command <literal>\d
963
+ <replaceable>tablename</replaceable></literal> can be helpful
964
+ here; other interfaces might also provide a way to inspect table
965
+ details. Then the command is:
966
+ <programlisting>
967
+ ALTER TABLE products DROP CONSTRAINT some_name;
968
+ </programlisting>
969
+ This works the same for all constraint types except not-null
970
+ constraints. To drop a not null constraint use
971
+ <programlisting>
972
+ ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
973
+ </programlisting>
974
+ (Recall that not-null constraints do not have names.)
975
+ </para>
976
+ </sect2>
977
+
978
+ <sect2>
979
+ <title>Changing the Default</title>
980
+
981
+ <para>
982
+ To set a new default for a column, use a command like this:
983
+ <programlisting>
984
+ ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
985
+ </programlisting>
986
+ To remove any default value, use
987
+ <programlisting>
988
+ ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
989
+ </programlisting>
990
+ This is equivalent to setting the default to null, at least in
991
+ PostgreSQL. As a consequence, it is not an error to drop a
992
+ default where one hadn't been defined, because the default is
993
+ implicitly the null value.
994
+ </para>
995
+ </sect2>
996
+
997
+ <sect2>
998
+ <title>Renaming a Column</title>
999
+
1000
+ <para>
1001
+ To rename a column:
1002
+ <programlisting>
1003
+ ALTER TABLE products RENAME COLUMN product_no TO product_number;
1004
+ </programlisting>
1005
+ </para>
1006
+ </sect2>
1007
+
1008
+ <sect2>
1009
+ <title>Renaming a Table</title>
1010
+
1011
+ <para>
1012
+ To rename a table:
1013
+ <programlisting>
1014
+ ALTER TABLE products RENAME TO items;
1015
+ </programlisting>
1016
+ </para>
1017
+ </sect2>
898
1018
</sect1>
899
1019
900
1020
<sect1 id="ddl-schemas">
@@ -990,10 +1110,10 @@ DROP TABLE products CASCADE;
990
1110
991
1111
<note>
992
1112
<para>
993
- Foreign Key constraint dependencies and SERIAL dependencies from
994
- <productname>PostgreSQL</productname> versions prior to 7.3 are
995
- <emphasis>not</emphasis> maintained or created during the upgrade
996
- process. However, all other dependency types are created successfully .
1113
+ Foreign key constraint dependencies and serial column dependencies
1114
+ from <productname>PostgreSQL</productname> versions prior to 7.3
1115
+ are <emphasis>not</emphasis> maintained or created during the
1116
+ upgrade process. All other dependency types survive the upgrade .
997
1117
</para>
998
1118
</note>
999
1119
</sect1>
0 commit comments