e.g.
当前的表中主键设为;corp_id和stuff_qr_code;需要删除这两个主键;并设置id为主键
type StuffPos struct {
ID int64
CorpID int64 `gorm:"primary_key;index;not null"`
StuffQrCode string `gorm:"primary_key;index;not null"`
StuffFilesID int64 `gorm:"index;not null"`
StuffFilesName string `gorm:"not null"`
DeptID int64 `gorm:"index;not null"`
DeptName string `gorm:"not null"`
.....
}
执行命令
SHOW CONSTRAINTS FROM 表名; //查看表的主键信息
root@:26257/corpstore18> SHOW CONSTRAINTS FROM corpstore_stuff_position;
table_name | constraint_name | constraint_type | details | validated
---------------------------+-----------------+-----------------+----------------------+------------
corpstore_stuff_position | primary | PRIMARY KEY | PRIMARY KEY (id ASC) | true
(1 row)
Time: 6.5095ms
BEGIN;
ALTER TABLE corpstore_stuff_position DROP CONSTRAINT "primary";
ALTER TABLE corpstore_stuff_position ADD CONSTRAINT "primary" PRIMARY KEY (id);
COMMIT;
本文介绍如何将表corpstore_stuff_position的主键从corp_id和stuff_qr_code改为单一字段id的过程,包括查看现有约束、删除旧主键及设置新主键的具体SQL命令。
850

被折叠的 条评论
为什么被折叠?



