Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除SQL Server数据id不需要';不存在_Sql_Sql Server - Fatal编程技术网

删除SQL Server数据id不需要';不存在

删除SQL Server数据id不需要';不存在,sql,sql-server,Sql,Sql Server,我不熟悉SQL查询,但我正在尝试构建一个查询,以使用要匹配的ID,基于与另一个表(用户)的连接删除任何数据(报告的注释),下面是一个示例架构: create table tbl_reported_comment(id int, commentId int, reported_by_userid int); insert tbl_reported_comment values (1, 1, 101), (2, 2, 131), (3, 3, 101), (4, 4, 101),

我不熟悉SQL查询,但我正在尝试构建一个查询,以使用要匹配的ID,基于与另一个表(用户)的连接删除任何数据(报告的注释),下面是一个示例架构:

create table tbl_reported_comment(id int, commentId int, reported_by_userid int);

insert tbl_reported_comment values
  (1, 1, 101),
  (2, 2, 131),
  (3, 3, 101),
  (4, 4, 101),
  (5, 5, 24),
  (6, 6, 201),
  (7, 7, 1),
  (8, 8, 24),
  (9, 9, 23),
  (10, 10, 16),
  (11, 11, 31);

Create table tbl_user(userId int, Username varchar(50));

insert tbl_user values
  (1, 'admin'),
  (101, 'test1'),
  (131, 'test2'),
  (24, 'test3'),
  (201, 'test4');
在这种情况下,我试图实现以下目标:

删除tbl_reported_comment表中 [reported_by_userid]列在用户中不作为[userid]存在 桌子

以下是指向具有此示例架构的SQLFIDDLE的链接:。我使用SQL Server作为数据库

非常感谢,

这个怎么样

  DELETE FROM dbo.tbl_reported_comment
  OUTPUT DELETED.id, DELETED.commentId, DELETED.reported_by_userid
  WHERE reported_by_userid NOT IN 
             (SELECT UserID FROM dbo.tbl_user)
这将删除这些行,并将删除的行输出到屏幕(在SQLServerManagementStudio中),以便您可以查看删除的内容

Ben是对的——一旦您这样做了,您应该在这两个表之间建立一个外键关系,以避免将来出现这样的僵尸数据

-- make your "UserID" column NOT NULL
ALTER TABLE dbo.tbl_user
ALTER COLUMN UserID INT NOT NULL

-- so that we can use it as the PRIMARY KEY for that table!
ALTER TABLE dbo.tbl_user
ADD CONSTRAINT PK_user PRIMARY KEY CLUSTERED(UserID)

-- so that we can then establish a FK relationship between those two tables....
ALTER TABLE dbo.tbl_reported_comment
ADD CONSTRAINT FK_reported_comment_user
FOREIGN KEY (reported_by_userid) REFERENCES dbo.tbl_user(UserID)

使用
EXISTS
函数的替代子查询如下所示:

DELETE x
FROM  tbl_reported_comment x 
WHERE 
   NOT EXISTS 
    (
    SELECT 1
    FROM tbl_user y
    WHERE 
       x.reported_by_userid = y.userId
    )

构建数据库的正确方法是创建一个数据库,这使您现在面临的情况变得不可能。数据库的一个重要方面是确保引用和数据完整性,即使您的数据不可能出错。
notin(选择DISTINCT…
在我脑海中敲响了警钟。这真的需要吗?“不存在”不是比“不存在”更好吗?@ErikE:你可能也可以省略
不同的
——不,我不认为
如果不存在()
总是更好。有时一种解决方案更快,另一种则更快。我更喜欢这种方法(但
如果不存在()
也没有错),谢谢您的回答。实际上,我选择了NOT EXISTS,因为在in子句的值超过2000的情况下使用它可能更安全。Quassnoi对此有很好的了解。我对
in()
没有任何问题,只是
内部的
没有逻辑意义。