如何更改列的数据类型并在 SQL 中添加外键?

How to change the datatype of column and add foreign key in SQL?

我有一个 user table 和 userInvoice table。现在我在 userInvoice 中有 userId 作为 varchar(150)。我需要将数据类型更改为 bigint 并使此列成为 user table.

的外键

如何在 alter 查询中完成?

更改数据类型。您可能会找到此 link 以获取更多信息 link

 ALTER TABLE userInvoice ALTER COLUMN UserId BIGINT

对于外键。您可能会找到此 link 以获取更多信息 link

ALTER TABLE userInvoice 
ADD FOREIGN KEY (USERID) REFERENCES USER(USERID);

注意:更改列之前要小心,因为如果数据格式不受支持,数据可能会丢失。