PostgreSQL 管理数据表(三)

删除约束

删除约束通常需要知道它的名称,可以通过psql工具的\d table_name命令查看表的约束。

ALTER TABLE table_name DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ];

RESTRICT是默认值,如果存在其他依赖于该约束的对象,需要使用CASCADE执行级联删除。例如,外键约束依赖于被引用字段上的唯一约束或主键约束。

删除产品表name字段上的唯一约束 

test=#  ALTER TABLE products DROP CONSTRAINT products_name_uk;
ALTER TABLE
test=# \d products;
                  Table "hr.products"
   Column   |  Type   | Collation | Nullable | Default 
------------+---------+-----------+----------+---------
 product_no | integer |           | not null | 
 name       | text    |           | not null | 
 price      | numeric |           |          | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_no)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

删除非空约束也需要使用单独的语法:

ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL;

以下语句将会删除产品表name 字段上的非空约束:

test=# ALTER TABLE products ALTER COLUMN name DROP NOT NULL;
ALTER TABLE
test=# \d products;
                  Table "hr.products"
   Column   |  Type   | Collation | Nullable | Default 
------------+---------+-----------+----------+---------
 product_no | integer |           | not null | 
 name       | text    |           |          | 
 price      | numeric |           |          | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_no)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

修改字段默认值

如果想要为某个字段设置或者修改默认值,可以使用以下语句:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT value;

我们将为产品表的价格设置一个默认值

test=# ALTER TABLE products ALTER COLUMN price SET DEFAULT 8.88;
ALTER TABLE
test=# \d products;
                  Table "hr.products"
   Column   |  Type   | Collation | Nullable | Default 
------------+---------+-----------+----------+---------
 product_no | integer |           | not null | 
 name       | text    |           |          | 
 price      | numeric |           |          | 8.88
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_no)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

同样,可以删除已有的默认值:

ALTER TABLE table_name ALTER COLUMN column_name DROP DEFAULT;

以下语句可以删除产品表中的价格默认值:

test=# ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
ALTER TABLE
test=# \d products;
                  Table "hr.products"
   Column   |  Type   | Collation | Nullable | Default 
------------+---------+-----------+----------+---------
 product_no | integer |           | not null | 
 name       | text    |           |          | 
 price      | numeric |           |          | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_no)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

删除字段的默认值相当于将它设置为空值(NULL)。

修改字段数据类型

通常来说,可以将字段的数据类型修改为兼容的类型。

ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type;

以下语句将产品表的price字段的类型修改为numeric(10,2):

test=# ALTER TABLE products ALTER COLUMN price TYPE numeric(10,2);
ALTER TABLE
test=# \d products;
                     Table "hr.products"
   Column   |     Type      | Collation | Nullable | Default 
------------+---------------+-----------+----------+---------
 product_no | integer       |           | not null | 
 name       | text          |           |          | 
 price      | numeric(10,2) |           |          | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_no)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

因为已有的数据能够隐式转换为新的数据类型,上面的语句能够执行成功。如果无法执行隐式转换(例如将字符串‘1’转换为数字1),可以使用USING执行显式转换

ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type USING expression;

我们先为产品表增加一个字符串类型的字段level,然后将其修改为整数类型。

test=# ALTER TABLE products ADD COLUMN level VARCHAR(10);
ALTER TABLE
test=# ALTER TABLE products ALTER COLUMN level TYPE INTEGER;
ERROR:  column "level" cannot be cast automatically to type integer
HINT:  You might need to specify "USING level::integer".
test=# ALTER  TABLE  products  ALTER  COLUMN  level  TYPE  INTEGER  USING level::integer;
ALTER TABLE

重命名字段

使用以下语句可以修改表中字段的名称

ALTER TABLE table_nameRENAME COLUMN column_name TO new_column_name;

将产品表的字段product_no 改名为product_number:

test=# ALTER TABLE products RENAME COLUMN product_no TO product_number;
test=# \d products
                       Table "hr.products"
     Column     |     Type      | Collation | Nullable | Default 
----------------+---------------+-----------+----------+---------
 product_number | integer       |           | not null | 
 name           | text          |           |          | 
 price          | numeric(10,2) |           |          | 
 level          | integer       |           |          | 
Indexes:
    "products_pkey" PRIMARY KEY, btree (product_number)
Check constraints:
    "products_price_min" CHECK (price > 0::numeric)

重命名表

如果需要修改表的名称,可以使用以下语句:

ALTER TABLE table_name RENAME TO new_name;

例如,将产品表的名称改为items的命令如下

ALTER TABLE productsRENAME TO items;

删除表

删除表可以使用DROP TABLE语句

DROP TABLE [ IF EXISTS ] name [ CASCADE | RESTRICT ];

其中,name表示要删除的表;如果使用了IF EXISTS,删除一个不存在的表不会产生错误,而是显示一个信息。以下语句将会删除表emp1:

DROP TABLE emp1;

如果被删除的表存在依赖于它的视图或外键约束,需要指定CASCADE选项执行级联删除。

posted @ 2023-07-05 10:16  晓枫的春天  阅读(89)  评论(0编辑  收藏  举报