我有两个SQL表ctgl_geometry和ctgl_vertices
ctgl_geometry将存储点如何相互连接的几何,因此它有一个id、一个值、顶点数和一个外键来链接id以获得该几何学的值列表。
CREATE TABLE ctgl_geometry (
Id int primary key,
otherVal int,
NoVertices int,
vertexID int
);
create table ctgl_vertices
(
Id int primary key,
GeometryId int,
val int
);例如,如果有一个顶点列表( {0,1,6,5,4} of 5 ),我将存储它的顶点表,如
INSERT INTO ctgl_vertices VALUES
(1,101,0),
(2,101,1),
(3,101,6),
(4,101,5),
(5,101,4);在几何学方面,如:
INSERT INTO ctgl_geometry VALUES
(1, 10,5,101); 101是知道列表的链接。
但是,如果我不知道标识值,我就不知道如何保存这些值(例如,101,)
因此,我正在考虑使用IDENT_CURRENT来知道最后插入的顶点表值,例如:
SELECT IDENT_CURRENT('ctgl_vertices ') + IDENT_INCR('ctgl_vertices ');并将其分配给ctgl_geometry vertexID的外键。
但我觉得会有更好的方法.
如何插入顶点列表,然后将值赋给几何学?
我还使用这个查询来检索值:
select g.NoVertices,v.val from ctgl_geometry g
inner join ctgl_vertices v
on g.vertexID = v.GeometryId where g.otherVal = 10; 并获取
NoVertices val
5 0
5 1
5 6
5 5
5 4
4 1
4 2
4 7
4 6
4 2
4 3
4 8
4 7
4 4
4 5
4 10
4 9
4 5
4 6
4 11
4 10
4 6
4 7
4 12
4 11
4 7
4 8
4 13
4 12请看一下小提琴
发布于 2016-08-07 22:18:48
您的表模式需要一些认真的注意。你在头脑中合理地定义了对外关系,但实际上并没有实现它们。
您的引用列必须在其上定义一个外键约束,在您的情况下,它是GeometryId。
所引用的列必须是主键列,在您的示例中,该列将是vertexID
考虑到这些建议,表模式应该类似于..。
CREATE TABLE ctgl_geometry (
Id int ,
otherVal int,
NoVertices int,
vertexID int IDENTITY(1,1) NOT NULL primary key
);
GO
create table ctgl_vertices
(
Id int primary key,
GeometryId int REFERENCES ctgl_geometry(vertexID),
val int
);
GO现在,在逻辑上更正表模式之后,您将使用SCOPE_IDENTITY()获取由ctgl_geometry表中的identity列生成的最新标识值,并使用该值将行插入到ctgl_vertices表中。
就像.
Declare @NewID INT;
INSERT INTO ctgl_geometry (Id , otherVal, NoVertices)
VALUES (1, 10,5);
SET @NewID = SCOPE_IDENTITY();
INSERT INTO ctgl_vertices (Id, GeometryId ,Val)
VALUES
(1,@NewID,0),
(2,@NewID,1),
(3,@NewID,6),
(4,@NewID,5),
(5,@NewID,4);https://stackoverflow.com/questions/38818902
复制相似问题