sql 主键和外键_SQL,唯一键和主键

sql 主键和外键

With a table created with this command:

使用通过此命令创建的表:

CREATE TABLE people (
  age INT NOT NULL,
  name CHAR(20) NOT NULL
);

We can insert an item more than once.

我们可以多次插入一个项目。

And in particular, we can have columns that repeat the same value.

特别是,我们可以有重复相同值的列。

We can force a column to have only unique values using the UNIQUE key constraint:

我们可以使用UNIQUE键约束将一列强制为唯一值:

CREATE TABLE people (
  age INT NOT NULL,
  name CHAR(20) NOT NULL UNIQUE
);

Now if you try to add the ‘Flavio’ twice:

现在,如果您尝试两次添加“ Flavio”:

INSERT INTO people VALUES (37, 'Flavio');
INSERT INTO people VALUES (20, 'Flavio');

You’d get an error:

您会得到一个错误:

ERROR:  duplicate key value violates unique constraint "people_name_key"
DETAIL:  Key (name)=(Flavio) already exists.

A primary key is a unique key that has another property: it’s the primary way we identify a row in the table.

主键是具有另一个属性的唯一键:这是我们在表中标识行的主要方式。

CREATE TABLE people (
  age INT NOT NULL,
  name CHAR(20) NOT NULL PRIMARY KEY
);

The primary key can be an email in a list of users, for example.

例如,主键可以是用户列表中的电子邮件。

The primary key can be a unique id that we assign to each record automatically.

主键可以是我们自动分配给每个记录的唯一id

Whatever that value is, we know we can use it to reference a row in the table.

无论该值是多少,我们都知道可以使用它来引用表中的一行。

翻译自: https://flaviocopes.com/sql-unique-primary-keys/

sql 主键和外键

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值