PostgreSql 表

news/2024/11/17 4:54:58/

一、创建表

1.1 语法

CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [{ column_name data_type [ 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_nameOF 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_namePARTITION 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 ]其中 column_constraint 是:[ 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 ]table_constraint 是:[ 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 ]like_option 是:{ INCLUDING | EXCLUDING } { COMMENTS | CONSTRAINTS | DEFAULTS | GENERATED | IDENTITY | INDEXES | STATISTICS | STORAGE | ALL }partition_bound_spec 是:IN ( partition_bound_expr [, ...] ) |
FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )UNIQUEPRIMARY KEY以及EXCLUDE约束中的index_parameters是:[ INCLUDE ( column_name [, ... ] ) ]
[ WITH ( storage_parameter [= value] [, ... ] ) ]
[ USING INDEX TABLESPACE tablespace_name ]一个EXCLUDE约束中的exclude_element是:{ column_name | ( expression ) } [ opclass ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ]

具体参数说明可参考:https://www.postgresql.org/docs/current/sql-createtable.html

1.2 常用示例

--创建表
CREATE TABLE test(id int, note varchar(20));
--创建表时带主键
CREATE TABLE test(id int PRIMARY KEY, note varchar(20));
--创建表时带复合主键
CREATE TABLE test(id1 int, id2 int, note varchar(20), CONSTRAINT pk_test PRIMARY KEY(id1,id2));
--创建表时带符合主键和唯一约束
CREATE TABLE test(id1 int, id2 int, id3 int, note varchar(20), CONSTRAINT pk_test PRIMARY KEY(id1,id2), CONSTRAINT uk_test_id3 UNIQUE(id3));
--创建表时带非空约束
CREATE TABLE test(id int NOT NULL, note varchar(20));
--创建表时带检查约束
CREATE TABLE child(name varchar(20), age int, note text, CONSTRAINT ck_child_age CHECK(age <18));
--创建表时带外键约束
CREATE TABLE class(class_no int PRIMARY KEY, class_name varchar(40));
CREATE TABLE student(student_no int PRIMARY KEY, student_name varchar(40), age int, class_no int REFERENCES class(class_no));
--创建表时指定默认值
CREATE TABLE student(no int, name varchar(20),age int DEFAULT 15);
--创建临时表
CREATE TEMPORARY/TEMP TABLE tmp_t1(id int PRIMARY KEY, note text);
--创建 UNLOGGED 表
CREATE UNLOGGED TABLE unlogged01(id int PRIMARY KEY, t text);

UNLOGGED表在使用上与普通表没有区别,仅仅在插入、删除、更新数据时不产生WAL日志,所以做这些DML操作的性能会更高。另外需要注意的是,数据库异常宕机时,UNLOGGED表的数据可能会丢失。

--复制表结构
CREATE TABLE baby (LIKE child);
CREATE TABLE baby AS SELECT * FROM child WITH NO DATA;
--复制表结构时带所有属性
CREATE TABLE baby (LIKE child INCLUDING ALL);

此处创建的表没有把源表列上的约束复制过来,第一种写法如果想完全复制源表列上的约束和其他信息,则需要加“INCLUDING”关键字,可用的“INCLUDES”选项如下:

  • INCLUDING DEFAULTS
  • INCLUDING CONSTRAINTS
  • INCLUDING INDEXES
  • INCLUDING STORAGE
  • INCLUDING COMMENTS
  • INCLUDING ALL

二、修改表

--修改名表
ALTER TABLE table_name RENAME TO new_name;
--修改表所在模式
ALTER TABLE table_name SET SCHEMA new_schema;
--修改表所属 owner
ALTER TABLE table_name OWNER TO role_name;
--修改表所在表空间
ALTER TABLE table_name SET TABLESPACE tablespace_name;
--添加字段
ALTER TABLE table_name ADD column_name datatype;
--删除字段
ALTER TABLE table_name DROP column_name;
--修改字段类型
ALTER TABLE table_name ALTER column_name TYPE datatype;
--修改字段名
ALTER TABLE table_name RENAME column_name TO new_column_name;
--设置/删除字段非空约束
ALTER TABLE table_name ALTER column_name {SET|DROP} NOT NULL;
--设置字段默认值
ALTER TABLE table_name ALTER column_name SET DEFAULT expression;
--修改约束名
ALTER TABLE table_name RENAME CONSTRAINT constraint_name TO new_constraint_name;
--添加主键
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY(id);
--添加唯一约束
ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(id);
--添加检查约束
ALTER TABLE table_name ADD CONSTRAINT constraint_name CHECK(id > 100);
--添加外键约束
ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY(id) REFERENCES reftable(id);
--删除约束
ALTER TABLE table_name DROP CONSTRAINT constraint_name;

三、删除表

ALTER TABLE table_name [CASCADE];
  • CASCADE:自动删除依赖于该表的对象(例如视图)。

四、分区表

https://xiaosonggong.blog.csdn.net/article/details/123357556


http://www.ppmy.cn/news/788565.html

相关文章

使用OpenCV检测两张图片的关键点并计算关键点的描述子

#include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/features2d/features2d.hpp> #include <opencv2/highgui/highgui.hpp>

第三章——视图和视图层级

在接下来的五章中&#xff0c;您将构建一个名为 WorldTrotter 的应用程序。 完成后&#xff0c;此应用程序将转换华氏温度和摄氏温度之间的值。 在本章中&#xff0c;您将通过创建 WorldTrotter 的UI来了解视图和视图层级。 在本章结尾&#xff0c;您的应用程序将如图3.1所示。…

雅思口语考试的口音问题怎么解决?

很多学生都担心自己的口音不标准&#xff0c;也许在考试中难拿高分。确实口音不标准对于学生的雅思口语会有影响&#xff0c;那么就来看看雅思考试口音问题怎么解决吧。 一、雅思口语考试口音问题 1、分清楚元音和辅音中的易混音 听力中大家一定遇到过这样的情况&#xff0c;…

[RISC-V]Milk-v开发板 陀螺仪 lsm6dsr i2c驱动module及测试程序

碰到权限问题 sudo chmod 777 -R /home/dar/riscv/milkv/duo-buildroot-sdk 加载配置及环境 cd build/ source milkvsetup.sh defconfig cv1800b_milkv_duo_sd 编译驱动&#xff0c;要求先编译内核 build_all 设备树 build\boards\cv180x\cv1800b_milkv_duo_sd\dts_risc…

go数据结构之slice与map

1. 切片 1. 切片结构定义 type slice struct {array unsafe.Pointerlen intcap int }array:引用的底层数组&#xff0c;动态数组&#xff0c;可以修改 如果多个切片的array指针指向同一个动态数组&#xff0c;则它们都可以对底层这个动态数组元素进行修改。 len:&#xf…

解决电脑不能进入BIOS页面

电脑不能进入BIOS页面的解决方法 解决方法&#xff1a; 今天我的win10系统按F1进不去BIOS页面&#xff0c;做好我尝试了使用U盘来辅助进入BIOS页面&#xff0c;具体操作如下: http://www.yayihouse.com/yayishuwu/chapter/1632

VoxelNet End-to-End Learning for Point Cloud Based 3D Object Detection 论文学习

代码&#xff1a;VoxelNet End-to-End Learning for Point Cloud Based 3D Object Detection 论文&#xff1a;VoxelNet End-to-End Learning for Point Cloud Based 3D Object Detection 1. 解决了什么问题&#xff1f; 对点云做 3D 检测是许多应用得以实现的关键&#xff0…

super函数的用法

1、super函数的作用&#xff1a;调用父类的方法 class A:def __init__(self):print(A)class B(A):def __init__(self):print(B)super().__init__()b B()结果 B A2、什么场景下使用super函数&#xff1a;在代码重用的场景下使用 # 需要创建一个类&#xff0c;让类具有多线程…