0

だから、私は本当に簡単なことをしようとしています。テーブルを変更し、外部キー制約を追加します。問題は、列の照合が一致しないというエラーが表示されることです。

列 blah は、外部キー blah で列 blah を参照するのと同じ照合順序ではありません。

私はこれを理解していません、そして私はどこでも見ました。SQL プロパティ ページを見ると、同じ照合順序 (Latin1_General_CI_AS) が表示されます。私はいたるところを見てきましたが、これを変更する方法がわかりません。誰でも助けていただければ幸いです。ありがとう!

4

2 に答える 2

8

When attempting to compare two strings in SQL Server, the collations must match, otherwise you get this error. Collation tells SQL Server how to compare strings (i.e. CHAR, NCHAR, VARCHAR, NVARCHAR). The collation Latin1_General_CI_AS uses the Latin1_General character set, case insensitive, accent sensitive.

Some stuff you need to know before I give you the solution: There are three levels of collation in SQL Server:

  1. The server default collation: This is set during the SQL Server installation, and defines the default collation used for new databases created on the server. This is also the default collation used for the system databases, including TempDB. In other words, if you create a temp table without specifying the collation to use, it will use the server's default. You can find this by right-clicking on the server and selecting properties.
  2. The database default collation. This is the collation that will be used when tables are created in the database, without explicitly specifying the collation when creating the table. By default, this is the same as the Server's collation, but can be specified when creating the database. If you back up the database and restore it on another server with a different collation, it will keep its own collation, and not inherit that of the server. You can see this by right-clicking on the database and selecting properties.
  3. The field collation. This is the collation for a specific field, and exists for all string type fields in the database. If you change the collation of the database, it does not change the collation of all of the string fields in the database; only those fields that are subsequently created on the database. You can see this collation in the table designer, using sp_help TableName, or right-clicking on the field and selecting properties.

In order to fix your problem, you need to change the collation of one of the two fields you're trying to compare, to make them the same. Changing both to match the collation of the database will probably be your best practice:

ALTER TABLE MyMaster ALTER COLUMN MyMasterKey VARCHAR(30) COLLATE LATIN1_General_CI_AS
ALTER TABLE MyDetail ALTER COLUMN MyMasterKey VARCHAR(30) COLLATE LATIN1_General_CI_AS

Then you'll be able to define the foreign key:

ALTER TABLE MyDetail ADD CONSTRAINT FK_Master_Detail FOREIGN KEY (MyMasterKey) REFERENCES MyMaster(MyMasterKey)
于 2012-05-04T12:13:18.870 に答える
1

2 つのテーブルまたは 2 つのフィールド (外部キー - 主キー) の照合順序が異なるようです。もう少し情報をいただけますか?

  1. データベースで照合を確認できますか?
  2. 被参照テーブルの主キーだけでなく、外部キーフィールドの照合も確認できますか?
  3. どこかで一時テーブルを使用していますか? 一時テーブルは、SQL インスタンスの照合を採用しています。データベースが別の照合順序で作成された場合、この問題が発生する可能性があります。

照合順序を確認する方法については、この投稿を参照してください: SQL Authority ブログ投稿

于 2012-05-04T08:12:52.167 に答える