Composite primary key is a special type of primary key comprises a set of columns.In relational database a primary key is a candidate key to uniquely identify each row in a table.
A unique key or primary key comprises a single column or set of columns (COMPOSITE Primary KEY).
No two distinct rows in a table can have the same value or combination of values(if it is composite primary key) in those columns.
Primary keys are defined through the PRIMARY KEY constraint (ANSI SQL Standard).
The examples given below explains both Primary key and Composite primary keys.
The below given example uses CREATE TABLE statement to create a table with a single column primary key.
SQL> create table MYTABLE( name VARCHAR2(50), id NUMBER, salary NUMBER(8,2), CONSTRAINT MYTABLE_ID PRIMARY KEY (id)); Table created
Now let us INSERT few records into MYTABLE.
SQL> insert into MYTABLE values ('CCC',1,2548.21); SQL> insert into MYTABLE values ('ADS',2,3548.21); SQL> insert into MYTABLE values ('GDS',2,1548.21); SQL> select * from MYTABLE ORDER BY SALARY; NAME ID SALARY GDS 2 1548.21 CCC 1 2548.21 ADS 2 3548.21
The below given example uses CREATE TABLE statement to create a table with a multiple column primary key (COMPOSITE Primary KEY).
SQL> drop table mytable; Table dropped SQL> create table MYTABLE( name VARCHAR2(50), id NUMBER, salary NUMBER(8,2), CONSTRAINT MYTABLE_NAME_ID_PK PRIMARY KEY (name,id)); Table created
Now let us INSERT few records into MYTABLE.
SQL> insert into MYTABLE values ('CCC',1,2548.21); SQL> insert into MYTABLE values ('ADS',2,3548.21); SQL> insert into MYTABLE values ('GDS',2,1548.21); SQL> select * from MYTABLE ORDER BY SALARY; NAME ID SALARY GDS 2 1548.21 CCC 1 2548.21 ADS 2 3548.21
Related Articles,Oracle/PLSQL: Primary Key and Composite Primary Key
ALTER TABLE to ADD PRIMARY KEY in Oracle.
SQL MIN Function | MIN() function in SQL
- SQL COUNT() Function ,Usage of COUNT(*) and COUNT(1)
- SQL order by ,using ‘ORDER BY’ clause in SQL
- Oracle Max(Date)? | sql max date
- Oracle Trim Function | PL/SQL TRIM Function.
- Oracle Decode Function | Use PLSQL DECODE function to handle NULL and default values
- SQL: MAX Function | SQL Max examples
- SQL GROUP BY Statement | SQL GROUP BY Clause examples
- NVL | Oracle/PLSQL: NVL Function
Technorati Tags:
Oracle Primary Key, Composite Primary Key, Primary key
Pingback: Oracle Tables:Create Table statement.How to create tables? | SQL and PLSQL
Pingback: Oracle tables: Primary Key and Composite Primary Key | SQL and PLSQL
Pingback: Oracle PL/SQL:CREATE TABLE statement: create a table with primary key. | SQL and PLSQL
Pingback: Oracle PL/SQL:Create Table statement | SQL and PLSQL
There is one mistake, single row primary keyed example table mentioned above will not allow same data to be inserted twice in primary column.So please give note on that.
Composite Primary key also does not allowed DUPLICATE values.
Waste artical, dnt post if u dnt know anything…..
Dont post confusing others without explaining anything. Better stay silent