The use of mysql unique key in query and related issues

The use of mysql unique key in query and related issues

1. Create table statement:

CREATE TABLE `employees` (
 `emp_no` int(11) NOT NULL,
 `birth_date` date NOT NULL,
 `first_name` varchar(14) NOT NULL,
 `last_name` varchar(16) NOT NULL,
 `gender` char(1) NOT NULL,
 `hire_date` date NOT NULL,
 PRIMARY KEY (`emp_no`),
 UNIQUE KEY `idxunique_first_name_gender` (`first_name`,`gender`),
 UNIQUE KEY `uniq_idx_firstname` (`first_name`,`gender`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2. Add unique key:

-- Add a unique index alter table employees add constraint idxunique_first_name_gender unique(first_name, gender);

3. Query test statement:

-- Include all fields in the index, and the order is consistent with the index select * from employees where first_name='Chirstian' and gender='M';
-- key column value: idxunique_first_name_gender
-- Include all fields in the index, but the order and index are inconsistent select * from employees where gender='M' and first_name='Chirstian';
-- key column value: idxunique_first_name_gender
-- Include index on first field select * from employees where first_name='Chirstian';
-- key column value: idxunique_first_name_gender
-- Include non-first fields in the index select * from employees where gender='M';
-- key column value: NULL

4. When querying, if the where condition contains only columns (part or all) in the unique key and the column value type is const. If you want to use a unique key, the where clause must contain the first column when creating the unique key. Other columns are optional.

A question related to mysql unique key

The user_id of xxx_tab is a unique key. Let’s take a look at the pseudo code:

get user_id=10000 from xxx_tab;
if exist 
{
  return ok
}
ret = insert user_id=10000
if ret ok 
{
  return ok
}
return not_ok

Question: Why is the final result not_ok?

Tip: Distributed Impact

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Solve the problem of SQL Server saving object string conversion into uniqueidentifier failure
  • Explanation of MySQL index types Normal, Unique and Full Text
  • MySQL uses UNIQUE to implement non-duplicate data insertion
  • How to add and delete unique indexes for fields in MySQL
  • How to solve the problem of inserting duplicate values ​​into unique columns in Mysql
  • SQL statement to convert uniqueidentifier into varchar data type
  • Detailed explanation of the principles and applications of Unique SQL

<<:  Three solutions for sub-functions accessing external variables in JavaScript

>>:  Tutorial on installing and configuring Tomcat on Alibaba Cloud Server and adding external network access ports

Recommend

Detailed explanation of CSS animation attribute keyframes

How long has it been since I updated my column? H...

The meaning and calculation method of QPS and TPS of MySQL database

When doing DB benchmark testing, qps and tps are ...

Solution to slow response of Tomcat server

1. Analytical thinking 1. Eliminate the machine&#...

Use JS to zoom in and out when you put the mouse on the image

Use JS to zoom in and out when the mouse is on th...

Things to note when designing web pages for small-screen mobile devices

The reason is that this type of web page originate...

How to start multiple MySQL instances in CentOS 7.0 (mysql-5.7.21)

Configuration Instructions Linux system: CentOS-7...

Steps and pitfalls of upgrading linux mysql5.5 to mysql5.7

Table of contents Linux MySQL 5.5 upgraded to MyS...

Analysis of multi-threaded programming examples under Linux

1 Introduction Thread technology was proposed as ...

Function overloading in TypeScript

Table of contents 1. Function signature 2. Functi...

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

Why is UTF-8 not recommended in MySQL?

I recently encountered a bug where I was trying t...

A simple method to merge and remove duplicate MySQL tables

Scenario: The crawled data generates a data table...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...