如何使用mssql将列作为唯一键? [英] how to make a column as unique key, using mssql ?

查看:221
本文介绍了如何使用mssql将列作为唯一键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好......

im制作在线学生入学表格。

现在在数据库表中使用asp.net中的ms-sql ...

学生ID被设置为主键,

现在在我的网页上,当学生想要创建他的用户名时,

i想要用户名不应该同桌排......

u知道我不能把两列作为主键..



所以我听说,我们可以使用唯一键来停止在表格行中输入相同的用户名...



如何在ms-sql中设置一个唯一键...

如果我可以得到一个任务,那也不错...



提前感谢...

hello...
i m making online student addmission form.
now in database table using ms-sql in asp.net...
"student id" is set as "primary key",
now in my web page when, a student want to create his username,
i want the username should not same in row of table...
u know i cant make two columns as "primary key"..

so i hear that, we can use "unique key" to stop entry same username in table row...

how to set a unique key in ms-sql...
if i can get a queary , that is also good...

thanks in advance...

推荐答案

我想你正在寻找 UNIQUE约束 [ ^ ]。
I suppose you are looking for the UNIQUE constraint[^].


您可以尝试包含这在你的控制器中:





尝试{



var DupeCheck = context.Students.Where(x => x.UserName == model.UserName);

if(DupeCheck .Count()> 0)

{

返回Json(new {Result =ERROR,Message =UserName已经存在!});

}



//其他方法

//在您的控制器中



}



catch(Exeption ex)

{



}
You can try to include this in your controller:


try {

var DupeCheck = context.Students.Where(x => x.UserName == model.UserName);
if (DupeCheck .Count() > 0 )
{
return Json(new { Result = "ERROR", Message = "UserName Already Exists!" });
}

//other methods
//in your controller

}

catch(Exeption ex)
{

}


在SQL管理控制台中连接到您的数据库,然后展开它和表节点,继续展开表并右键单击索引文件夹,然后选择新建索引。接下来单击索引键列上的add,直到获得所有这些列,然后单击确定。
In SQL Management Console connect to you database, then expand it and the tables node, continue to expand your table and the rightclick on the indexes folder and choose New Index. Next click add on the index key columns until you've got all of them, then click ok.


这篇关于如何使用mssql将列作为唯一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆