質問

I try to modell an order. An order should be identified by a customer and a timestamp. Now i have a second table that holds all ordered products. Coloumns are customer,timestamp,product and amount where customer and timestamp should be foreign keys taken from my order. The problem is in mysql-workbench it seems like I can´t reference a timestamp. In the "Foreign Keys" menu in the right column i try to check datetime column but it remains unchecked and i can´t select a "referenced column".

I hope it´s clear what I want to do. Thanks for your help.

役に立ちましたか?

解決

For a column to be referenced by a foreign key it must be a primary key. I would recommend adding an ID column to your tables and making it your primary key. You can add constraints if necessary to ensure that combinations of Customer and Timestamp are unique.

Assuming you are using SQL Server:

CREATE TABLE Order
(
    ID int IDENTITY(1,1) PRIMARY KEY,
    Customer varchar(255) NOT NULL,
    Timestamp DateTime NOT NULL,
    CONSTRAINT uc_Order UNIQUE (Customer ,Timestamp)
)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top