我正在使用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)
有更好的想法吗?
考虑到以下关系:
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)