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:
|
<<: Three solutions for sub-functions accessing external variables in JavaScript
How long has it been since I updated my column? H...
When doing DB benchmark testing, qps and tps are ...
1. Analytical thinking 1. Eliminate the machine...
Use JS to zoom in and out when the mouse is on th...
The reason is that this type of web page originate...
Configuration Instructions Linux system: CentOS-7...
Table of contents Linux MySQL 5.5 upgraded to MyS...
Preface When we installed the system, we did not ...
1 Introduction Thread technology was proposed as ...
Table of contents 1. Function signature 2. Functi...
Do you know what fonts are used in the logo desig...
I recently encountered a bug where I was trying t...
Table of contents Current Issues Solution process...
Scenario: The crawled data generates a data table...
1. Software Introduction VirtualBox VirtualBox is...