Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. --let us create table with 2 constraints ( one primary and another unique)
  2.  
  3. DROP TABLE EMP_IND;
  4.  
  5. CREATE TABLE EMP_IND
  6. ( EMPNO NUMBER CONSTRAINT EMP_IND_PK PRIMARY KEY,
  7. ENAME VARCHAR2(100) UNIQUE,
  8. NICKNAME VARCHAR2(100),
  9. email varchar2(100)
  10. );
  11.  
  12. INSERT INTO EMP_IND (EMPNO,ENAME,NICKNAME,EMAIL)
  13. VALUES ('1','Ahmed Samer','Ahmed.Samer','Ahmed.Samer@gmail.com');
  14. INSERT INTO EMP_IND (EMPNO,ENAME,NICKNAME,EMAIL)
  15. VALUES ('2','Rami Nader','Rami.Nader','Rami.Nader@hotmail.com');
  16. INSERT INTO EMP_IND (EMPNO,ENAME,NICKNAME,EMAIL)
  17. VALUES ('3','Khaled Ali','Khaled.Ali','Khaled.Ali@hotmail.com');
  18. INSERT INTO EMP_IND (EMPNO,ENAME,NICKNAME,EMAIL)
  19. VALUES ('4','Hassan Nabil','Hassan.Nabil','Hassan.Nabil@yahoo.com');
  20. COMMIT;
  21.  
  22.  
  23. --the oracle create implicit UNIQUE indexes for the PK, UK and the name for
  24. --the index will be same the name of constraint name
  25.  
  26.  
  27. SELECT * FROM USER_INDEXES
  28. WHERE TABLE_NAME='EMP_IND';
  29.  
  30. SELECT * FROM USER_IND_COLUMNS
  31. WHERE TABLE_NAME='EMP_IND';
  32.  
  33. --now the oracle will use the index in the where clause to speed the query
  34.  
  35. SELECT * FROM
  36. EMP_IND
  37. WHERE EMPNO=1; --you will see that oracle use the index in the explain plan
  38.  
  39. SELECT * FROM
  40. EMP_IND
  41. WHERE ename='Ahmed Samer';--you will see that oracle use the index in the explain plan
  42.  
  43. SELECT * FROM
  44. EMP_IND
  45. WHERE NICKNAME='Ahmed.Samer'; --no index on LNAME so the the oracle will make full scan on the table
  46.  
  47. CREATE INDEX EMP_IND_NICKNAME ON EMP_IND (NICKNAME);
  48.  
  49. SELECT * FROM USER_INDEXES
  50. WHERE TABLE_NAME='EMP_IND';
  51.  
  52. --now the server will use the index for NICKNAME in the where clause
  53. SELECT * FROM
  54. EMP_IND
  55. WHERE NICKNAME='Ahmed.Samer';
  56.  
  57. ---now you can create unique index for email, but it is better to add unique constraint
  58.  
  59. CREATE UNIQUE INDEX EMP_IND_EMAIL ON EMP_IND (EMAIL);
  60.  
  61. --now if you try to insert existing email then you will see error like constraint
  62. INSERT INTO EMP_IND (EMPNO,ENAME,NICKNAME,EMAIL)
  63. VALUES ('10','karem Samer','Ahmed.Samer','Ahmed.Samer@gmail.com');
  64.  
  65. --also you can create another index for ENAME column, but using function-based index upper(ENAME)
  66.  
  67. SELECT * FROM
  68. EMP_IND
  69. WHERE upper(ename)='AHMED SAMER';
  70.  
  71. CREATE INDEX EMP_IND_UP_ENAME ON EMP_IND (UPPER(ENAME));
  72.  
  73. SELECT * FROM USER_INDEXES
  74. WHERE TABLE_NAME='EMP_IND';
  75.  
  76. SELECT * FROM USER_IND_COLUMNS
  77. WHERE TABLE_NAME='EMP_IND';
  78.  
  79. SELECT * FROM USER_IND_EXPRESSIONS
  80. WHERE TABLE_NAME='EMP_IND';
  81.  
  82. SELECT * FROM
  83. EMP_IND
  84. WHERE UPPER(ENAME)='AHMED SAMER';
  85. ------------------------------------------------------------------
  86.  
  87. --naming the index while creating the table
  88. DROP TABLE EMP_IND1;
  89.  
  90. CREATE TABLE EMP_IND1
  91. ( EMPNO NUMBER CONSTRAINT EMP_IND1_PK PRIMARY KEY USING INDEX
  92. (create index EMP_IND1_ind on EMP_IND1 (EMPNO) ),
  93. FNAME VARCHAR2(100),
  94. lname VARCHAR2(100),
  95. EMAIL VARCHAR2(100),
  96. gender char(1)
  97. );
  98.  
  99. --you can create index of composit columns
  100. CREATE INDEX EMP_IND1_COMP ON EMP_IND1 (FNAME,LNAME);
  101.  
  102. SELECT * FROM USER_INDEXES
  103. WHERE TABLE_NAME='EMP_IND1';
  104.  
  105. SELECT * FROM USER_IND_COLUMNS
  106. WHERE TABLE_NAME='EMP_IND1';
  107.  
  108. --you can create index with type bitmap
  109.  
  110. CREATE BITMAP INDEX EMP_IND_b ON EMP_IND1 (GENDER); --but this feature in the Enterprise Edition
  111.  
  112. ---finaly you can drop the index
  113.  
  114. DROP INDEX EMP_IND1_COMP;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement