我在MySQL和
中有一个名为payment_request
的表
DESCRIBE payment_request
提供以下输出
下面提供了oderbook
表,
我想将id
中payment_request
表中的orderbook
添加为payment_request_id
列之后名为id
的外键(第二位)。
运行MySQL的SQL是什么?
答案 0 :(得分:2)
首先,您需要在表orderbook
ALTER TABLE orderbook
ADD payment_request_id INT(10) unsigned AFTER ID;
然后添加一个将定义外键
的约束ALTER TABLE orderbook
ADD CONSTRAINT fk_orderbook FOREIGN KEY (payment_request_id)
REFERENCES payment_request (id);
参考:
答案 1 :(得分:1)
您可以在创建表格时执行此操作:
#include <iostream>
using namespace std;
// This function is to create the requested magic squares
int main()
{
int n;
//asking for n
cout << "Please enter an odd number" << endl;
cin >> n;
//checking in case n doesnt follow rules
if (n < 3 || n % 2 == 0)
{
cout << "Invalid Entry, Please re-enter an odd number that is 3 or larger " << endl;
}
else
{
// A function to generate odd sized magic squares
int magicSquare[n][n];
// Setting every slot to 0
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
magicSquare[j][i] = 0;
}
}
// Initializing position to 1
int j = n / 2;
int i = n - 1;
// Setting each value to generate the magic square
for (int num = 1; num <= n * n; )
{
if (j == -1 && i == n)
{
i = n - 2;
j = 0;
}
else
{
//send to next number
// moving it to the right side
if (i == n)
i = 0;
//send to next number again
// moving it to the upper side
if (j < 0)
j = n - 1;
}
//second condition
if (magicSquare[j][i])
{
i -= 2;
j++;
continue;
}
else
//add the number
magicSquare[j][i] = num++;
//first condition
i++; j--;
}
//displaying sum of col/row
cout << "The sum of each row/col: " << n * (n*n + 1) / 2 << endl;
//Dispplaying magic square
for (j = 0; j<n; j++)
{
for (i = 0; i<n; i++)
cout << " " << magicSquare[i][j];
cout << "\n";
}
}
cout << endl;
//re running program
return main();
}
或通过改变表格:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
另请参阅this tutorial。