Get the App
SLTechnology News&Howtos  ›  Database  › 

Constraint Operation of Dameng Database

Shulou Source: shulou.com Published: 2022-06-01 22:31:11 10月05日 Update

Constraint Operation of Dameng Database

The common constraints of Dameng database are primary key constraints, foreign key constraints, unique constraints, and some less commonly used check constraints, non-empty constraints, and so on. Next, we use test1 users to create operations.

1. Create a primary key constraint

Let's first create a tab1 table to test with the test1 user and create an id at the same time

Create table tab1 (id int primary key, name char)

Inserting a piece of data is normal.

Insert into tab1 values (2,'a')

Inserting again will indicate that the insert failed, indicating a violation of the table [TAB1] uniqueness constraint.

Insert into tab1 values (2,'b')

2. Create a foreign key constraint

Create table tab2 (id int unique, name char)

Create table tab3 (id int references tab2 (id), name date)

Inserting data into two tables is normal.

Insert into tab2 values (1,'a')

Insert into tab2 values (2,'b')

Insert into tab3 values (2019-11-28')

Insert into tab3 values (2019-11-28')

Inserting again will prompt the insert to fail, indicating a violation of the reference constraint [CONS134218851].

Insert into tab3 values (3 recorder 2019-11-28')

3. Create a unique constraint

Create table tab4 (id int unique, name char)

Insert data is normal

Insert into tab4 values (1,'a')

Inserting again will prompt the insert to fail, violating the table [tab4] uniqueness constraint.

Insert into tab4 values (1,'b')

4. Add or delete constraints after creating the table

Add constraints after creating the table

Create table tab5 (id int, name char (10))

Alter table tab5 add constraint con_id primary key (id)

Delete constraint

Alter table tab5 drop constraint con_id

Tags: Data hint again database uniqueness commonly used user next two at the same time come first don't come check test Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi OPPO Reno Shulou Information Apple