开发者

using column name as foreign key in sqlite

开发者 https://www.devze.com 2023-03-05 00:06 出处:网络
Is there a way to reference \"column-names of given table\" as foreign key in sqlite? I\'ve tested: create table \"test\"(

Is there a way to reference "column-names of given table" as foreign key in sqlite?

I've tested:

create table "test"(
    t开发者_StackOverflowest TEXT
);
create table "othertest"(
    othertest TEXT references PRAGMA table_info("test")[1]
);

But as expected, it does not work... (the error is Error: near "table_info": syntax error, but even if table_info was accepted, I doubt that the [1] part would be valid)


In general, SQL database management systems don't allow expressions in DDL. Systems that support information_schema views won't let you use a scalar subquery to return the target for a foreign key reference. This is a syntax error in all the SQL dbms I use day to day.

create table test (
    article_id integer primary key 
        references articles (select column_name 
                             from information_schema.columns
                             where table_name = 'articles'
                             and is_nullable = 'NO')
);

What is the real problem you need to solve by trying to do this?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号