Oracle中主键约束跟唯一索引之间的关联关系
在Oracle中,可以在创建主键约束的时候自动创建唯一索引,也可以先创建唯一索引,然后再基于这个唯一索引来创建主键约束。后一种
在Oracle中,可以在创建主键约束的时候自动创建唯一索引,也可以先创建唯一索引,然后再基于这个唯一索引来创建主键约束。后一种方式有一个好处,在 需要对数据量比较大而且读写频繁的OLTP表创建主键约束的时候,可以先ONLINE的创建一个唯一的索引,然后再创建主键约束,这样可以减少对表的读写 阻塞。但这样就带来一个问题,第一种方式创建的索引在删除约束的时候索引会被自动删除,而第二种方式创建的索引在删除约束的时候不会自动删除,,需要删完约 束删索引,如果忘记了这个唯一索引的话,可能会带来跟想象不一样的结果。同时,Oracle针对这种情况提供了特殊的删除约束的方法,可以同时删除约束和 索引,就是:alter table bear drop constraint pk_bear drop index。在删除约束的最后加上删除索引的关键字。
但写这一大堆都不是这里要重点描述的内容,这里要写的是怎么知道已经创建好的索引到底是第一种方式创建的?还是第二种方式创建的呢?
在Oracle的SYS.IND$视图中有一个叫PROPERTY的字段,里面记录的就是每个索引对应的属性,这个字段的含义是在创建这个表的SQL中有定义的。在10G的版本中,可以到$Oracle_HOME/rdbms/admin/sql.bsp中查找ind$表的创建脚本;在11G的版本中,可以到相同的位置的dcore.bsp中查找。这些脚本都是创建系统核心表的脚本,很多字段在官方文档中没有注释的,可以来这里找找看。在11G中对PROPERTY字段的注释如下:
property number not null, /* immutable flags for life of the index */
/* unique : 0x01 */
/* partitioned : 0x02 */
/* reverse : 0x04 */
/* compressed : 0x08 */
/* functional : 0x10 */
/* temporary table index: 0x20 */
/* session-specific temporary table index: 0x40 */
/* index on embedded adt: 0x80 */
/* user said to check max length at runtime: 0x0100 */
/* domain index on IOT: 0x0200 */
/* join index : 0x0400 */
/* system managed domain index : 0x0800 */
/* The index was created by a constraint : 0x1000 */
/* The index was created by create MV : 0x2000 */
/* composite domain index : 0x8000 */
这个是典型的Oracle的表示方法,其中每个值表示一个含义,但是多个值是可以累加起来表示多个含义的。比如一个UNIQUE的REVERSE的索引,对应的值就会是0X05,PROPERTY中对应的是10进制的存放,就应该也是5。
可以用下面的SQL来查询主键跟索引的关系是第一种还是第二种:
SELECT DECODE(BITAND(PROPERTY, 4096), 4096, 'implicit', 'user-generated') GENERATION,
B.INDEX_NAME
FROM SYS.IND$ A, USER_INDEXES B, USER_OBJECTS C
WHERE B.UNIQUENESS = 'UNIQUE'
AND A.OBJ# = C.OBJECT_ID
AND B.INDEX_NAME = C.OBJECT_NAME
AND B.INDEX_NAME = 'INDEX_NAME';
上面的SQL就是说如果PROPERTY为4096,那么对应到16进制应该是0X1000,也就表示The index was created by a constraint,也就是第一种;其他都是第二种,这种情况下典型的值就是4097,也就是0X1000和0X01值的和。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
