PDA

View Full Version : تعیین چند فیلد منحصر به فرد



dkhatibi
یک شنبه 06 آبان 1386, 14:51 عصر
در Enterprise Manager در هنگام طراحی جدول چه جوری می شه چند فیلد منحصر به فرد را معین نمود؟

رضا عربلو
یک شنبه 06 آبان 1386, 18:21 عصر
اگه منظورت multiselect کافی کلید ctrl را نگه داری و بروی ستون ها کلیک کنی.

dkhatibi
دوشنبه 07 آبان 1386, 06:58 صبح
نه منظورم MultiSelect نیست.
فیلدی که Primary انتخاب می شه لزوما منحصر به فرد است.
می خواهیم علاوه بر اون یک یا چند فیلد منحصر به فرد داشته باشم. مثلا فیلد a , b که اگر کاربر داده لی تکراری برای هر کدوم از این فیلدها به طور جداگانه وارد کرد خطا رخ دهد؟
یا کاری کرد که چند Primary داشته باشم.
با MultiSelect می شه چند تا Primary داشت اما اونا به هم وابسته می شن!

hmm
دوشنبه 07 آبان 1386, 08:55 صبح
به نقل از BOL:


Creating a Unique Constraint

Create a unique constraint (http://barnamenevis.org/forum/#sql:unique_constraints) to ensure no duplicate values are entered in specific columns (http://barnamenevis.org/forum/#sql:column) that do not participate in a primary key (http://barnamenevis.org/forum/#sql:primary_key__pk_). While both a unique constraint and a primary key enforce uniqueness, you should attach a unique constraint instead of a primary key constraint to a table (http://barnamenevis.org/forum/#sql:table) if:

You want to enforce uniqueness in a column or combination of columns. You can attach multiple unique constraints to a table, whereas you can attach only one primary key constraint to a table.
You want to enforce uniqueness in a column that allows null values. You can attach unique constraints to columns that allow null values, whereas you can attach primary key constraints only to columns that do not allow null values. When you attach a unique constraint to a column allowing null values, you ensure that at most one row will have a null value in the constrained column. To create a unique constraint

In your database diagram, right-click the table that will contain the constraint, then select Properties from the shortcut menu. -or-
Open the Table Designer for the table that will contain the constraint, right-click in the Table Designer, and choose Properties from the shortcut menu.
Choose the Indexes/Keys tab.
Choose New. A system-assigned name appears in the Index name box.
Under Column name, expand the list of columns and select the column that you want to attach the constraint to. To attach the constraint to multiple columns, select the additional columns in subsequent rows.
Select the Create UNIQUE check box.
Select the Constraint option. The unique constraint is created in the database when you save the table or the diagram.
If you are using SQL Server, you can control the sort order of key values and the action taken when duplicate keys exist. To do this, you should create a unique index (http://barnamenevis.org/forum/#sql:index) instead of a unique constraint. For more information, see Creating a Unique Index (http://barnamenevis.org/forum/dvhowcreatinguniqueindex.htm).