表結構javascript
- 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
給resource_name和resource_type添加聯合惟一約束html
- ALTER TABLE jw_resource
- ADD UNIQUE KEY(resource_name, resource_type);
結果 show create table jw_resource;java
- 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
刪除惟一約束mysql
- ALTER TABLE jw_role DROP INDEX resource_name;
增長一個惟一約束,而且給惟一約束起名字?sql
原表結構:spa
- mysql> show create table test_table;
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- ---------------------------------+
- | Table | Create Table
- |
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- ---------------------------------+
- | test_table | CREATE TABLE `test_table` (
- `id` int(50) NOT NULL AUTO_INCREMENT,
- `kk` varchar(30) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- ---------------------------------+
- 1 row in set (0.00 sec)
增長惟一約束的語句,給惟一約束起名叫作 kk_uq:.net
- mysql> alter table test_table add unique key `kk_uq` ( kk);
查看結果xml
- mysql> show create table test_table;
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- --------------------------------------------------------------+
- | Table | Create Table
- |
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- --------------------------------------------------------------+
- | test_table | CREATE TABLE `test_table` (
- `id` int(50) NOT NULL AUTO_INCREMENT,
- `kk` varchar(30) DEFAULT NULL,
- PRIMARY KEY (`id`),
- UNIQUE KEY `kk_uq` (`kk`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
- +------------+--------------------------------------------------------------------------------------------------------------------------------------------
- --------------------------------------------------------------+
- 1 row in set (0.00 sec)