Oracle 添加主鍵和索引

來源:互聯網
上載者:User

資料的主鍵和索引一般情況下都是必須的,特別是表有大量資料的時候,索引和主鍵更是必不可少,這樣可以提供資料的查詢效率;

一、建立表的同時建立主鍵約束

(1)無命名

create table student (

studentid int primary key not null,

studentname varchar(8),

age int);

(2)有命名

create table students (

   studentid int ,

   studentname varchar(8),

   age int,

   constraint yy primary key(studentid));

2、刪除表中已有的主鍵約束

(1)有命名

alter table students drop constraint yy;

(2)無命名

可用 SELECT   *   from   user_cons_columns;

尋找表中主鍵名稱得student表中的主鍵名為SYS_C002715

alter table student drop constraint SYS_C002715;

3、向表中添加主鍵約束

alter table student add constraint pk_student primary key(studentid);

3、當建立主鍵時,本來主鍵列有資料,而且有重複的:此時必須先重複資料刪除資料再建立:

  delete  from Tb_TEST       a where rowid !=(select max(rowid)      from Tb_TEst     b where a.test_ID=b.test_ID  );

二、建立索引

create Index IX_xxxx   on table_name (colummname);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.