我在PostgreSQL中很新,只知道有关数据库的基本操作。我有一个名为neuro_db的数据库,用户名为neuro_user。现在我发现我无法从neuro_db中删除表中的列。
在ubuntu 14.04中尝试psql neuro_db
后,我进入了以neuro_db=#
开头的命令行。 \d
命令将显示如下表:
List of relations
Schema | Name | Type | Owner
--------+-----------------------------------+----------+------------
public | auth_group | table | neuro_user
public | auth_group_id_seq | sequence | neuro_user
public | auth_group_permissions | table | neuro_user
public | auth_group_permissions_id_seq | sequence | neuro_user
public | auth_permission | table | neuro_user
public | auth_permission_id_seq | sequence | neuro_user
public | auth_user | table | neuro_user
public | auth_user_groups | table | neuro_user
public | auth_user_groups_id_seq | sequence | neuro_user
public | auth_user_id_seq | sequence | neuro_user
public | auth_user_user_permissions | table | neuro_user
public | auth_user_user_permissions_id_seq | sequence | neuro_user
public | django_admin_log | table | neuro_user
public | django_admin_log_id_seq | sequence | neuro_user
public | django_content_type | table | neuro_user
public | django_content_type_id_seq | sequence | neuro_user
public | django_migrations | table | neuro_user
public | django_migrations_id_seq | sequence | neuro_user
public | django_session | table | neuro_user
public | neuro_category | table | neuro_user
public | neuro_category_id_seq | sequence | neuro_user
public | neuro_page | table | neuro_user
public | neuro_page_id_seq | sequence | neuro_user
(23 rows)
我想编辑表格" neuro_page"在输入\d neuro_page
后,它将显示此表:
Table "public.neuro_page"
Column | Type | Modifiers
-------------+------------------------+---------------------------------------------------------
id | integer | not null default nextval('neuro_page_id_seq'::regclass)
category_id | integer | not null
title | character varying(128) | not null
text | text | not null
url | character varying(200) | not null
photo | character varying(100) | not null
order | character varying(128) | not null
date | date | not null
views | integer | not null
likes | integer | not null
我想删除"订单"此表中的列,因此我尝试在ALTER TABLE neuro_page DROP COLUMN order
行之后键入neuro_db=#
,但没有任何反应。分别尝试ALTER TABLE neuro_page
和DROP COLUMN order
后也没有任何内容。 "命令"专栏还在那里!并且没有错误信息!
我也在谷歌上搜索了答案,但一无所获。我想我已经遵循了数据库中的正常操作,但是不能在表中删除单个列。没有外键用于" order"列。
如果有人能帮我解决这个问题,我会非常感激。谢谢!
答案 0 :(得分:1)
您需要在反引号中转义列名称顺序,因为order是关键字(例如order by 'column_name'
)。确保使用分号显示命令的结尾:
ALTER TABLE neuro_page DROP COLUMN `order`;