当前位置:网站首页>MySQL - 唯一索引
MySQL - 唯一索引
2022-07-24 10:25:00 【骆驼胡杨】
唯一索引
所谓唯一索引,就是在创建索引时,限制索引的字段值必须是唯一的。通过该类型的索引可以比普通索引更快速地查询某条记录。
1. 创建表时定义索引
CREATE TABLE tablename( propname1 type1, …… propnamen type..n, UNIQUE INDEX | KEY [indexname] (propnamen [(length)] [ ASC | DESC ] ) );注意:
参数UNIQUE INDEX和UNIQUE KEY是用来指定字段为索引的,两者选择其中之一即可;
参数indexname是索引名字,可省略;
参数propnamen是索引对应的字段的名称,该字段必须为前面定义好的字段且必须定义为 UNIQUE 约束;
参数length是可选参数,其指索引的长度,必须是字符串类型才可以使用;
参数ASC和DESC都是可选参数,ASC表示升序排列,DESC表示降序排列,如果不指定,则为升序。mysql> create table class(id int, name varchar(64), teacher varchar(64), UNIQUE INDEX name_index(name)); //创建class表,同时指定name为唯一索引列 mysql> show create table class; //查询表结构 mysql> insert into class values(1, '一班', '张三'); //插入数据1 mysql> insert into class values(2, '二班', '李四'); //插入数据2 mysql> select * from class where id>0; //根据列查询
2. 已存在的表上创建索引
方法一:CREATE UNIQUE INDEX indexname ON tablename (propname [(length)] [ASC|DESC]);
方法二:
ALTER TABLE tablename ADD UNIQUE INDEX | KEY indexname (propname [(length)] [ASC|DESC]);
边栏推荐
- zoj 2770 差分约束系统---2--2022年5月20日
- ES6 question
- Onpropertychange event "suggestions collection"
- Wechat applet
- This usage, SystemVerilog syntax
- Sentinel 三种流控模式
- The concept and representation of a tree
- PostgreSQL rounding
- Curse of knowledge
- 757. Set the intersection size to at least 2: greedy application question
猜你喜欢

Uniapp calendar component
![[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display](/img/f2/6fcd4b2e747b4ceb52a52eda0c1af4.png)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display

New:Bryntum Grid 5.1.0 Crack
![[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)](/img/6e/b7cf7a8e3296e29d61a0626e3793ea.png)
[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)

String__
![[STM32 learning] (14) two 74HC595 controls four nixie tube displays](/img/22/c83e29bead8e6298a0a3564419022c.png)
[STM32 learning] (14) two 74HC595 controls four nixie tube displays

Nirvana rebirth! Byte Daniel recommends a large distributed manual, and the Phoenix architecture makes you become a God in fire

Simply use golang SQLC to generate MySQL query code

NiO knowledge points

The paper of gaojingjian center was selected into the ACL 2022 of the international summit to further expand the privacy computing capacity of Chang'an chain
随机推荐
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
MySQL - 更新表中的数据记录
聚集日志服务器
Configuration description and componentization development steps of various documents in the scaffold
OSPF含特殊区域实验,MGRE构建和重发布
Kotlin advanced
What is NFT? How to develop NFT system?
[STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
ffmpeg花屏解决(修改源码,丢弃不完整帧)
Add a love power logo to your website
Gaode map
常量指针、指针常量
第五章 修改实现(IMPL)类
Scan line, weight segment tree
Looting (leetcode-198)
Sentinel 三种流控效果
pom文件dependency中的 scope用法
Will not be rejected! Learn the distributed architecture notes sorted out by Alibaba Daniel in 35 days, with a salary increase of 20K
Detailed explanation of uninstalling MySQL completely under Linux
[STM32 learning] (7) use of serial port 2 (usart2)