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,
- oracle date difference | date difference in sql an…
- DATE Data type in Oracle|Oracle Dates and Times|Dates in Oracle
- Formatting date in SQL (Oracle) -Simple date forma…
- Database Schema | Oracle Schema Objects | What is database schema?
- Autonomous Transactions in PL/SQL
- SQL – Transaction Statements -Transaction Manageme…
- Savepoints In SQL Transactions. – SAVEPOINT
- Oracle Tablespace, and Datafiles – Introduction
- Formatting date in SQL (Oracle) -Simple date forma…
- FINDING database objects, finding valid and INVALI…
Oracle ALTER TABLE, ALTER TABLE, primary key, creating primary key, CONSTRAINT
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
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
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.