Oracle's primary KEY (Primary key) usage detailed

Source: Internet
Author: User

Oracle/plsql: Primary KEY (Primary key) usage

1 Goals

Examples explain how to create, delete, disable, and turn on primary keys.


2 Preface-What is the primary key

In Oracle, a primary key refers to a single data table column or union data table column (Union PRIMARY key | Composite primary key) that uniquely identifies a record. The Data table column data used by the primary key cannot contain null values. Also, a table can contain only a single primary key.

Description: In an Oracle database, there cannot be more than 32 columns for a federated primary key. The primary key can be defined when the table is created or by the ALTER TABLE syntax.


3 Creating a primary key-defining a primary key when creating a table

Example single-column primary key:

CREATE TABLE tb_pk_example (  ID number,  NAME varchar2 (),  DESCRIPTION varchar2,  CONSTRAINT tb_ PK_EXAMPLE_PK PRIMARY Key (ID)--Define primary key);

Example of Federated primary key:

CREATE TABLE tb_supplier_ex (  supplier_id number,  supplier_name varchar2 (),  supplier_description VARCHAR2 (+),  contact_name varchar2 (+),  constraint TB_SUPPLIER_EX_PK primary key (supplier_id, Supplier_ Name)--Federated primary key);

4 Creating a primary key-using the ALTER TABLE syntax

Grammar

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (col1, col2,... coln);

Sample preparation

Create two tables (Tb_employees and tb_departments) with the following script:

CREATE TABLE Tb_employees (employee_id number,employee_name varchar2 (), Employee_age Number,employee_birth date, department_id number); Create Table tb_departments (department_id number,department_name varchar2 (+), location VARCHAR2 (300));

Create a primary key with the ALTER TABLE syntax:

--single-column primary key ALTER TABLE tb_employees ADD constraint TB_EMPLOYEES_PK primary key (EMPLOYEE_ID);--Union PRIMARY key ALTER TABLE tb_departments Add constraint TB_DEPARTMENTS_PK primary key (Department_id,department_name);


5 Disabling primary keys

Grammar:

ALTER TABLE table_name DISABLE CONSTRAINT constraint_name;
Example:

ALTER TABLE tb_employees disable constraint tb_employees_pk;


6 Enable primary key

Grammar:

ALTER TABLE table_name ENABLE CONSTRAINT constraint_name;

Example:

ALTER TABLE tb_employees enable constraint tb_employees_pk;

7 Deleting a primary key

Grammar:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Example:

ALTER TABLE tb_employees drop constraint tb_employees_pk;alter table tb_departments drop constraint tb_departments_pk; ALTER TABLE tb_pk_example drop constraint tb_pk_example_pk;alter table TB_SUPPLIER_EX drop constraint tb_supplier_ex_pk;



--------------------------------------------------------------------------------------------------------------- ----

If you have any problems in the process of trying, or if my code is wrong, please correct me, thank you very much!

Contact information: [Email protected]

Copyright @: Reprint please indicate the source!
--------------------------------------------------------------------------------------------------------------- -----


Oracle's primary KEY (Primary key) usage detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.