• Sharebar

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,

, , , ,

 
Be Sociable, Share!
  • 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

  • Pingback: Oracle 'ALTER TABLE' command to ADD columns syntax | SQL and PLSQL

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

  • http://reviewsnewspaper.com 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

  • 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.

SEO Powered by Platinum SEO from Techblissonline