开发者

Table without a identity column but this column can't have repeated values

开发者 https://www.devze.com 2023-03-06 23:45 出处:网络
I\'m creating a table that needs to have 2 columns. The first column can\'t be repeated. The thing is, I will insert the value of the first column. How do I create this column?

I'm creating a table that needs to have 2 columns. The first column can't be repeated. The thing is, I will insert the value of the first column. How do I create this column? SQLSer开发者_高级运维ver 2005


Make the first column the primary key of the table.


Set the column as a primary key. I doesn't have to be an identity column to has a PK.


Create it the same way you would any other column: create table sometable (column1 varchar(10), column2 varchar(20)) or whatever.

Do you mean: How can you get the database to force it to be unique? Either declare it to be the primary key, or create a unique index on the column.

Perhaps you're thinking that a primary key must be auto-generated? There's no such rule. Whether you invent the value yourself or use an autonumber feature has nothing to do with whether a field can be a primary key.


Why not just put a unique constraint on it:

ALTER TABLE <table_name>
 ADD CONSTRAINT <constraint_name>
 UNIQUE(<column_name>)
0

精彩评论

暂无评论...
验证码 换一张
取 消