【问题标题】:Simple SQL question: where is the syntax error? [duplicate]简单的 SQL 问题:语法错误在哪里? [复制]
【发布时间】:2021-01-14 03:24:40
【问题描述】:
sqlite> .schema autor

CREATE TABLE autor (ID INTEGER PRIMARY KEY, NOME TEXT, CIDADE TEXT, ESTADO TEXT, TELEFONE TEXT, idade number);

sqlite> alter table autor drop idade;

Error: near "drop": syntax error

(也尝试使用 DROP COLUMN 而不是 DROP)。 请帮忙,这是一个错误吗?

【问题讨论】:

标签: sql sqlite ddl create-table alter-table


【解决方案1】:

您不能使用 SQLite 执行此操作,因为它不支持删除列。这是a documented limitation(强调我的):

仅支持ALTER TABLE 命令的RENAME TABLEADD COLUMNRENAME COLUMN 变体。省略了其他类型的ALTER TABLE 操作,例如DROP COLUMNALTER COLUMNADD CONSTRAINT 等。

一种可能的解决方法是使用正确的结构重新创建另一个表,然后复制数据,删除旧表并重命名新表:

create table autor_new (
    id integer primary key, 
    nome text, 
    cidade text, 
    estado text, 
    telefone text
);
insert into autor_new select id, nome, cidade, estado, telefone from autor;
drop table autor;   -- back it up first!
alter table autor_new rename to autor;

【讨论】:

    猜你喜欢
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-11-06
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多