t-sql唯⼀约束_SQL唯⼀约束-终极指南
t-sql 唯⼀约束
Hello! Hope you guys are doing well. In this article, we will be unveiling one of the interesting and important topic in — SQL UNIQUE Constraint.
你好! 希望你们⼀切都好。 在本⽂中,我们将揭露有趣且重要的主题之⼀-SQL UNIQUE Con s traint 。
什么是UNIQUE约束? (What is a UNIQUE constraint?)
is a set of rules that are applicable to the schema or the data that is fed to the structure of a database. One such constraint is UNIQUE. The name ‘UNIQUE’ itself speaks about its nature and function.
是⼀组适⽤于架构或馈⼊数据库结构的数据的规则。 这样的约束之⼀就是UNIQUE。 “ UNIQUE”这个名称本⾝就说明了其性质和功能。
SQL UNIQUE constraint enables the column to be free from redundant values. It helps restrict the input values to be unique. Thus, by applying a UNIQUE constraint to a column or a set of columns, we ensure that the data contains no Duplicate entries within it.
SQL UNIQUE约束使列中没有多余的值。 它有助于将输⼊值限制为唯⼀。 因此,通过将UNIQUE约束应⽤于⼀列或⼀组列,我们确保数据中不包含重复项。
Let us understand the importance and functionality of UNIQUE constraint through a real time application/example.
让我们通过实时应⽤程序/⽰例了解UNIQUE约束的重要性和功能。
Consider an online e-learning business portal. The owner of the business portal announces that he would grant free access to Data science courses in the coming time. In order to access the course material, the candidates would have to enter their email address and phone number.
考虑⼀个在线电⼦学习业务门户。 商业门户⽹站的所有者宣布,他将在未来⼀段时间内授予免费访问数据科学课程的权限。 为了访问课程资料,候选⼈必须输⼊其电⼦邮件地址和电话号码。
Now, to make sure that a single candidate receives a single material kit at a time, the database engineer has to make sure that no repeated email ids and phone numbers must be accepted in the database.
现在,要确保单个候选⼈⼀次能收到⼀个材料包,数据库⼯程师必须确保数据库中不再需要重复的电
⼦邮件ID和电话号码。
This is where SQL UNIQUE constraint comes into the picture. Using UNIQUE constraint, no duplicate entries would be allowed. Thus, making sure that a single candidate does not grant access to multiple course material kits.
这是SQL UNIQUE约束出现的地⽅。 使⽤UNIQUE约束,不允许重复条⽬。 因此,确保单个候选⼈不会授予访问多个课程资料包的权限。
Having understood the working of SQL UNIQUE constraint, let us now understand the syntax of the same.
了解了SQL UNIQUE约束的⼯作原理后,现在让我们了解⼀下它的语法。
SQL UNIQUE约束的语法 (Syntax of SQL UNIQUE constraint)
SQL UNIQUE constraint works with the structure as well as the data of the database.
SQL UNIQUE约束适⽤于数据库的结构和数据。
-- Any data definition language command(CREATE/ALTER/DROP)
column-name UNIQUE;
The SQL UNIQUE constraint is applied to one or multiple columns while defining the commands in the table or even can be applied while modifying the table(eg.ALTER command)
SQL UNIQUE约束在表中定义命令时应⽤于⼀列或多列,甚⾄可以在修改表时应⽤(例如ALTER命令)
By applying UNIQUE rule to a column, the database accepts only ‘unique’ values for the specified column.
通过将UNIQUE规则应⽤于列,数据库将仅接受指定列的“唯⼀”值。
通过⽰例实现UNIQUE约束 (Implementing UNIQUE constraint through examples)
We’ll use the SQL UNIQUE constraint in multiple different ways.
我们将以多种不同⽅式使⽤SQL UNIQUE约束。
具有CREATE表命令SQL UNIQUE CONSTRAINT 。 (SQL UNIQUE CONSTRAINT with CREATE table command.)
In this example, we create a table using and define the following columns:
在此⽰例中,我们使⽤创建表并定义以下列:创建唯一约束sql语句
id
ID
acc_no
acc_no
name
名称
CREATE TABLE INFO(
ID INT NOT NULL,
ACC_NO INT UNIQUE,
NAME VARCHAR (20),
PRIMARY KEY (ID)
);
Here, the column ‘id’ is specified as (constraint), which means that the data values passed to this column can never be empty or null. Further, the column ‘ID’ is defined to behave as a PRIMARY KEY. SQL too makes sure that the values passed to the table are non-redundant.
在这⾥,列“ id”被指定为 (约束),这意味着传递给该列的数据值永远不能为空或null。 此外,列“ ID”被定义为充当主键。 SQL 也可以确保传递到表的值是⾮冗余的。
We have set the column ‘acc_no’ to UNIQUE by which the duplicate entries for this column would be forbidden. If we try to enter duplicate values for ‘acc_no’, duplicate value error would be raised.
我们已将“ acc_no”列设置为UNIQUE,通过该列,该列的重复条⽬将被禁⽌。 如果我们尝试为“ acc_no”输⼊重复值,则会出现重复值错误。
We can set UNIQUE constraint for multiple columns of the table as shown below–
我们可以为表的多个列设置UNIQUE约束,如下所⽰–
CREATE TABLE Sales (
ID int NOT NULL,
NAME varchar(255) NOT NULL,
Age int,
Salary INT,
CONSTRAINT Unique_sales UNIQUE (NAME,Age)
);
In the above example, we have set UNIQUE constraint to two columns — ‘NAME’ and ‘Age’ and have provided a constraint name as ‘Unique_sales’.
在上⾯的⽰例中,我们将UNIQUE约束设置为两列-“ NAME”和“ Age”,并将约束名称提供为“ Unique_sales”。
带有ALTER table命令SQL UNIQUE CONSTRAINT (SQL UNIQUE CONSTRAINT with ALTER table command)
At times, if we wish to set the UNIQUE constraint to any data column after defining it, we can club it with SQL Alter table query.
有时,如果我们希望在定义后将UNIQUE约束设置为任何数据列,则可以将其与SQL Alter表查询合并。
query along with UNIQUE constraint helps us modify the rules of the column and set unique rule/constraint to the specified column or columns.
查询以及UNIQUE约束可帮助我们修改列的规则,并为指定的⼀个或多个列设置唯⼀的规则/约束。
Here, we have modified the above-created table – ‘Sales’ and have set Unique constraint to the column ‘Age’.
在这⾥,我们修改了上⾯创建的表“ Sales”,并将“ Unique”约束设置为“ Age”列。
ALTER TABLE Sales
MODIFY Age INT UNIQUE;
带有SQL DROP命令SQL UNIQUE CONSTRAINT (SQL UNIQUE CONSTRAINT with SQL DROP command)
We can remove the rule/constraint from the columns with the help of as shown below-
我们可以借助从列中删除规则/约束,如下所⽰-
ALTER TABLE Sales
DROP CONSTRAINT Unique_sales;
指向突出显⽰! (Point to Highlight!)
As I had mentioned, that Primary key also ensures non-duplicate entries in the database.
如前所述,该主键还可以确保数据库中的条⽬不重复。
Then, why has the need of using UNIQUE constraint arised??
那么,为什么需要使⽤UNIQUE约束呢?
Let me answer this question to the simplest terms.
让我⽤最简单的术语回答这个问题。
SQL Primary Key serves the same purpose (like Unique constraint) of ensuring nonredundant values in the table. But, a table can have a SINGLE primary key while there can be multiple unique constraints applied to various columns of the table.
SQL主键具有确保表中⾮冗余值的相同⽬的(如唯⼀性约束)。 但是, ⼀个表可以有⼀个SINGLE主键,同时可以对表的各个列应⽤多个唯⼀约束 。
结论 (Conclusion)
That’s all for this topic. Please feel free to comment below in case you come across any doubt.
这就是本主题的全部内容。 如果您有任何疑问,请随时在下⾯发表评论。
For more such posts related to SQL, do visit .
有关与SQL有关的更多此类帖⼦,请访问 。
参考资料 (References)
t-sql 唯⼀约束
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论