Oracle's primary key (Primary key) usage detailed ____oracle

Source: Internet
Author: User

Oracle/plsql: Primary key (Primary key) usage 1 Goals

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


2 Preface-What is a primary key

In Oracle, a primary key refers to a single data table column or union of data table columns (Union PRIMARY key | Compound 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 one primary key.

Note: In an Oracle database, there are no more than 32 columns for a federated primary key. A primary key can be defined or defined by the ALTER TABLE syntax when creating a table.


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

Single column PRIMARY Key example:

CREATE TABLE tb_pk_example
(
  ID number,
  NAME varchar2,
  DESCRIPTION varchar2)
  , CONSTRAINT tb_pk_example_pk PRIMARY Key (ID)--Defines the primary key
);

Federated PRIMARY Key Example:

CREATE TABLE tb_supplier_ex
(
  supplier_id number,
  supplier_name varchar2 (m),
  Supplier_ Description Varchar2 (+), contact_name varchar2 (constraint) TB_SUPPLIER_EX_PK primary
  key (supplier_ ID, Supplier_name)--Joint primary key
);

4 Creating a primary key-using ALTER TABLE syntax

Grammar

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

Sample preparation

First create two tables (Tb_employees and Tb_departments), and the script is as follows:

CREATE TABLE Tb_employees
(
employee_id number,
employee_name varchar2 (m),
employee_age number,
Employee_birth Date,
department_id number
);

CREATE TABLE Tb_departments
(
department_id number,
department_name varchar2 (MB),
location VARCHAR2 ()
);

Create a primary key with the ALTER TABLE syntax:

--Single row primary key
ALTER TABLE tb_employees add constraint TB_EMPLOYEES_PK primary key (employee_id);
--Joint 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 Delete 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;



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

如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢。

联系方式:david.louis.tian@outlook.com 版权@:转载请标明出处。

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


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.