为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL 主键

标签:
MySQL

Summary: in this tutorial, you will learn how to use MySQL primary key constraint to create a primary key for the table.

Introduction to MySQL primary key

mysql primary key

A primary key is a column or a set of columns that uniquely identifies each row in the table. The following are the rules that you must follow when you define a primary key for a table:

  • A primary key must contain unique values. If the primary key consists of multiple columns, the combination of values in these columns must be unique.

  • A primary key column cannot contain NULL values. It means that you have to declare the primary key column with NOT NULL attribute. If you don’t, MySQL will force the primary key column as NOT NULL implicitly.

  • A table has only one primary key.

Because MySQL works faster with integers, the primary key column’s type should be an integer type e.g., INT or BIGINT. You can choose a smaller integer type such as TINYINTSMALLINT, etc., however you should make sure that the range of values of the integer type for the primary key is sufficient for storing all possible rows that the table may have.

A primary key column often has AUTO_INCREMENT attribute that generates a unique sequence for the key automatically. The the primary key of the next row is greater than the previous one.

MySQL creates an index named PRIMARY with PRIMARY type for the primary key in a table.

Defining MySQL PRIMARY KEY Constraints

MySQL allows you to to create a primary key by defining a primary key constraint when you create or modify the table.

Defining MySQL PRIMARY KEY constraints using CREATE TABLE statement

MySQL allows you to create the primary key when you create the table by using the CREATE TABLE statement. To create a PRIMARY KEY constraint for the table, you specify the PRIMARY KEY in the primary key column’s definition.

The following example creates users table whose primary key is user_id column:

CREATE TABLE users(    user_id INT AUTO_INCREMENT PRIMARY KEY,    username VARCHAR(40),    password VARCHAR(255),    email VARCHAR(255) );

You can also specify the PRIMARY KEY at the end of the CREATE TABLE statement as follows:

CREATE TABLE roles(    role_id INT AUTO_INCREMENT,    role_name VARCHAR(50),    PRIMARY KEY(role_id) );

In case the primary key consists of multiple columns, you must specify them at the end of the CREATE TABLE statement. You put a coma-separated list of primary key columns inside parentheses followed the PRIMARY KEY keywords.

CREATE TABLE userroles(    user_id INT NOT NULL,    role_id INT NOT NULL,    PRIMARY KEY(user_id,role_id),    FOREIGN KEY(user_id) REFERENCES users(user_id),    FOREIGN KEY(role_id) REFERENCES roles(role_id) );

Besides creating the primary key that consists of user_id and role_id columns, the statement also created two foreign key constraints.

Defining MySQL PRIMARY KEY constraints using ALTER TABLE statement

If a table, for some reasons, does not have a primary key, you can use the ALTER TABLE statement to add a column that has all necessary primary key’s characteristics to the primary key as the following statement:

ALTER TABLE table_name ADD PRIMARY KEY(primary_key_column);

The following example adds the id column to the primary key.

First, create t1 table  without defining the primary key.

CREATE TABLE t1(    id int,    title varchar(255) NOT NULL );

Second, add the id column to primary key of the t1 table.

ALTER TABLE t1 ADD PRIMARY KEY(id);

 

PRIMARY KEY vs. UNIQUE KEY vs. KEY

KEY is a synonym for INDEX. You use KEY when you want to create an index for a column or a set of column that is not a part of a primary key or unique key.

UNIQUE index creates a constraint for a column whose values must be unique. Unlike the PRIMARY index, MySQL allows NULL values in the UNIQUE index. A table can also have multiple UNIQUE indexes.

For example, the email and username of user in the users table must be unique. You can define UNIQUEindexes for the email and username column as the following  statement:

Add a   UNIQUE index for the username column.

ALTER TABLE users ADD UNIQUE INDEX username_unique (username ASC) ;

Add a UNIQUE index for the email column.

ALTER TABLE users ADD UNIQUE INDEX  email_unique (email ASC) ;

In this tutorial, you have learned how to create a primary key for a new table or add a primary key for an existing table.

原文链接:http://outofmemory.cn/mysql/mysql-primary-key

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消