create table oracle primary key code example

Example 1: add primary key constraint in oracle

-- Adding Using alter
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);

Example 2: drop primary key oracle

-- Dropping Using alter
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;

Example 3: identify primary key in oracle table

-- syntax:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '<table-name>' -- Replace <table-name> with your table-name
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner


-- example:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'CUSTOMERS' 
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner

Tags:

Sql Example