If you want OrderInfo.OrderID to be a foreign key that references OrderNumber.OrderID, then you have to declare it just as INTEGER not as an INTEGER IDENTITY. If it were an identity, you couldn't assign to it the actual value of the corresponding OrderID that you would have retrieved after inserting to OrderNumber.
That is, you should declare your tables like:
CREATE TABLE Customer
(
CustomerID integer IDENTITY(1,1) PRIMARY KEY,
FirstName varchar(100),
LastName varchar(100)
) ;
CREATE TABLE Consultants
(
ConsultantID integer IDENTITY(1,1) PRIMARY KEY,
EmployeeID integer /* REFERENCES Employees(EmployeeID) */
) ;
CREATE TABLE OrderNumber
(
OrderID integer IDENTITY(1,1) PRIMARY KEY,
CustomerID integer REFERENCES Customer(CustomerID) ON DELETE NO ACTION, /* Referenced CustomerID cannot be deleted */
ConsultantID integer REFERENCES Consultants(ConsultantID) ON DELETE NO ACTION /* Referenced ConsultantID cannot be deleted */
) ;
CREATE TABLE OrderInfo
(
OrderID integer NOT NULL REFERENCES OrderNumber(OrderID) ON DELETE CASCADE,
/* My assumption: one order can have several items
If that were not the case, the following column could be ommitted.
Otherwise, just put all the [OrderInfo] columns in [OrderNumber] table,
and just call it [Order].
*/
SomeOtherColumnSuchAsLineNumber integer NOT NULL,
/* The actual PK should be a composite */
PRIMARY KEY (OrderID, SomeOtherColumnSuchAsLineNumber),
/* Rest of columns */
Cost decimal(12,2),
Description varchar(max)
) ;
-- etc.
We insert customers, consultants and two orders, with two OrderInfo each (I've assumed an order can have more than one item, and added an extra column to sort them).
INSERT INTO Customer(FirstName, LastName)
OUTPUT inserted.CustomerID
VALUES('John', 'Doe') ;
GO
| CustomerID |
| ---------: |
| 1 |
INSERT INTO Consultants(EmployeeID)
OUTPUT inserted.ConsultantID
VALUES (1), (2);
GO
| ConsultantID |
| -----------: |
| 1 |
| 2 |
INSERT INTO OrderNumber(CustomerID, ConsultantID)
OUTPUT inserted.OrderID
VALUES (1,1), (1,2) ;
GO
| OrderID |
| ------: |
| 1 |
| 2 |
INSERT INTO OrderInfo
(OrderID, SomeOtherColumnSuchAsLineNumber, Cost, Description)
VALUES
(1, 1, 100.00, 'Item costing 100.00'),
(1, 2, 99.99, 'Item costing 99.99'),
(2, 1, 200.00, 'Item costing 200.00'),
(2, 2, 199.99, 'Item costing 199.99') ;
GO
4 rows affected
At this point, if you do have for rows in OrderInfo:
SELECT
* -- Don't do this in production, just in demo
FROM
OrderInfo
ORDER BY
OrderInfo.OrderID, OrderInfo.SomeOtherColumnSuchAsLineNumber ;
GO
OrderID | SomeOtherColumnSuchAsLineNumber | Cost | Description
------: | ------------------------------: | :----- | :------------------
1 | 1 | 100.00 | Item costing 100.00
1 | 2 | 99.99 | Item costing 99.99
2 | 1 | 200.00 | Item costing 200.00
2 | 2 | 199.99 | Item costing 199.99
If you delete now from OrderNumber one row, the change propagates to OrderInfo
DELETE FROM
OrderNumber
WHERE
OrderID = 2;
GO
1 rows affected
-- We deleted from OrderNumber OrderID = 2, it won't appear here
SELECT
* -- Don't do this in production, just in demo
FROM
OrderInfo
ORDER BY
OrderInfo.OrderID, OrderInfo.SomeOtherColumnSuchAsLineNumber ;
GO
OrderID | SomeOtherColumnSuchAsLineNumber | Cost | Description
------: | ------------------------------: | :----- | :------------------
1 | 1 | 100.00 | Item costing 100.00
1 | 2 | 99.99 | Item costing 99.99
You can check everything at dbfiddle here