当前位置:网站首页>postgresql创建表
postgresql创建表
2022-06-11 15:39:00 【happytree001】
一、环境
[email protected]:~$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
[email protected]:~$ /usr/local/pgsql/bin/psql -V
psql (PostgreSQL) 14.2
二、创建表
2.1 语法
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 创建表
[email protected]:~$ /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 删除表
[email protected]:~$ /usr/local/pgsql/bin/psql mydb
psql (14.2)
Type "help" for help.
mydb=# DROP TABLE mytable;
DROP TABLE
mydb=#
- 没有show databases/tables;查看不方便,还没有找到如何看已经存在的库/表,
- 对于表空间、schema等待学习
边栏推荐
猜你喜欢

Take you in-depth understanding of AGC cloud database

leetcode 120. Triangle minimum path sum

码农必备SQL调优(下)

openGauss 3.0.0版本正式发布,立即体验社区首个轻量版本

MAUI 入门教程系列(1.框架简介)

Introduction and use of etcd

零基础自学软件测试,我花7天时间整理了一套学习路线,希望能帮助到大家..

3000 words to teach you how to use mot
![[creation mode] single instance mode](/img/80/b90c7358de9670e9b07d28752efee5.png)
[creation mode] single instance mode

Learn automatic testing of postman interface from 0 to 1
随机推荐
After nine years of testing, the salary for interviewing Huawei is 10000. Huawei employees: the company doesn't have such a low salary position
Google Earth engine (GEE) - create a simple panel demo to display the map
MAUI 入门教程系列(1.框架简介)
PHP Apache built-in stress testing tool AB (APACHE bench)
Opengauss enterprise installation
openGauss简单查询SQL的执行流程解析
Zero foundation self-study software test, I spent 7 days sorting out a set of learning routes, hoping to help you
Unified record of my code variable names
DB4AI: 数据库驱动AI
Yiwenjiaohui your database system tuning
Using cloud DB to build apps quick start - quick games
关于 JS 函数的一切
Go language slice
Is it possible to use multiple indexes together in a query?
From 0 to 1, master the mainstream technology of large factories steadily. Isn't it necessary to increase salary after one year?
Learn automatic testing of postman interface from 0 to 1
Opengauss version 3.0.0 was officially released, and immediately experience the first lightweight version in the community
数据库设计建议
【愚公系列】2022年06月 .NET架构班 077-分布式中间件 ScheduleMaster加载程序集定时任务
中山大学大气院徐伟新教授团队关于弱对流造成极端降水的研究成果被Science亮点报道