Unique Indexes in MySQL and Other Databases
2014-05-18 07:29
344 查看
If you’ve been using databases for a while, you’ve probably set a primary key in most of your tables. A primary key is a unique identifier for each record, e.g.
In this example, the ‘id’ column is our primary key. When we
if we don’t specify an id, an
the highest existing id.
Presume you’ve added the following data:
then issue the following
The database would refuse to add a new record because one already exists with an id of 1. Fortunately, we can omit the id from our
automatically generate one:
We now have four records:
We can add almost 17 million records before the id goes out of range.
Great — except that records 1 and 4 are identical. What if we want to ensure all phone numbers are unique?
Unique indexes work in much the same way as a primary key. Although you can only have one primary key, any number of unique indexes can be created with any number of fields.
In our example, we want to ensure no two records have the same country, area, number and extension. We can do this by altering our table:
Note that the index name ‘ix_phone’ is optional. Alternatively, we could re-create our table:
Most databases support unique indexes but the SQL syntax may differ.
Let’s try inserting a duplicate record even though we’re not specifying an id:
The following error will be generated if you’re using MySQL:
If you’re using almost any database, you can guarantee your phone records are unique no matter how the data is inserted.
I say almost any database because MySQL has an odd quirk. NULL is treated as a unique value — which is why you cannot use comparisons such as
indexes and no logic has been implemented to fix it.
We can execute our original
time because the extension field defaults to NULL and is considered to be unique:
Yes, it’s insane. I’m not aware of the problem in other databases and even MySQL works as expected if you’re using the BDB storage engine. It’s been reported as a MySQL
bug but there are no known plans to fix it.
The solution: ensure all fields defined in a unique index cannot be set to NULL. In this example, we could indicate that there’s no extension number by setting a value such as 0 or 99999. Or perhaps we could make the
field a signed number and set -1. It’s horrible, but it’ll work.
Despite this issue, unique indexes are useful in many situations and help you to retain data integrity when other programmers and users are not so conscientious!
http://www.sitepoint.com/use-unique-indexes-mysql-databases/
INSERTtelephone numbers,
if we don’t specify an id, an
AUTO_INCREMENTnumber will be generated by adding one to
the highest existing id.
Presume you’ve added the following data:
id | country | area | number | extension |
---|---|---|---|---|
1 | 1 | 234 | 567890 | NULL |
2 | 44 | 9876 | 54321 | 42 |
3 | 61 | 3 | 90908200 | NULL |
INSERT:
INSERTto
automatically generate one:
id | country | area | number | extension |
---|---|---|---|---|
1 | 1 | 234 | 567890 | NULL |
2 | 44 | 9876 | 54321 | 42 |
3 | 61 | 3 | 90908200 | NULL |
4 | 1 | 234 | 567890 | NULL |
Great — except that records 1 and 4 are identical. What if we want to ensure all phone numbers are unique?
Unique Indexes
Unique indexes work in much the same way as a primary key. Although you can only have one primary key, any number of unique indexes can be created with any number of fields.In our example, we want to ensure no two records have the same country, area, number and extension. We can do this by altering our table:
Let’s try inserting a duplicate record even though we’re not specifying an id:
MySQL NULLs
I say almost any database because MySQL has an odd quirk. NULL is treated as a unique value — which is why you cannot use comparisons such as value = NULLand need to use
value IS NULL. Unfortunately, this also affects unique
indexes and no logic has been implemented to fix it.
We can execute our original
INSERTmultiple times and a new record will be created each
time because the extension field defaults to NULL and is considered to be unique:
bug but there are no known plans to fix it.
The solution: ensure all fields defined in a unique index cannot be set to NULL. In this example, we could indicate that there’s no extension number by setting a value such as 0 or 99999. Or perhaps we could make the
field a signed number and set -1. It’s horrible, but it’ll work.
Despite this issue, unique indexes are useful in many situations and help you to retain data integrity when other programmers and users are not so conscientious!
http://www.sitepoint.com/use-unique-indexes-mysql-databases/
相关文章推荐
- Understanding JOINs in MySQL and Other Relational Databases 理解JOIN在MySQL和其他数据库中的运用
- Trees and Other Hierarchies in MySQL
- Difference between Key, Primary Key, Unique Key and Index in MySQL
- Adding support for MrSid and other raster data formats in GeoServer
- How to install LAMP (Apache, PHP and MySQL in Linux) using Yum
- Sams teach yourself PHP, MySQL and Apache in 24 hours
- Joe Celko's Data and Databases: Concepts in Practice
- [转]各种分页 in SQL Server 2005, SQL Server 2000, MS Access and MySQL
- notes about crosstool,uboot,mysql and some tools in linux(ubuntu)
- SQL SELECT TOP N equivalent in ORACLE and MySQL
- SQL SELECT TOP N equivalent in ORACLE and MySQL
- Delphi Interface, implementation, uses and other keywords explained in easy language!
- Units in QReport and Other summary(关于QReport中的Unit)
- Defense in Depth using OSSEC and other free tools
- Defense in Depth using OSSEC and other free tools
- VARCHAR and CHAR differences in mysql
- SQL SELECT TOP N equivalent in ORACLE and MySQL
- create a first user and a first database for him in mysql
- Participate in MySQL-GlassFish Student Contest and Win $500
- Benchmarking and Profiling in MySQL