CREATE [[ GLOBAL | LOCAL ]{ TEMPORARY | TEMP }| UNLOGGED ] TABLE
[ IF NOT EXISTS ] table_name ([{ column_name data_type [ COMPRESSION compression_method ][ COLLATE collation ][ column_constraint [... ]]| table_constraint
| LIKE source_table [ like_option ... ]}[, ... ]])[ INHERITS ( parent_table [, ... ])][ PARTITION BY { RANGE | LIST | HASH }({ column_name |( expression )}[ COLLATE collation ][ opclass ][, ... ])][ USING method ][ WITH ( storage_parameter [= value][, ... ])| WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP }][ TABLESPACE tablespace_name ]
CREATE [[ GLOBAL | LOCAL ]{ TEMPORARY | TEMP }| UNLOGGED ] TABLE
[ IF NOT EXISTS ] table_name
OF type_name [({ column_name [ WITH OPTIONS ][ column_constraint [... ]]| table_constraint }[, ... ])][ PARTITION BY { RANGE | LIST | HASH }({ column_name |( expression )}[ COLLATE collation ][ opclass ][, ... ])][ USING method ][ WITH ( storage_parameter [= value][, ... ])| WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP }][ TABLESPACE tablespace_name ]
CREATE [[ GLOBAL | LOCAL ]{ TEMPORARY | TEMP }| UNLOGGED ] TABLE
[ IF NOT EXISTS ] table_name
PARTITION OF parent_table [({ column_name [ WITH OPTIONS ][ column_constraint [... ]]| table_constraint }[, ... ])]{ FOR VALUES partition_bound_spec | DEFAULT }[ PARTITION BY { RANGE | LIST | HASH }({ column_name |( expression )}[ COLLATE collation ][ opclass ][, ... ])][ USING method ][ WITH ( storage_parameter [= value][, ... ])| WITHOUT OIDS ][ ON COMMIT { PRESERVE ROWS | DELETE ROWS | DROP }][ TABLESPACE tablespace_name ]
where column_constraint is:
[ CONSTRAINT constraint_name ]{ NOT NULL |
NULL |
CHECK ( expression )[ NO INHERIT ]|
DEFAULT default_expr |
GENERATED ALWAYS AS ( generation_expr ) STORED |
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY
[( sequence_options )]|
UNIQUE index_parameters |
PRIMARY KEY index_parameters |
REFERENCES reftable [( refcolumn )][ MATCH FULL | MATCH
PARTIAL | MATCH SIMPLE ][ ON DELETE referential_action ][ ON UPDATE referential_action
]}[ DEFERRABLE | NOT DEFERRABLE ][ INITIALLY DEFERRED | INITIALLY
IMMEDIATE ]
and table_constraint is:
[ CONSTRAINT constraint_name ]{ CHECK ( expression )[ NO INHERIT ]|
UNIQUE ( column_name [, ... ]) index_parameters |
PRIMARY KEY ( column_name [, ... ]) index_parameters |
EXCLUDE [ USING index_method ]( exclude_element WITH operator
[, ... ]) index_parameters [ WHERE ( predicate )]|
FOREIGN KEY ( column_name [, ... ]) REFERENCES reftable
[( refcolumn [, ... ])][ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ][ ON
DELETE referential_action ][ ON UPDATE referential_action ]}[ DEFERRABLE | NOT DEFERRABLE ][ INITIALLY DEFERRED | INITIALLY
IMMEDIATE ]
and like_option is:
{ INCLUDING | EXCLUDING }{ COMMENTS | COMPRESSION | CONSTRAINTS |
DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE |
ALL }
and partition_bound_spec is:
IN (partition_bound_expr [, ...])|
FROM ({ partition_bound_expr | MINVALUE | MAXVALUE }[, ...])
TO ({ partition_bound_expr | MINVALUE | MAXVALUE }[, ...])|
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )
index_parameters in UNIQUE, PRIMARY KEY, and EXCLUDE constraints
are:
[ INCLUDE ( column_name [, ... ])][ WITH ( storage_parameter [= value][, ... ])][ USING INDEX TABLESPACE tablespace_name ]
exclude_element in an EXCLUDE constraint is:
{ column_name |( expression )}[ opclass ][ ASC | DESC ][ NULLS
{ FIRST | LAST }]
2.2 创建表
postgres@2f9d6ce41c2b:~$ /usr/local/pgsql/bin/psql mydb
psql (14.2)
Type "help"for help.
mydb=# CREATE TABLE mytable (id int, name char(32), age int);
CREATE TABLE
mydb=#
2.3 删除表
postgres@2f9d6ce41c2b:~$ /usr/local/pgsql/bin/psql mydb
psql (14.2)
Type "help"for help.
mydb=# DROP TABLE mytable;
DROP TABLE
mydb=#