相关疑难解决方法(0)

如何在NULL列上创建唯一索引?

我正在使用SQL Server 2005.我想将列中的值限制为唯一,同时允许NULLS.

我当前的解决方案涉及视图上的唯一索引,如下所示:

CREATE VIEW vw_unq WITH SCHEMABINDING AS
    SELECT Column1
      FROM MyTable
     WHERE Column1 IS NOT NULL

CREATE UNIQUE CLUSTERED INDEX unq_idx ON vw_unq (Column1)
Run Code Online (Sandbox Code Playgroud)

有更好的想法吗?

sql sql-server indexing constraints unique

98
推荐指数
3
解决办法
9万
查看次数

在Rails中添加索引有很多关系

考虑到以下关系:

class Style < ActiveRecord::Base
  has_many :stylefeatures, :dependent => :destroy
  has_many :features, :through => :stylefeatures
end

class Stylefeature < ActiveRecord::Base
  belongs_to :style
  belongs_to :feature
end

class Feature < ActiveRecord::Base
  has_many :stylefeatures, :dependent => :destroy
  has_many :styles, :through => :stylefeatures
end
Run Code Online (Sandbox Code Playgroud)

如何在Style模型中最有效地添加索引以加速此方法:

  def has_feature? (arg)
    self.features.where(:name=>arg).exists?
  end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

4
推荐指数
1
解决办法
3206
查看次数

标签 统计

constraints ×1

indexing ×1

ruby-on-rails ×1

sql ×1

sql-server ×1

unique ×1