這篇文章主要介紹了MySQL中的主鍵以及設置其自增的用法教程,是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)
新聞熱點
疑難解答