MySQL中的主键以及设置其自增的用法教程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

MySQL中的主键以及设置其⾃增的⽤法教程
1、声明主键的⽅法:
您可以在创建表的时候就为表加上主键,如:
CREATE TABLE tbl_name ([字段描述省略...], PRIMARY KEY(index_col_name));
也可以更新表结构时为表加上主键,如:
ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,…);
/*
创建⼀个qq表,将qq_id设为主键,且没有对其进⾏NOT NULl约束
*/
create table qq(
qq_id int(10),
nick_name varchar(255) not null,
primary key (qq_id))
/*
插⼊⼀条数据,将qq号设为10000(咱也幻想⼀下),昵称设为"simaopig"
*/
INSERT INTO qq( qq_id, nick_name )
VALUES (
'10000', 'simaopig');
主键被认为是NOT NULL和UNIQUE约束最好的结合。

如果这些列没有被明确地定义为NOT NULL,MySQL会隐含地定义这些列。

2、主键也是索引:
刚才已经说了,主键其实也是索引,甚⾄在MySQL的术语⾥⾯“键”就等于“索引”,所以“外键”⼀定要先设为“索引”。

所以主键也应该和索引⼀样,既可以作⽤于单独的字段,⼜可以作⽤于多个字段。

举个简的例⼦吧,我住3单元,501室,我叫⼩⼩⼦,那么只有3单元501室才能在本⼩区表⾥⾯唯⼀确定我家。

因为2单
元,501室住着的可能也是个⼩⼩⼦,所以只有两个字段才能唯⼀确定我,也就是说可以⼆者组合作为主键。

组合的主键,每个列都会隐含定义NOT NULL约束,且其⼆者加在⼀起被定义了UNIQUE 惟⼀约束。

/*
创建防⽕墙表,将host 和port组合设为主键,注意我没有将port设NOT NULL约束
*/
create table firewall(
host varchar(11) not null,
port smallint(4),
access enum('deny', 'allow') not null,
primary key (host,port))
/*
插⼊⼀条新的记录,没有啥问题
1 row(s) inserted.
*/
INSERT INTO firewall (
host ,
port ,
access)
VALUES (
'202.65.3.87', '21', 'deny');
3、设置主键⾃增
下⾯我们通过⼀个实例来讲解设置主键⾃增的⽅法:
⾸先创建数据库,创建表
mysql> create database ssh2;
Query OK, 1 row affected (0.04 sec)
mysql> use ssh2;
Database changed
mysql> create table user(
-> id integer primary key,
-> firstname varchar(200) not null,
-> lastname varchar(200) not null,
-> age integer
-> );
Query OK, 0 rows affected (0.46 sec)
给主键增加⼀个⾃增的功能:
mysql> alter table user modify id integer auto_increment ;
Query OK, 1 row affected (0.28 sec)
Records: 1 Duplicates: 0 Warnings: 0
这样,上⾯的user表⾥⾯的主键,id可以⾃增了。

给上⾯的主键id增加默认值和⾃增功能。

mysql> alter table user modify id integer auto_increment ;
Query OK, 0 rows affected (0.39 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify id integer default '1';
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table user modify id integer auto_increment ;
Query OK, 1 row affected (0.28 sec)
Records: 1 Duplicates: 0 Warnings: 0
MySql获取系统时间:
mysql> alter table user add createtime timestamp default current_timestamp;
Query OK, 2 rows affected (0.17 sec)
Records: 2 Duplicates: 0 Warnings: 0
MySql设置主键不能为空,还要⾃动增长(这⾥没有设置默认值,但是默认是1,从1开始增长。

),还要得到系统默认⽇期:mysql> create table dd(
-> id int primary key not null auto_increment,
-> name varchar(20),
-> time timestamp default current_timestamp
-> );
Query OK, 0 rows affected (0.10 sec)
mysql> insert into dd(name) values ('fhihgifds');
Query OK, 1 row affected (0.14 sec)
mysql> insert into dd(name) values ('steven');
Query OK, 1 row affected (0.08 sec)
mysql> select * from dd;
+----+-----------+---------------------+
| id | name | time |
+----+-----------+---------------------+
| 1 | fhihgifds | 2011-03-27 01:58:46 |
| 2 | steven | 2011-03-27 01:59:35 |
+----+-----------+---------------------+
2 rows in set (0.08 sec)
mysql> insert into dd(name) values ('anthony'); Query OK, 1 row affected (0.09 sec)
mysql> select * from dd;
+----+-----------+---------------------+
| id | name | time |
+----+-----------+---------------------+
| 1 | fhihgifds | 2011-03-27 01:58:46 |
| 2 | steven | 2011-03-27 01:59:35 |
| 3 | anthony | 2011-03-27 02:00:07 |
+----+-----------+---------------------+
3 rows in set (0.00 sec)。

相关文档
最新文档