这些等于添加独特的约束吗?
我想知道以下查询是否等效。 我想为电影表中的两个列添加一个唯一的约束
CREATE TABLE Movie (
title VARCHAR(255),
year INTEGER,
…
producerC# INTEGER,
UNIQUE (title, year)
);
CREATE TABLE Movie (
title VARCHAR(255) UNIQUE,
year INTEGER UNIQUE,
…
producerC# INTEGER
);
I wonder if the following queries are equivalent.
I want to add a unique constraint for two of the columns in the movie table
CREATE TABLE Movie (
title VARCHAR(255),
year INTEGER,
…
producerC# INTEGER,
UNIQUE (title, year)
);
CREATE TABLE Movie (
title VARCHAR(255) UNIQUE,
year INTEGER UNIQUE,
…
producerC# INTEGER
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个版本将强制执行电影标题和电影年的组合将永远是独一无二的。因此,允许以下两种组合:
但是请注意,同一电影 can can Can 不止一次出现,只要它与同年不这样做。
第二版要求每个电影标题和年份分别是独特的。这意味着只有一年的身体抢夺者只能出现一次。
根据我的示例数据,我想您想使用第一个版本。
The first version will enforce that the combination of movie title and movie year will always be unique. So, the following two combinations are permitted:
But note that the same movie can appear more than once, provided that it does not do so with the same year.
The second version requires that each movie title and year separately are unique. This would mean that Body Snatchers can only appear once with one given year.
Based on my sample data, I would guess that you want to go with the first version.
不,他们与众不同。
意味着标题和年份的组合是独一无二的。
因此,title =“ alpha”,年=“ 2022”和title =“ beta”,Year =“ 2022”是唯一的。
暗示各个列必须具有唯一的值。
因此,上面的示例违反了上述约束,因为2022年被重复。
No they are different.
implies that combination of title and year is unique.
So title ="alpha", year="2022" and title ="beta", year="2022" are unique as per this .
implies that individual columns must have unique values.
So the example above violates the above constraint as year 2022 is repeated.