在SQL中插入两个列的约束
大家好,我为数据库创建了此表,
CREATE TABLE IF NOT EXISTS compositions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id_product INTEGER REFERENCES products(id),
id_receipt INTEGER REFERENCES receipt(id),
quantity INTEGER NOT NULL,
price FLOAT NOT NULL,
id_user VARCHAR REFERENCES users(id),
CONSTRAINT CHK_price_quantity CHECK (price > 0 AND quantity > 0)
);
我想插入一个约束,对于给定的ID_receipt,ID_USER必须始终相同,但我不知道该如何实现。 有人可以帮助吗?
先感谢您。
Hi everyone I created this table for a database
CREATE TABLE IF NOT EXISTS compositions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
id_product INTEGER REFERENCES products(id),
id_receipt INTEGER REFERENCES receipt(id),
quantity INTEGER NOT NULL,
price FLOAT NOT NULL,
id_user VARCHAR REFERENCES users(id),
CONSTRAINT CHK_price_quantity CHECK (price > 0 AND quantity > 0)
);
I would like to insert a constraint that, for a given id_receipt, the id_user must be always the same but I don't know how to implement it.
Could someone give a help?
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此要求,列
id_user
不应在表coptosings
。中定义。
将其存储在表
Receipt
中更有意义,由于每个id_receipt
与1相关,只有1个id_user
。这样,您将
id_receipt
作为中的外键
构图,通过此列,您可以从receip
(例如id_user
)获得(通过加入)所需的所有信息。For this requirement the column
id_user
should not be defined in the tablecompositions
.It makes more sense to store it in the table
receipt
, since eachid_receipt
is related to 1 and only 1id_user
.This way you have
id_receipt
as a foreign key incompositions
and through this column you can get (by a join) all the info that you want fromreceipt
, like theid_user
.