Add a unique constraint to a table using myql

Source: Internet
Author: User

Table Structure

FIELD          TYPE          COLLATION       NULL    KEY     DEFAULT  Extra           PRIVILEGES            COMMENT-------------  ------------  --------------  ------  ------  -------  --------------  --------------------  -------id             BIGINT(20)    (NULL)          NO      PRI     (NULL)   AUTO_INCREMENT  SELECT,INSERT,UPDATE         resource_name  VARCHAR(128)  gbk_chinese_ci  YES             (NULL)                   SELECT,INSERT,UPDATE         resource_type  TINYINT(4)    (NULL)          YES             (NULL)                   SELECT,INSERT,UPDATE 

Add a unique constraint for resource_name and resource_type

ALTER TABLE jw_resourceADD UNIQUE KEY(resource_name, resource_type);

Result: Show create table resource_resource;

CREATE TABLE `jw_resource` (  `id` BIGINT(20) NOT NULL AUTO_INCREMENT,  `resource_name` VARCHAR(128) DEFAULT NULL,  `resource_type` TINYINT(4) DEFAULT NULL,  PRIMARY KEY (`id`),  UNIQUE KEY `resource_name` (`resource_name`,`resource_type`)) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=gbk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.