Table of contents
PostgreSQL constraints: used to specify data rules in tables
PRIMARY KEY constraint: primary key
FOREIGN KEY constraint: foreign key
CHECK constraint: ensure that all values in the column meet a certain condition
EXCLUSION constraint: exclusive constraint
PostgreSQL constraints: used to specify data rules in tables
PostgreSQL constraints are used to specify rules for data in a table.
If there is a data behavior that violates the constraint, the behavior will be terminated by the constraint.
Constraints can be specified when the table is created (via the CREATE TABLE statement), or after the table is created (via the ALTER TABLE statement).
Constraints ensure the accuracy and reliability of data in the database.
Constraints can be column-level or table-level. Column-level constraints apply only to columns, table-level constraints apply to the entire table.
The following are commonly used constraints in PostgreSQL:
- NOT NULL : Indicates that a column cannot store NULL values.
- UNIQUE : Ensure that the values in a column are unique.
- PRIMARY Key: A combination of NOT NULL and UNIQUE. Ensuring that a column (or a combination of two or more columns) has a unique identifier can help make it easier and faster to find a specific record in a table. .
- FOREIGN Key: Ensures referential integrity that data in one table matches values in another table.
- CHECK: Ensure that the values in the column meet the specified conditions.
- EXCLUSION: Exclusive constraint, which guarantees that if the specified columns or expressions of any two rows are compared using the specified operator, at least one of the operator comparisons will return false or a null value.
NOT NULL constraint
By default, columns can be saved as NULL values.
If you do not want a column to have NULL values, you need to define this constraint on the column, specifying that NULL values are not allowed on the column.
NULL is different from no data, it represents unknown data.
UNIQUE constraint
The UNIQUE constraint can set a column to be unique and avoid duplicate values in the same column.
PRIMARY KEY constraint: primary key
PRIMARY KEY is very important when designing a database.
PRIMARY KEY is called the primary key and is the unique identifier of each record in the data table.
There may be multiple columns for which UNIQUE is set, but only one column of a table can be set for PRIMARY KEY.
The primary key is a combination of a non-null constraint and a unique constraint.
use:
You can use primary keys to reference rows in a table,
Relationships between tables can be created by setting the primary key to a foreign key in another table.
A table can only have one primary key, which can consist of one or more fields.
Composite Key: When multiple fields serve as primary keys, they are called composite keys.
If a primary key is defined on some fields of a table, then these fields are subject to non-null constraints and unique constraints.
Example
/*下面实例创建了一张新表叫 COMPANY1,
/*添加了 5 个字段,
/*1.NOT NULL约束: 其中三个 ID,NAME,AGE 设置添加NOT NULL约束:不接受空置:
/*2.UNIQUE约束: 其中 AGE 设置为 UNIQUE,因此你不能添加两条有相同年龄的记录:
/*3.PRIMARY KEY约束: 其中 ID 作为主键:*/
CREATE TABLE COMPANY1(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL UNIQUE,
ADDRESS CHAR(50),
SALARY REAL DEFAULT 50000.00
);
FOREIGN KEY constraint: foreign key
FOREIGN KEY is a foreign key constraint, which specifies that the value in a column (or a set of columns) must match a value that appears in a row of another table.
Usually the FOREIGN KEY in one table points to the UNIQUE KEY (the key of the unique constraint) in another table, that is, the referential integrity between the two related tables is maintained.
/*下面实例创建一张 DEPARTMENT1 表,并添加 3 个字段,
/*EMP_ID 就是外键,参照 COMPANY1 的 ID:*/
CREATE TABLE DEPARTMENT1(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT references COMPANY1(ID)
);
CHECK constraint: ensure that all values in the column meet a certain condition
CHECK constraint: Ensure that all values in the column meet a certain condition, that is, inputting a record must be checked. If the condition value is false, the record violates the constraint and cannot be entered into the table.
EXCLUSION constraint: exclusive constraint
EXCLUSION constraint: Ensures that if any two rows are compared on a specified column or expression using the specified operator, at least one of the operator comparisons will return false or null.
/*下面实例创建了一张 COMPANY2 表,添加 5 个字段,
/*1.CHECK 约束:其中为 SALARY 列添加 CHECK,所以工资不能为零:
/*2.EXCLUSION 约束:使用了 EXCLUSION 约束。*/
CREATE TABLE COMPANY2(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL CHECK(SALARY > 0),
EXCLUDE USING gist
(NAME WITH =, -- 如果满足 NAME 相同,AGE 不相同则不允许插入,否则允许插入
AGE WITH <>) -- 其比较的结果是如果整个表边式返回 true,则不允许插入,否则允许
);
Here, USING gist is a type of index used to build and execute.
You need to execute the CREATE EXTENSION btree_gist command once per database, which installs the btree_gist extension, which defines EXCLUDE constraints on pure scalar data types.
Delete constraints
You must know the constraint name to delete a constraint. It is simple to delete a constraint if you already know the name. If you do not know the name, you need to find the system-generated name. You can find this information using \d table name.
/*通用语法如下:*/
ALTER TABLE table_name DROP CONSTRAINT some_name;