为什么有必要在数据库中显式指定外键和引用?(Why is it necessary to explicitly specify foreign keys and references in databases?)
你能解释一下为什么在创建表时需要指定那些外键吗? 我的意思是,我创建了两个具有一对多关系的表(在ER图上)但我没有指定外键和引用。 我可以使用where子句连接表,甚至可以执行连接等。
可能,我没有得到一些基本概念,虽然我已经阅读了一些关于它的东西。 我想它与数据一致性或参照完整性等有关。
那么,你能解释一下这些概念吗? 如果我有8-10个具有一对多关系的表,并且我可以确保数据正确插入数据库,那么这些引用和外键是绝对必需的吗?
Could you please explain me why it is necessary to specify those foreign keys when creating tables? I mean, I've created two tables which have a one-to-many relationship (on the ER-diagram) but I didn't specify the foreign keys and references. I can connect the tables using the where-clause and even perform joins and so on.
Probably, I don't get some basic concepts, although I've read some stuff about it. I guess it has something to do with data consistency or referential integrity or something.
So, could you explain me these concepts? Are those references and foreign keys are absolutely required if I have, let's say, 8-10 tables with one-to-many relationships and if I can assure that the data is inserted correctly into the database?
原文:https://stackoverflow.com/questions/30287442
最满意答案
您将需要根据分割数据
.
。 您可以使用递归CTE分割数据并返回深度:;with cte (id, DescriptionItem, Description, depth) as ( select id, cast(left(Description, charindex('.',Description+'.')-1) as varchar(50)) DescriptionItem, stuff(Description, 1, charindex('.',Description+'.'), '') Description, 1 as depth from MyTable union all select id, cast(left(Description, charindex('.',Description+'.')-1) as varchar(50)) DescriptionItem, stuff(Description, 1, charindex('.',Description+'.'), '') Description, depth+1 from cte where Description > '' ) select id, DescriptionItem, depth from cte order by id, depth;
或者您可以使用拆分数据的UDF函数:
create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1)) returns @temptable TABLE (items varchar(MAX), depth int) as begin declare @idx int declare @slice varchar(8000) declare @depth int = 1 select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items, depth) values(@slice, @depth) set @String = right(@String,len(@String) - @idx) set @depth = @depth +1 if len(@String) = 0 break end return end;
然后当你调用该函数时,你将使用类似于此的
CROSS APPLY
:select t.id, c.items description, c.depth from mytable t cross apply dbo.split(t.description, '.') c order by t.id, c.depth;
You will want to split the data based on the
.
. You can use a recursive CTE to split the data and return the depth:;with cte (id, DescriptionItem, Description, depth) as ( select id, cast(left(Description, charindex('.',Description+'.')-1) as varchar(50)) DescriptionItem, stuff(Description, 1, charindex('.',Description+'.'), '') Description, 1 as depth from MyTable union all select id, cast(left(Description, charindex('.',Description+'.')-1) as varchar(50)) DescriptionItem, stuff(Description, 1, charindex('.',Description+'.'), '') Description, depth+1 from cte where Description > '' ) select id, DescriptionItem, depth from cte order by id, depth;
Or you can use a UDF function that splits the data:
create FUNCTION [dbo].[Split](@String varchar(MAX), @Delimiter char(1)) returns @temptable TABLE (items varchar(MAX), depth int) as begin declare @idx int declare @slice varchar(8000) declare @depth int = 1 select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items, depth) values(@slice, @depth) set @String = right(@String,len(@String) - @idx) set @depth = @depth +1 if len(@String) = 0 break end return end;
Then when you call the function, you will use
CROSS APPLY
similar to this:select t.id, c.items description, c.depth from mytable t cross apply dbo.split(t.description, '.') c order by t.id, c.depth;
相关问答
更多-
sql server中的子字符串(sub string in sql server)[2023-05-15]
更新了您的查询。 另请注意,您的IF (NULLIF(@Core, '')) IS NOT NULL不正确。 DECLARE @Core VARCHAR(10)='902', @OrgNumber VARCHAR(6) = NULL, @DeptNumber VARCHAR(2) = NULL IF @Core IS NOT NULL BEGIN SELECT @OrgNumber = CASE ... -
CHARINDEX ()在较大的字符串中搜索子字符串,并返回匹配的位置,如果没有找到匹配项,则为0 if CHARINDEX('ME',@mainString) > 0 begin --do something end 编辑或从丹麦人回答,如果您想要找到一个单词(而不是单词的子组件),您的CHARINDEX调用将如下所示: CHARINDEX(' ME ',' ' + REPLACE(REPLACE(@mainString,',',' '),'.',' ') + ' ') (为可能发生的任何其他 ...
-
尝试这个 ; With MyTable as ( SELECT 'okbogkbogkbokgbokgobkgobkofkgbokfgbo&name=fokdofkd&okfaos' FullString UNION SELECT 'fkgbokfgbo&name=fokdofkd&okfaos' FullString UNION SELECT 'okbogkbogkbokgbok' FullString ), PatternIndex as ( Sele ...
-
假设package[@name=告诉你你要找的字符串在哪里,你可以试试这样的东西: DECLARE @toSearch AS VARCHAR(100) = '/content/folder[@name=''Reporting Packages'']/package[@name=''PiotCube_2Year'']' DECLARE @toFind AS VARCHAR(100) = 'package[@name=' DECLARE @start AS INT = CHARINDEX(@toFind , ...
-
sql server中的子串(Substring in sql server)[2023-07-24]
您将@st1定义为varchar(10),然后尝试为其分配15个字符( 'Case # 00548295' )。 在这种情况下,SQL Server会以静默方式截断数据。 只需增加尺寸: DECLARE @st1 varchar(20) SET @st1 = 'Case # 00548295' SELECT @st1,ltrim(SUBSTRING(@st1, CHARINDEX('# ', @st1) + 1, LEN(@st1))) You define @st1 as varchar(10), bu ... -
尝试这个 DECLARE @STR AS VARCHAR(1000) SELECT @STR='Balance Stock Error for Product ID : 4' SELECT substring(@STR,charINDEX(':',@STR)+1,LEN(@STR)-charINDEX(':',@STR)+1) Try this DECLARE @STR AS VARCHAR(1000) SELECT @STR='Balance Stock Error for Product ID : ...
-
你可以使用这样的东西: declare @tbl table (col nvarchar(100)); insert @tbl values ('YY_12.Yellow'), ('ABC_WSA.Thisone_A'), ('SS_4MON.DHHE_A_A') select * , charindex('_', col2, 0) , left(col2, case when charindex('_', col2, ...
-
您将需要根据分割数据. 。 您可以使用递归CTE分割数据并返回深度: ;with cte (id, DescriptionItem, Description, depth) as ( select id, cast(left(Description, charindex('.',Description+'.')-1) as varchar(50)) DescriptionItem, stuff(Description, 1, charindex('.',Description+ ...
-
试试这个 - DECLARE @temp TABLE (st NVARCHAR(50)) INSERT INTO @temp (st) VALUES ('001 BASI Distributor (EXAM)'), ('002 BASI Supplier (EXAM2)'), ('MASI DISTRIBUTOR (EXAM002)'), ('MASI SUPPLIER (EXAM003)'), ('EXAM_ND Distributor Success ...
-
SQL Server子字符串(SQL Server substring)[2022-04-17]
一种方法是使用下面的SQL基本上创建一个lastIndex类型搜索,并使用结果作为长度: len(Referer) – (charindex('_', reverse(Referer))-1) 然后你可以按如下方式重写你的查询,虽然你需要第一个charIndex的结果,所以这是相当激烈的: substring(Referer, charIndex('_',Referer)+1, (len(Referer) – (charindex('_', reverse(Referer))-1) - (charIndex ...