如何在MySQL中的表格上保持两个列之间的完整性?

发布于 2025-01-19 01:07:08 字数 1032 浏览 4 评论 0原文

我有的列主键on Table上的普通列。我想维护Integrity大约两列。

假设我有一个用户表和两个数据。我想在ID> ID列和create_user列之间具有Integrity

CREATE TABLE USER (
    id varchar(10) not null,
    create_user varchar(10) not null,
    PRIMARY KEY (id)
);

insert into USER (id,create_user) values ('system','system');
insert into USER (id,create_user) values ('user01','system');

The result is

| id       | create_user |
| -------- | ------------|
| system   | system      |
| user01   | system      |

,如果我更新ID(主键),它没有完整性。

update USER SET id='master' WHERE id='system';

The result is

| id       | create_user |
| -------- | ------------|
| master   | system      |
| user01   | system      |

但是我想在。是否可以?我不想要其他更新queris

| id       | create_user |
| -------- | ------------|
| master   | master      |
| user01   | master      |

I have columns which are a primary key and a plain column on a table. I want to maintain integrity about two columns.

Suppose I have a user table and two datas. I want to have integrity between a id column and a create_user column

CREATE TABLE USER (
    id varchar(10) not null,
    create_user varchar(10) not null,
    PRIMARY KEY (id)
);

insert into USER (id,create_user) values ('system','system');
insert into USER (id,create_user) values ('user01','system');

The result is

| id       | create_user |
| -------- | ------------|
| system   | system      |
| user01   | system      |

If I update id(a primary key), It doesn't have integrity.

update USER SET id='master' WHERE id='system';

The result is

| id       | create_user |
| -------- | ------------|
| master   | system      |
| user01   | system      |

But I want to this on a table. Is it possible? I don't want additional update queris

| id       | create_user |
| -------- | ------------|
| master   | master      |
| user01   | master      |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2025-01-26 01:07:08

您可以根据需要更新任意数量的列,并使用 case 语句来决定将它们设置为什么

drop table if exists t;
CREATE TABLE t (
    id varchar(10) not null,
    create_user varchar(10) not null,
    PRIMARY KEY (id)
);

insert into t (id,create_user) values ('system','system');
insert into t (id,create_user) values ('user01','system');

update t
SET id = case when id= 'system' then 'master' else id end ,
    create_user = case when create_user = 'system' then 'master' else create_user end 
where id = 'system' or create_user = 'system';

select * from t;

+--------+-------------+
| id     | create_user |
+--------+-------------+
| master | master      |
| user01 | master      |
+--------+-------------+
2 rows in set (0.001 sec)

You can update as many columns as you like and use case statements to decide what to set them to

drop table if exists t;
CREATE TABLE t (
    id varchar(10) not null,
    create_user varchar(10) not null,
    PRIMARY KEY (id)
);

insert into t (id,create_user) values ('system','system');
insert into t (id,create_user) values ('user01','system');

update t
SET id = case when id= 'system' then 'master' else id end ,
    create_user = case when create_user = 'system' then 'master' else create_user end 
where id = 'system' or create_user = 'system';

select * from t;

+--------+-------------+
| id     | create_user |
+--------+-------------+
| master | master      |
| user01 | master      |
+--------+-------------+
2 rows in set (0.001 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文