Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of the only constraint of sql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what the role of the only constraint sql is, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

The role of a unique constraint in SQL is to ensure that there is a unique identity in each record so that there are no two identical record values on the column.

Unique constraint

The only constraint in SQL is to prevent the same two record values in a particular column, that is, it is used to ensure that each record has a unique identity so that there are no duplicate values on the column. The primary key of a table is a uniqueness constraint, but there can be only one primary key, so a uniqueness constraint can be established if the data of other columns is not allowed to be duplicated.

Create a unique constraint

For example, the following SQL creates a new table named CUSTOMERS and adds five columns. Here, the AGE column is set to unique, so no two records can use the same age:

CREATE TABLE CUSTOMERS (ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL UNIQUE, ADDRESS CHAR (25), SALARY DECIMAL (18,2), PRIMARY KEY (ID))

If the CUSTOMERS table has been created, then add a unique constraint to the AGE column, similar to the following declaration:

ALTER TABLE CUSTOMERS;MODIFY AGE INT NOT NULL UNIQUE

You can also use the following syntax, which supports named constraints for multiple columns:

ALTER TABLE CUSTOMERS;ADD CONSTRAINT myUniqueConstraint UNIQUE (AGE, SALARY)

Delete unique constraint

To delete a UNIQUE constraint, use the following SQL:

ALTER TABLE CUSTOMERS;DROP CONSTRAINT myUniqueConstraint

If you are using MySQL, you can use the following syntax:

ALTER TABLE CUSTOMERSDROP INDEX myUniqueConstraint; above is the only constraint of sql what the role of all the content, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report