英文:
In a Spanner database can I change existing foreign keys for interleaving relationship?
问题
我正在创建一个用于创建Spanner数据库的脚本,我有一些一对多的关系,并且我想将外键用作其他数据库。如果将来我决定使用交错关系,我能否将现有的外键更改为交错关系?
创建表Singers
(
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
)主键(SingerId);
创建表Albums
(
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX),
)主键(SingerId,AlbumId);
ALTER TABLE Albums
ADD CONSTRAINT Albums_fk FOREIGN KEY(SingerId)REFERENCES Singers(SingerId);
这是我的示例脚本,我想使用这个脚本创建我的数据库,并在数据库创建后将来修改关系。
英文:
I am creating a script to create a Spanner database, I have some one-to-many relationships, and I would like to use foreign keys as other databases. if in the future I decide to use interleaving relationships, can I change the existing foreign keys for interleaving?
CREATE TABLE Singers
(
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
) PRIMARY KEY (SingerId);
CREATE TABLE Albums
(
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING( MAX),
) PRIMARY KEY (SingerId, AlbumId);
ALTER TABLE Albums
ADD CONSTRAINT Albums_fk FOREIGN KEY (SingerId) REFERENCES Singers (SingerId);
This is my example script, I would like create my database with this script, and modify the relationship in the future after the database creation
答案1
得分: 1
Spanner不支持交错存在的表格。您可以通过将“child”表格复制到另一个表格(比如child_temp)来解决这个问题。删除现有的child表格,然后使用相同的名称创建一个新的child表格,并将其与parent表格交错。将数据从child_temp复制到child表格,然后删除child_temp表格。
英文:
Spanner doesn't support interleaving existing tables. You can work around that by making a copy of the "child" table to another table (say child_temp). Drop the existing child then create a new child using the same name and interleave it with the parent. Copy data from child_temp to child then drop child_temp.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论