ALTER TABLE to ADD PRIMARY KEY in Oracle| Oracle ALTER TABLE

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Buffer 0 Email -- Filament.io 0 Flares ×

Primary key is a type of constraint used in Oracle tables.A primary key is used to uniquely identify each row in a table.A primary key can consist of one or more fields on a table. When more than one fields are used as a primary key, the key is called a composite key.

You can create primary keys in two ways using CREATE TABLE and ALTER TABLE statements.

1.when the table is created using CREATE TABLE statement

Step 1:

SQL> CREATE TABLE Mytable
(id number ,
 name varchar2(100),
 msg varchar2(500),
 CONSTRAINT    PK_MYTABLE    PRIMARY KEY (id ));

The above script will create a table names Mytable with primary key Mytable_id_pk using column id.

2.By changing existing table structure using ALTER TABLE statement.

Example

Step 1:
Create a table using the following script.

SQL> create table Mytable (name varchar2(100), msg varchar2(500), id number);

This will create a table named Mytable with three columns.

Step 2:

-- Create/Recreate primary, unique and foreign key constraints
SQL> alter table MYTABLE
  add constraint PK_MYTABLE primary key (ID);

This will ALTER the TABLE to create primary key PK_MYTABLE using column id.

Also Read,

, , , ,

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Buffer 0 Email -- Filament.io 0 Flares ×

5 thoughts on “ALTER TABLE to ADD PRIMARY KEY in Oracle| Oracle ALTER TABLE

  1. Anonymous

    I found this site using [url=http://google.com]google.com[/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

    Sorry for offtopic

    Reply
  2. Pingback: Oracle 'ALTER TABLE' command to ADD columns syntax | SQL and PLSQL

  3. Pingback: oracle to_date | Oracle/PLSQL: To_Date Function | SQL and PLSQL

  4. ANGELA PIERCE

    Hi good blog yea nice job our review blog will soon be adding reviews on blogs and add them to our blogs as the top best 50 blogs to visit we also do reviews on Customer Reviews all types of reviews we will get back to you

    Reply
  5. Ghafar Ali Khan

    how we change the non key atteribute to primary key atteribute in oracle 10g.
    NOTE: the method mentioned above is not vaild. if vaild then why not impliment truly, because i have applied.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Paged comment generated by AJAX Comment Page