Home > Database > Oracle > body text

How to add constraints in oracle

WBOY
Release: 2022-01-26 16:07:44
Original
8543 people have browsed it

Method: 1. Use the "alter table table name add constraint primary key name primary key" statement to add primary key constraints; 2. Use the "alter table table name add constraint constraint name unique" statement to add unique constraints, etc.

How to add constraints in oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

How to add constraints in oracle

The constraint types in Oracle include primary key constraints, unique constraints, foreign key constraints, and check constraints.

Primary key constraint

Primary key constraint: The primary key column data is required to be unique and cannot be empty.

Add a primary key constraint:

For example, in the student table, add a primary key constraint for the column named id (student id):

The syntax is:

alter table 表名 add constraint 主键名 primary key(字段名);
Copy after login

For example:

alter table student add constraint pk_student primary key(id);
Copy after login

Unique constraint

Unique constraint: The column is required to be unique and is allowed to be empty, but a null value cannot appear.

Add a unique constraint:

For example, in the student table, add a unique constraint to the column named name:

The syntax is:

alter table 表名 add constraint 约束名 unique(字段名);
Copy after login

For example:

alter table student add constraint uq_student unique(name);
Copy after login

Foreign key constraint

Foreign key constraint: used to establish a connection between two tables, you need to specify the reference to the main table Which column.

Add foreign key constraints:

For example, in the student table, add a foreign key constraint to the column with the field name gradeno (grade number), and the referenced foreign key is gno in the grade table. (Grade number):

The syntax is:

alter table 主表名 add constraint 外键名 foreign key(字段名) references 被引用的表名(字段名);
Copy after login

For example:

alter table student add constraint fk_student foreign key(gradeno) references grade(gno);
Copy after login

Check constraint

Check constraint : Limits on the value range of a certain column, format restrictions, etc. Such as age restrictions.

Add a check constraint:

For example, in the student table, add a check constraint for the column named gender:

The syntax is:

alter table 表名 add constraint 约束名 check(约束条件);
Copy after login

For example:

alter table student add constraint ck_student check(gender in(‘男’,‘女’));
Copy after login

This statement means that in the gender column, the data can only be male or female.

Recommended tutorial: "Oracle Video Tutorial"

The above is the detailed content of How to add constraints 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