我将如何创建“多项选择”本地数据库中的列? [英] How would I create "multiple choice" columns in Local Database?
问题描述
我使用本地数据库第一次与我的WPF项目。我有数据库设置,我连接好的ETC。治疗一些列,我想成为多个选择,在几个值之间或整个值的价值。问题很明显是人为错误会在输入数据时产生拼写错误。
I am using local database for first time with my WPF project. I have the database setup, and I am connecting fine ETC. Ther eare some columns which I want to be multiple choice, either between a few values or a whole bunch of values. Problem is obviously human error will make typos now and then when inputting the data.
如何让数据输入为用户提供多重选择?例如,我有一个名为类别的列,并且目前(这将在以后扩展)我只想允许以下选项:
Bronze
Misc
How would I go about making the data entry give the user a multiple choice? So for example, I have a column called "Category", and at the moment (this will be expanded later) I only want to allow the following options: Bronze Misc
我目前有列设置为nvarchar(50),但是手动输入相同的字符串...不是我想做的TBH ...所以...我可以设置它是否有一个预定义值的列表,它会接受? :)
I have the columns set to nvarchar(50) at present, but typing the same string manually constantly... not what I would like to be doing TBH... so... Could I set it so that there are a list of predefined values it will accept? :)
感谢:D
推荐答案
您的表列上的任何复杂性。 在这里查看MSDN
So您的表定义将是:
You can use CHECK constraint of any complexity on your table column(s). Check MSDN here So your table definition would be as:
CREATE TABLE T
(
Category nvarchar(50) CHECK (Category in ('Bronze','Misc'))
)
将来可能更改的可能值列表,并且您不想更改表定义,则可以使用值列表创建一个单独的表,并使用外键。
If you expect your list of possible values to change in the future and you do not want to change a table definition, you can create a separate table with the list of values and use the foreign key.
CREATE TABLE Categories
(
Id int PRIMARY KEY,
CategoryName nvarchar(50)
)
INSERT INTO Categories VALUES (1, 'Bronze'), (2, 'Silver'), (3, 'Misc')
CREATE TABLE T
(
CategoryId int REFERENCES Categories
)
这篇关于我将如何创建“多项选择”本地数据库中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!