Home > Database > Mysql Tutorial > MySQL 零散笔记--主键_MySQL

MySQL 零散笔记--主键_MySQL

WBOY
Release: 2016-06-01 13:51:33
Original
1029 people have browsed it

Reference: 《浅谈MySQL主键》 作者:simaopig


主键没有着明确的概念定义,其是索引的一种,并且是唯一性索引的一种,且必须定义为“PRIMARY KEY”。

声明方法:

    CREATE TABLE tbl_name ([字段描述省略...], PRIMARY KEY(index_col_name));

或者

    CREATE TABLE tbl_name( id INT(10) PRIMARY KEY |…);

或者更新表结构:

   ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,…);

主键被认为是NOT NULL和UNIQUE约束最好的结合。如果这些列没有被明确地定义为NOT NULL,MySQL会隐含地定义这些列。

同时,主键也是索引。索引可以用于多字段,主键也一样,既可以作用于单独的字段,又可以作用于多个字段。组合的主键,每个列都会隐含定义NOT NULL约束,且其二者加在一起被定义了UNIQUE 惟一约束

例子代码:一个防火墙,由host和port组合确定一个防火墙

<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb;">/*
Copy after login
创建防火墙表,将host 和port组合设为主键,注意我没有将port设NOT NULL约束
Copy after login
*/
Copy after login
create table firewall(
Copy after login
host varchar(11) not null,
Copy after login
port smallint(4),
Copy after login
access enum('deny', 'allow') not null,
Copy after login
primary key (host,port)
Copy after login
)
Copy after login
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb;">/* 
Copy after login
Copy after login
Copy after login
插入一条新的记录,没有啥问题 
Copy after login
1 row(s) inserted. 
Copy after login
*/ 
Copy after login
Copy after login
Copy after login
INSERT INTO firewall ( 
Copy after login
Copy after login
host , 
Copy after login
Copy after login
port , 
Copy after login
Copy after login
access 
Copy after login
Copy after login
) 
Copy after login
Copy after login
VALUES ( 
Copy after login
Copy after login
Copy after login
'202.65.3.87', '21', 'deny' 
Copy after login
);
Copy after login
Copy after login
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb;">/* 
Copy after login
Copy after login
Copy after login
插入失败,因为host 加port的主键值202.65.3.87-21已经存在了 
Copy after login
#1062 - Duplicate entry '202.65.3.87-21' for key 'PRIMARY' 
Copy after login
*/ 
Copy after login
Copy after login
Copy after login
INSERT INTO firewall ( 
Copy after login
Copy after login
host , 
Copy after login
Copy after login
port , 
Copy after login
Copy after login
access 
Copy after login
Copy after login
) 
Copy after login
Copy after login
VALUES ( 
Copy after login
Copy after login
Copy after login
'202.65.3.87', '21', 'allow' 
Copy after login
);
Copy after login
Copy after login
<pre style="font-size: 12px; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; background-color: #fbfbfb;">/* 
Copy after login
Copy after login
Copy after login
没声明NOT NULl port也不能为NULL 
Copy after login
#1048 - Column 'port' cannot be null 
Copy after login
*/ 
Copy after login
Copy after login
Copy after login
INSERT INTO firewall( host, port, access ) 
Copy after login
VALUES ( 
Copy after login
Copy after login
Copy after login
'192.168.0.1', NULL , 'deny' 
Copy after login
)
Copy after login

host和port都可以重复,但是不能同时重复,因为其是组合主键。且二者都不能被插入NULL,因为其是主键。

Related labels:
key
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template