我可以将foriegn密钥添加到现有表中。 | sqlite3

发布于 2025-02-04 19:05:31 字数 902 浏览 1 评论 0原文

因此,我试图完成 finance 。以下是.schema:

    sqlite> .schema
CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE history(
symbol TEXT, name TEXT, shares INTEGER, price NUMERIC, time DATETIME
);
CREATE UNIQUE INDEX username ON users (username);

当我尝试将foriegn键添加到历史记录表时,它总是会返回错误。这是我的代码:

    sqlite> ALTER TABLE history ADD COLUMN id INT;
sqlite> ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
Parse error: near "FOREIGN": syntax error
  ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
                          ^--- error here

So i am trying to complete finance. Following is the .schema:

    sqlite> .schema
CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT NOT NULL, hash TEXT NOT NULL, cash NUMERIC NOT NULL DEFAULT 10000.00);
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE history(
symbol TEXT, name TEXT, shares INTEGER, price NUMERIC, time DATETIME
);
CREATE UNIQUE INDEX username ON users (username);

When i try to add foriegn key to history table it always return error. Here is my code:

    sqlite> ALTER TABLE history ADD COLUMN id INT;
sqlite> ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
Parse error: near "FOREIGN": syntax error
  ALTER TABLE history ADD FOREIGN KEY(id) REFRENCES users(id);
                          ^--- error here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2025-02-11 19:05:31

我认为,根据我在sqlite文档中看到的内容,该语句应该与添加列一起使用:

ALTER TABLE history ADD COLUMN id INTEGER REFERENCES users(id);

但是您请在此语法上检查我!另一个选择是照顾您创建表的同时创建约束。

CREATE TABLE history(
    symbol TEXT,
    name TEXT,
    shares INTEGER,
    price NUMERIC,
    time DATETIME,
    id INTEGER,
    FOREIGN KEY (id)
        REFERENCES users (id));

它可能还没有(尚未意识到),但是每个数据库都具有SQL的独特风味,因此,尽管有SQL标准,但对于特定的DB实现,SQL的语法通常几乎没有差异。因此,在查找SQL DB命令时,您始终必须提防此事。

有关SQLite外键约束的更多详细信息,请参见:
https://www.sqlitetoriorarorial.net/sqlite-forite-foreign-foreign-key/

I think based on what I see in the sqlite docs that the statement should be together with the ADD column:

ALTER TABLE history ADD COLUMN id INTEGER REFERENCES users(id);

But you please check me on this syntax! Another option is to take care of creating the constraint at the same time that you create the table.

CREATE TABLE history(
    symbol TEXT,
    name TEXT,
    shares INTEGER,
    price NUMERIC,
    time DATETIME,
    id INTEGER,
    FOREIGN KEY (id)
        REFERENCES users (id));

It might not be something you have realized (yet) but every database has its unique flavor of SQL, so despite there being a SQL standard there are often little differences in the syntax of SQL for specific db implementations. So you always have to beware of this when looking up commands for your sql db.

Further detail on Sqlite foreign key constraints can be found here:
https://www.sqlitetutorial.net/sqlite-foreign-key/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文