# CREATE FOREIGN TABLE
#### 功能描述
创建外表。
#### 注意事项
- 外表中暂不支持使用系统列（如：tableoid、ctid等），其中Private和Shared模式的外表，需要初始用户或者运维模式下（operation_mode）的运维管理员权限。
- OPTIONS中的敏感字段（如：password、secret_access_key等）在使用多层引号时，语义和不带引号的场景是不同的，因此不会被识别为敏感字段进行脱敏。
- 多租场景下，只有初始用户可在PDB内部执行CREATE FOREIGN TABLE命令。
 
#### 语法格式
```
CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [
    column_name type_name [ OPTIONS ( { option 'value' } [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ]
    [, ... ]
] )
  SERVER server_name
[ OPTIONS ( { option 'value' } [, ... ] ) ];
```
![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/figure/zh-cn_image_0000002764461040.png "点击放大")
其中列约束column_constraint为：
```
[ CONSTRAINT constraint_name ]
{ NOT NULL |
  NULL |
  DEFAULT default_expr }
```
![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/figure/zh-cn_image_0000002764301110.png)
#### 参数说明
- **IF NOT EXISTS**
  用于判断是否存在同名的表。如果数据库中已经存在一个同名的表，不会直接报错，而是会产生一条提示，用于告知表关系已存在。
  
- **table_name**
  外表的表名。
  取值范围：字符串，要符合[标识符命名规范](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0003.html#ZH-CN_TOPIC_0000002793779057__section1257443811315)。
  
- **column_name**
  外表中的字段名。
  取值范围：字符串，要符合[标识符命名规范](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0003.html#ZH-CN_TOPIC_0000002793779057__section1257443811315)。
  
- **type_name**
  字段的数据类型。
  
- **COLLATE collation**
  指定列的排序规则（该列的数据类型必须为可排列的数据类型）。如果没有指定，则使用默认的排序规则。
  排序规则可以使用"SELECT \* FROM pg_collation;"命令从系统表中查询。
  
- **SERVER server_name**
  外表的server名称。
  
- **OPTIONS ( option 'value' \[, ... \] )**
  用于指定外表数据的各类参数。外部数据封装器file_fdw支持的options包括：
  ![](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  - 指定OPTIONS时，允许指定的选项名和值是由每一个外部数据封装器file_fdw进行指定，并且通过外部数据封装器file_fdw的验证函数进行验证。file_fdw更多使用内容请参见《开发指南》中"特性使用指导 \> Foreign Data Wrapper \> file_fdw"章节。
  
  - 不允许重复指定相同的选项名（尽管表选项和表字段选项可以存在相同的名字）。
   
  - filename 必选，指定需要读取的数据文件，必须为一个绝对路径名。
    
  
  - format 远端server的数据文件格式，支持TEXT、CSV、BINARY以及FIXED格式，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[FORMAT 'format_name'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l4a19396d1b2f4e5d9e24f1ec3a05fd50)选项相同。
    
  
  - header 指定的数据文件是否包含标题行，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[HEADER \[ boolean \]](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l937564f7eb774103b8b3506494ba646e)选项相同。
    
  
  - delimiter 指定数据文件的分隔符，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[DELIMITER 'delimiter_character'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_le3f16999f274421fbff626cba4961f6b)选项相同。
    
  
  - quote 指定数据文件的引用字符，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[QUOTE 'quote_character'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l0a97cff860044701a2b4f255e182cfc7)选项相同。
    
  
  - escape 指定数据文件的转义字符，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[ESCAPE 'escape_character'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l77f43e9956d041e68bf13aec95afb412)选项相同。
    
  
  - null 指定数据文件的null字符串，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[NULL 'null_string'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l51825abc2dfc46c39cd827422a41f402)选项相同。
    
  
  - encoding 指定数据文件的编码，与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[ENCODING 'encoding_name'](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_leec253f5fb1c43d788efdde6ac28c841)选项相同。
    
  
  - force_not_null 一个布尔选项，如果为真，则声明字段的值不应该匹配空字符串（文件级别null选项），与[COPY](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html)章节中的[FORCE_NOT_NULL ( column_name \[, ...\] )](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0240.html#ZH-CN_TOPIC_0000002793779485__zh-cn_topic_0283136676_zh-cn_topic_0237122096_zh-cn_topic_0059778766_l536b0c6d5be84730904e479b07e80960)选项中的字段相同。
    
    
 
- **NOT NULL**
  字段值不允许为NULL。
  
- **NULL**
  字段值允许为NULL。
  
- **DEFAULT default_expr**
  默认表达式，为字段指定默认值。default_expr可以是任何不含变量的表达式（不允许使用子查询和对本表中的其他字段的交叉引用），默认表达式的数据类型必须和字段类型匹配。
  默认表达式可用于未声明字段数值的插入操作，如果没有指定默认值，则默认为NULL。
  
 #### 示例
```
--创建server。
gaussdb=# CREATE SERVER my_server FOREIGN DATA WRAPPER log_fdw;
--创建外表。
gaussdb=# CREATE FOREIGN TABLE foreign_tbl (col1 text) SERVER my_server OPTIONS (logtype 'gs_log');
--删除外表。
gaussdb=# DROP FOREIGN TABLE foreign_tbl;
--删除server。
gaussdb=# DROP SERVER my_server;
```
#### 相关链接
[ALTER FOREIGN TABLE](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0191.html)，[DROP FOREIGN TABLE](https://support.huaweicloud.com/centralized-ref-v10-gaussdb/gaussdb-38-0310.html)
