Home > Database > Oracle > body text

How to set primary key in oracle?

coldplay.xixi
Release: 2020-07-16 16:45:25
Original
26413 people have browsed it

How to set the primary key in oracle: 1. Define the primary key when creating the table, the code is [constraint TF_B_AIR_CONFIG_PK primary key (TYPE_ID)]; 2. After creating the table, use alter table syntax to set the primary key.

How to set primary key in oracle?

How to set the primary key in oracle:

1. What is the primary key

Primary key (primary key) is one or more fields in the table, and its value is used to uniquely identify a record in the table.

The so-called composite primary key means that the primary key of your table contains more than one field. If there is no unique primary key in the table, you can use a composite primary key to determine the uniqueness of a record.

2. Two ways to create a primary key

The first is to define the primary key when creating the table

Example of single-column primary key :

----创建TF_B_AIR_CONFIG表
create table ECS_STORE.TF_B_AIR_CONFIG(
  TYPE_ID            VARCHAR2(20) not null,
  PROVINCE_CODE      VARCHAR2(4) not null,
  PARAMETER_TYPE     VARCHAR2(2) not null,
  LIMIT_NUM          VARCHAR2(4) not null,
  EFFECTIVE_FALG     VARCHAR2(2) default '1',
  UPDATE_TIME        DATE default sysdate,
  constraint TF_B_AIR_CONFIG_PK primary key(TYPE_ID)--单列主键  
)
Copy after login

Example of composite primary key:

----创建TF_B_AIR_CONFIG表
create table ECS_STORE.TF_B_AIR_CONFIG(
  TYPE_ID            VARCHAR2(20) not null,
  PROVINCE_CODE      VARCHAR2(4) not null,
  PARAMETER_TYPE     VARCHAR2(2) not null,
  LIMIT_NUM          VARCHAR2(4) not null,
  EFFECTIVE_FALG     VARCHAR2(2) default '1',
  UPDATE_TIME        DATE default sysdate,
  constraint TF_B_AIR_CONFIG_PK primary key(TYPE_ID , PROVINCE_CODE)--复合主键  
)
Copy after login

Second, after creating the table, use alter table syntax

Syntax

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

Example

----创建TF_B_AIR_CONFIG表
create table ECS_STORE.TF_B_AIR_CONFIG(
  TYPE_ID            VARCHAR2(20) not null,
  PROVINCE_CODE      VARCHAR2(4) not null,
  PARAMETER_TYPE     VARCHAR2(2) not null,
  LIMIT_NUM          VARCHAR2(4) not null,
  EFFECTIVE_FALG     VARCHAR2(2) default '1',
  UPDATE_TIME        DATE default sysdate
)
Copy after login
rrree

Related learning recommendations: oracle database learning tutorial

The above is the detailed content of How to set primary key in oracle?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!